#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
206 ms |
104316 KB |
Output is correct |
19 |
Correct |
103 ms |
49496 KB |
Output is correct |
20 |
Correct |
43 ms |
19396 KB |
Output is correct |
21 |
Correct |
108 ms |
45384 KB |
Output is correct |
22 |
Correct |
135 ms |
67012 KB |
Output is correct |
23 |
Correct |
38 ms |
15524 KB |
Output is correct |
24 |
Correct |
13 ms |
10572 KB |
Output is correct |
25 |
Correct |
34 ms |
15056 KB |
Output is correct |
26 |
Correct |
5 ms |
3660 KB |
Output is correct |
27 |
Correct |
39 ms |
16068 KB |
Output is correct |
28 |
Correct |
12 ms |
9488 KB |
Output is correct |
29 |
Correct |
34 ms |
16276 KB |
Output is correct |
30 |
Correct |
15 ms |
11212 KB |
Output is correct |
31 |
Correct |
40 ms |
14796 KB |
Output is correct |
32 |
Correct |
13 ms |
5708 KB |
Output is correct |
33 |
Correct |
29 ms |
14788 KB |
Output is correct |
34 |
Correct |
5 ms |
3276 KB |
Output is correct |
35 |
Correct |
15 ms |
14052 KB |
Output is correct |
36 |
Correct |
6 ms |
4812 KB |
Output is correct |
37 |
Correct |
15 ms |
15436 KB |
Output is correct |
38 |
Correct |
10 ms |
6444 KB |
Output is correct |
39 |
Correct |
16 ms |
16416 KB |
Output is correct |
40 |
Correct |
8 ms |
7500 KB |
Output is correct |
41 |
Correct |
333 ms |
143372 KB |
Output is correct |
42 |
Correct |
46 ms |
77976 KB |
Output is correct |
43 |
Correct |
309 ms |
143332 KB |
Output is correct |
44 |
Correct |
45 ms |
76592 KB |
Output is correct |
45 |
Correct |
342 ms |
143404 KB |
Output is correct |
46 |
Correct |
46 ms |
77968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
0 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
0 ms |
332 KB |
Output is correct |
36 |
Correct |
206 ms |
104316 KB |
Output is correct |
37 |
Correct |
103 ms |
49496 KB |
Output is correct |
38 |
Correct |
43 ms |
19396 KB |
Output is correct |
39 |
Correct |
108 ms |
45384 KB |
Output is correct |
40 |
Correct |
135 ms |
67012 KB |
Output is correct |
41 |
Correct |
38 ms |
15524 KB |
Output is correct |
42 |
Correct |
13 ms |
10572 KB |
Output is correct |
43 |
Correct |
34 ms |
15056 KB |
Output is correct |
44 |
Correct |
5 ms |
3660 KB |
Output is correct |
45 |
Correct |
39 ms |
16068 KB |
Output is correct |
46 |
Correct |
12 ms |
9488 KB |
Output is correct |
47 |
Correct |
34 ms |
16276 KB |
Output is correct |
48 |
Correct |
15 ms |
11212 KB |
Output is correct |
49 |
Correct |
40 ms |
14796 KB |
Output is correct |
50 |
Correct |
13 ms |
5708 KB |
Output is correct |
51 |
Correct |
29 ms |
14788 KB |
Output is correct |
52 |
Correct |
5 ms |
3276 KB |
Output is correct |
53 |
Correct |
15 ms |
14052 KB |
Output is correct |
54 |
Correct |
6 ms |
4812 KB |
Output is correct |
55 |
Correct |
15 ms |
15436 KB |
Output is correct |
56 |
Correct |
10 ms |
6444 KB |
Output is correct |
57 |
Correct |
16 ms |
16416 KB |
Output is correct |
58 |
Correct |
8 ms |
7500 KB |
Output is correct |
59 |
Correct |
333 ms |
143372 KB |
Output is correct |
60 |
Correct |
46 ms |
77976 KB |
Output is correct |
61 |
Correct |
309 ms |
143332 KB |
Output is correct |
62 |
Correct |
45 ms |
76592 KB |
Output is correct |
63 |
Correct |
342 ms |
143404 KB |
Output is correct |
64 |
Correct |
46 ms |
77968 KB |
Output is correct |
65 |
Correct |
251 ms |
122748 KB |
Output is correct |
66 |
Correct |
212 ms |
108092 KB |
Output is correct |
67 |
Correct |
195 ms |
100944 KB |
Output is correct |
68 |
Correct |
179 ms |
89640 KB |
Output is correct |
69 |
Correct |
249 ms |
120840 KB |
Output is correct |
70 |
Correct |
322 ms |
114376 KB |
Output is correct |
71 |
Correct |
236 ms |
114832 KB |
Output is correct |
72 |
Correct |
321 ms |
118176 KB |
Output is correct |
73 |
Correct |
157 ms |
103988 KB |
Output is correct |
74 |
Correct |
249 ms |
95344 KB |
Output is correct |
75 |
Correct |
204 ms |
109392 KB |
Output is correct |
76 |
Correct |
367 ms |
135056 KB |
Output is correct |
77 |
Correct |
286 ms |
134524 KB |
Output is correct |
78 |
Correct |
214 ms |
91508 KB |
Output is correct |
79 |
Correct |
307 ms |
96212 KB |
Output is correct |
80 |
Correct |
352 ms |
130980 KB |
Output is correct |
81 |
Correct |
228 ms |
97592 KB |
Output is correct |
82 |
Correct |
159 ms |
105460 KB |
Output is correct |
83 |
Correct |
321 ms |
142788 KB |
Output is correct |
84 |
Correct |
180 ms |
112576 KB |
Output is correct |
85 |
Correct |
253 ms |
128068 KB |
Output is correct |
86 |
Correct |
283 ms |
123912 KB |
Output is correct |
87 |
Correct |
268 ms |
110132 KB |
Output is correct |
88 |
Correct |
478 ms |
146128 KB |
Output is correct |
89 |
Correct |
329 ms |
145604 KB |
Output is correct |
90 |
Correct |
64 ms |
109996 KB |
Output is correct |
91 |
Correct |
426 ms |
146032 KB |
Output is correct |
92 |
Correct |
323 ms |
145468 KB |
Output is correct |
93 |
Correct |
88 ms |
139096 KB |
Output is correct |