제출 #486672

#제출 시각아이디문제언어결과실행 시간메모리
486672PoPularPlusPlusCollecting Stamps 3 (JOI20_ho_t3)C++17
100 / 100
478 ms146128 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long 
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007; 
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}

void solve(){
	int n;
	ll l;
	cin >> n >> l;
	ll arr[n];
	for(int i = 0; i < n; i++)cin >> arr[i];
	ll t[n];
	for(int i = 0; i < n; i++)cin >> t[i];
	queue<vector<int>> q;
	// i , j , taken, place
	q.push({n-1,0,0,0});
	q.push({n-1,0,0,1});
	ll dp[n + 1][n + 1][n + 1][2];
	bool vis[n + 1][n + 1][n + 1][2];
	memset(vis,0,sizeof vis);
	for(int i = 0; i <= n; i++){
		for(int j = 0; j <= n; j++){
			for(int k = 0; k <= n; k++)dp[i][j][k][0] = dp[i][j][k][1] = 1e18;
		}
	}
	dp[n-1][0][0][0] = dp[n-1][0][0][1] = 0;
	int ans = 0;
	int lol = 0;
	while(q.size()){
		vector<int> v = q.front();
		q.pop();
		int i = v[0] , j = v[1];
		ll time = dp[i][j][v[2]][v[3]];
		lol++;
		if((i + 1) % n == j && lol > 2){
			ans = max(ans , v[2]);
			continue;
		}
		if(v[3] == 0){
			int nexti = (i + 1) % n;
			ll dis = 0;
			if(i == n-1)dis = arr[nexti];
			else dis = arr[nexti] - arr[i];
			int t1 = v[2];
			if(time + dis <= t[nexti])t1++;
			dp[nexti][j][t1][0] = min(dp[nexti][j][t1][0] , dis + time);
			if(!vis[nexti][j][t1][0]){
				q.push({nexti,j,t1,0});
				vis[nexti][j][t1][0] = 1;
			}
			int nextj = (j + n - 1) % n;
			if(i == n-1)dis = l - arr[nextj];
			else dis = arr[i] + (l - arr[nextj]);
			t1 = v[2];
			if(time + dis <= t[nextj])t1++;
			dp[i][nextj][t1][1] = min(dp[i][nextj][t1][1] , dis + time);
			if(!vis[i][nextj][t1][1]){
				q.push({i , nextj , t1 , 1});
				vis[i][nextj][t1][1] = 1;
			}
		}
		else {
			int nexti = (i + 1) % n;
			ll dis =arr[nexti] + (j == 0 ? 0 : l - arr[j]);
			int t1 = v[2];
			if(time + dis <= t[nexti])t1++;
			dp[nexti][j][t1][0] = min(dp[nexti][j][t1][0] , dis + time);
			if(!vis[nexti][j][t1][0]){
				q.push({nexti,j,t1,0});
				vis[nexti][j][t1][0] = 1;
			}
			int nextj = (j + n - 1) % n;
			if(j == 0)dis = l - arr[nextj];
			else dis = (arr[j] - arr[nextj]);
			t1 = v[2];
			if(time + dis <= t[nextj])t1++;
			dp[i][nextj][t1][1] = min(dp[i][nextj][t1][1] , dis + time);
			if(!vis[i][nextj][t1][1]){
				q.push({i , nextj , t1 , 1});
				vis[i][nextj][t1][1] = 1;
			}
		}
	}
	cout << ans << '\n';
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
	//int t;cin >> t;while(t--)
	solve();
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...