#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cycle_len;
vector<pair<int, int>> a;
vector<vector<vector<array<ll, 2>>>> dp;
// Counter-clockwise
ll get_dist(ll l, ll r){
if (l < -1 || l > a.size()) return 1e15;
if (r < -1 || r > a.size()) return 1e15;
if (l == -1 || l == a.size()) l = 0;
else l = a[l].first;
if (r == -1 || r == a.size()) r = 0;
else r = a[r].first;
return (l + cycle_len - r) % cycle_len;
}
ll get_time(int idx){
if (idx == -1 || idx == a.size()) return -1;
return a[idx].second;
}
ll f(int l, int r, int picked, bool lr){
if (l == -1 || l == a.size()+1 || r == -1 || r == a.size()+1 || picked == -1) return 1e15;
if (l + r > a.size()) return 1e15;
if (l == 0 && r == 0){
if (picked) return 1e15;
return 0;
}
ll& value = dp[l][r][picked][lr];
if (value != -1) return value;
value = 1e15;
if (!lr){
// At left
// Case 1: From left
{
ll picking = f(l-1, r, picked-1, false) + get_dist(l-1, l-2);
if (picking <= get_time(l-1)) value = min(value, picking);
ll not_picking = f(l-1, r, picked, false) + get_dist(l-1, l-2);
value = min(value, not_picking);
}
// Case 2: From right
{
ll picking = f(l-1, r, picked-1, true) + get_dist(l-1, a.size()-r);
if (picking <= get_time(l-1)) value = min(value, picking);
ll not_picking = f(l-1, r, picked, true) + get_dist(l-1, a.size()-r);
value = min(value, not_picking);
}
}
else{
// At right
// Case 1: From right
{
ll picking = f(l, r-1, picked-1, true) + get_dist(a.size()-r+1, a.size()-r);
if (picking <= get_time(a.size()-r)) value = min(value, picking);
ll not_picking = f(l, r-1, picked, true) + get_dist(a.size()-r+1, a.size()-r);
value = min(value, not_picking);
}
// Case 2: From left
{
ll picking = f(l, r-1, picked-1, false) + get_dist(l-1, a.size()-r);
if (picking <= get_time(a.size()-r)) value = min(value, picking);
ll not_picking = f(l, r-1, picked, false) + get_dist(l-1, a.size()-r);
value = min(value, not_picking);
}
}
return value;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n >> cycle_len;
a.resize(n);
for (int i=0; i<n; i++){
cin >> a[i].first;
a[i].first %= cycle_len;
}
for (int i=0; i<n; i++){
cin >> a[i].second;
}
sort(a.begin(), a.end());
dp.assign(n+1, vector<vector<array<ll, 2>>> (n+1, vector<array<ll, 2>> (n+1, array<ll, 2>({-1, -1}))));
for (int result = n; result >= 0; result--){
for (int l=0; l<=a.size(); l++){
for (int r=0; r<=a.size(); r++){
if (f(l, r, result, false) < 1e15 || f(l, r, result, true) < 1e15){
cout << result << "\n";
return 0;
}
}
}
}
}
Compilation message
ho_t3.cpp: In function 'll get_dist(ll, ll)':
ho_t3.cpp:10:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | if (l < -1 || l > a.size()) return 1e15;
| ~~^~~~~~~~~~
ho_t3.cpp:11:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | if (r < -1 || r > a.size()) return 1e15;
| ~~^~~~~~~~~~
ho_t3.cpp:12:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | if (l == -1 || l == a.size()) l = 0;
| ~~^~~~~~~~~~~
ho_t3.cpp:14:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | if (r == -1 || r == a.size()) r = 0;
| ~~^~~~~~~~~~~
ho_t3.cpp: In function 'll get_time(int)':
ho_t3.cpp:19:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | if (idx == -1 || idx == a.size()) return -1;
| ~~~~^~~~~~~~~~~
ho_t3.cpp: In function 'll f(int, int, int, bool)':
ho_t3.cpp:23:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | if (l == -1 || l == a.size()+1 || r == -1 || r == a.size()+1 || picked == -1) return 1e15;
| ~~^~~~~~~~~~~~~
ho_t3.cpp:23:52: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | if (l == -1 || l == a.size()+1 || r == -1 || r == a.size()+1 || picked == -1) return 1e15;
| ~~^~~~~~~~~~~~~
ho_t3.cpp:24:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | if (l + r > a.size()) return 1e15;
| ~~~~~~^~~~~~~~~~
ho_t3.cpp: In function 'int main()':
ho_t3.cpp:84:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for (int l=0; l<=a.size(); l++){
| ~^~~~~~~~~~
ho_t3.cpp:85:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for (int r=0; r<=a.size(); r++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
320 KB |
Output is correct |
28 |
Correct |
1 ms |
320 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
270 ms |
93508 KB |
Output is correct |
19 |
Correct |
108 ms |
44244 KB |
Output is correct |
20 |
Correct |
63 ms |
17492 KB |
Output is correct |
21 |
Correct |
108 ms |
40708 KB |
Output is correct |
22 |
Correct |
148 ms |
59964 KB |
Output is correct |
23 |
Correct |
21 ms |
13780 KB |
Output is correct |
24 |
Correct |
25 ms |
9664 KB |
Output is correct |
25 |
Correct |
18 ms |
13376 KB |
Output is correct |
26 |
Correct |
9 ms |
3284 KB |
Output is correct |
27 |
Correct |
20 ms |
14164 KB |
Output is correct |
28 |
Correct |
28 ms |
8660 KB |
Output is correct |
29 |
Correct |
36 ms |
14688 KB |
Output is correct |
30 |
Correct |
27 ms |
10304 KB |
Output is correct |
31 |
Correct |
30 ms |
13268 KB |
Output is correct |
32 |
Correct |
13 ms |
5204 KB |
Output is correct |
33 |
Correct |
38 ms |
13396 KB |
Output is correct |
34 |
Correct |
6 ms |
3028 KB |
Output is correct |
35 |
Correct |
42 ms |
12884 KB |
Output is correct |
36 |
Correct |
11 ms |
4436 KB |
Output is correct |
37 |
Correct |
67 ms |
14144 KB |
Output is correct |
38 |
Correct |
20 ms |
5948 KB |
Output is correct |
39 |
Correct |
55 ms |
15060 KB |
Output is correct |
40 |
Correct |
17 ms |
6996 KB |
Output is correct |
41 |
Correct |
264 ms |
127768 KB |
Output is correct |
42 |
Correct |
296 ms |
70856 KB |
Output is correct |
43 |
Correct |
258 ms |
127912 KB |
Output is correct |
44 |
Correct |
263 ms |
69580 KB |
Output is correct |
45 |
Correct |
266 ms |
127840 KB |
Output is correct |
46 |
Correct |
263 ms |
70788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
320 KB |
Output is correct |
28 |
Correct |
1 ms |
320 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
320 KB |
Output is correct |
36 |
Correct |
270 ms |
93508 KB |
Output is correct |
37 |
Correct |
108 ms |
44244 KB |
Output is correct |
38 |
Correct |
63 ms |
17492 KB |
Output is correct |
39 |
Correct |
108 ms |
40708 KB |
Output is correct |
40 |
Correct |
148 ms |
59964 KB |
Output is correct |
41 |
Correct |
21 ms |
13780 KB |
Output is correct |
42 |
Correct |
25 ms |
9664 KB |
Output is correct |
43 |
Correct |
18 ms |
13376 KB |
Output is correct |
44 |
Correct |
9 ms |
3284 KB |
Output is correct |
45 |
Correct |
20 ms |
14164 KB |
Output is correct |
46 |
Correct |
28 ms |
8660 KB |
Output is correct |
47 |
Correct |
36 ms |
14688 KB |
Output is correct |
48 |
Correct |
27 ms |
10304 KB |
Output is correct |
49 |
Correct |
30 ms |
13268 KB |
Output is correct |
50 |
Correct |
13 ms |
5204 KB |
Output is correct |
51 |
Correct |
38 ms |
13396 KB |
Output is correct |
52 |
Correct |
6 ms |
3028 KB |
Output is correct |
53 |
Correct |
42 ms |
12884 KB |
Output is correct |
54 |
Correct |
11 ms |
4436 KB |
Output is correct |
55 |
Correct |
67 ms |
14144 KB |
Output is correct |
56 |
Correct |
20 ms |
5948 KB |
Output is correct |
57 |
Correct |
55 ms |
15060 KB |
Output is correct |
58 |
Correct |
17 ms |
6996 KB |
Output is correct |
59 |
Correct |
264 ms |
127768 KB |
Output is correct |
60 |
Correct |
296 ms |
70856 KB |
Output is correct |
61 |
Correct |
258 ms |
127912 KB |
Output is correct |
62 |
Correct |
263 ms |
69580 KB |
Output is correct |
63 |
Correct |
266 ms |
127840 KB |
Output is correct |
64 |
Correct |
263 ms |
70788 KB |
Output is correct |
65 |
Correct |
309 ms |
109692 KB |
Output is correct |
66 |
Correct |
291 ms |
96536 KB |
Output is correct |
67 |
Correct |
247 ms |
90368 KB |
Output is correct |
68 |
Correct |
254 ms |
80220 KB |
Output is correct |
69 |
Correct |
370 ms |
107988 KB |
Output is correct |
70 |
Correct |
160 ms |
101336 KB |
Output is correct |
71 |
Correct |
326 ms |
102968 KB |
Output is correct |
72 |
Correct |
153 ms |
104624 KB |
Output is correct |
73 |
Correct |
316 ms |
93408 KB |
Output is correct |
74 |
Correct |
142 ms |
84468 KB |
Output is correct |
75 |
Correct |
271 ms |
97996 KB |
Output is correct |
76 |
Correct |
230 ms |
120320 KB |
Output is correct |
77 |
Correct |
424 ms |
120320 KB |
Output is correct |
78 |
Correct |
161 ms |
81612 KB |
Output is correct |
79 |
Correct |
240 ms |
85912 KB |
Output is correct |
80 |
Correct |
218 ms |
116612 KB |
Output is correct |
81 |
Correct |
240 ms |
87384 KB |
Output is correct |
82 |
Correct |
400 ms |
94956 KB |
Output is correct |
83 |
Correct |
399 ms |
127776 KB |
Output is correct |
84 |
Correct |
370 ms |
101332 KB |
Output is correct |
85 |
Correct |
323 ms |
114928 KB |
Output is correct |
86 |
Correct |
552 ms |
111424 KB |
Output is correct |
87 |
Correct |
338 ms |
98116 KB |
Output is correct |
88 |
Correct |
162 ms |
129656 KB |
Output is correct |
89 |
Correct |
302 ms |
129684 KB |
Output is correct |
90 |
Correct |
640 ms |
99716 KB |
Output is correct |
91 |
Correct |
188 ms |
129680 KB |
Output is correct |
92 |
Correct |
298 ms |
129576 KB |
Output is correct |
93 |
Correct |
527 ms |
125880 KB |
Output is correct |