답안 #431620

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
431620 2021-06-17T13:40:52 Z jamezzz Long Distance Coach (JOI17_coach) C++17
0 / 100
1 ms 332 KB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#include <ext/rope>
using namespace __gnu_cxx;

typedef tree<long long, null_type, less<long long>,
rb_tree_tag, tree_order_statistics_node_update> pbds;
//less_equal for identical elements

#define DEBUG

#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__);
#else
#define debug(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));

ll x,n,m,w,t,s[10],d,c,memo[12][515],memo2[12][515];
vll v;

//cost to travel to pos+1 if u r at pos, with this mask of people
ll calc(int pos,int msk){
	if(memo2[pos][msk]!=-1)return memo2[pos][msk];
	ll res=0;
	for(int i=0;i<=m;++i){
		if(msk&(1<<i)){
			if(v[i].fi>s[pos+1])continue; //start after next pos
			ll l,r;
			if(s[pos]%t<=v[i].fi%t)l=s[pos]+v[i].fi%t-s[pos]%t;
			else l=s[pos]+v[i].fi%t+t-s[pos]%t;
			if(v[i].fi%t<=s[pos+1]%t)r=s[pos+1]-(s[pos+1]%t-v[i].fi%t);
			else r=s[pos+1]-(s[pos+1]%t+t-v[i].fi%t);
			if(r<l||s[pos+1]<l||r<s[pos])continue;
			res+=((r-l)/t+1)*w;
		}
	}
	return memo2[pos][msk]=res;
}

//check if i is between pos and pos+1
bool between(int pos,int i){
	ll l;
	if(s[pos]%t<=v[i].fi%t)l=s[pos]+v[i].fi%t-s[pos]%t;
	else l=s[pos]+v[i].fi%t+t-s[pos]%t;
	if(l>s[pos+1])return false;
	return true;
}

//minimum cost if u r at pos, with this mask of people
ll dp(int pos,int msk){
	int tmsk=msk;
	if(memo[pos][msk]!=-1)return memo[pos][msk];
	if(pos==n+1){
		ll res=0;
		for(int i=0;i<=m;++i){
			if((msk&(1<<i))==0)res+=v[i].se;
		}
		return memo[pos][msk]=res;
	}
	ll cost=calc(pos,msk),res=dp(pos+1,msk)+cost;
	for(int i=m;i>=1;--i){
		if((msk&(1<<i))==0)continue;
		if(s[pos+1]%t<v[i].fi%t)continue;
		if(s[pos+1]<v[i].fi)continue;
		if(!between(pos,i))continue;
		msk^=(1<<i);cost-=w;
		assert(cost>=0);
		res=min(res,dp(pos+1,msk)+cost);
	}
	return memo[pos][tmsk]=res;
}

bool cmp(pll &a,pll &b){
	if(a.fi%t==b.fi%t)return a.fi<b.fi;
	return a.fi%t<b.fi%t;
}

int main(){
	sf("%lld%lld%lld%lld%lld",&x,&n,&m,&w,&t);
	assert(n<=8&&m<=8);
	for(int i=1;i<=n;++i)sf("%lld",&s[i]);
	s[n+1]=x;
	for(int i=0;i<m;++i){
		sf("%lld%lld",&d,&c);
		v.pb(d,c);
	}
	v.pb(0,-1);
	sort(all(v),cmp);
	memset(memo,-1,sizeof memo);
	memset(memo2,-1,sizeof memo2);
	pf("%lld\n",dp(0,(1<<(m+1))-1));
}

Compilation message

coach.cpp: In function 'int main()':
coach.cpp:103:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |  sf("%lld%lld%lld%lld%lld",&x,&n,&m,&w,&t);
      |    ^
coach.cpp:105:25: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |  for(int i=1;i<=n;++i)sf("%lld",&s[i]);
      |                         ^
coach.cpp:108:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |   sf("%lld%lld",&d,&c);
      |     ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Incorrect 1 ms 332 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Incorrect 1 ms 332 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Incorrect 1 ms 332 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Incorrect 1 ms 332 KB Output isn't correct
6 Halted 0 ms 0 KB -