#include<bits/stdc++.h>
#define f first
#define int long long
#define s second
using namespace std;
const int N=205,Inf=1e10;
int t,dp[N][N][N][2],n,l,ans;
pair<int,int>p[N];
string s;
int add(int x ,int y){
if(x<y) swap(x,y);
return min(x-y,l-x+y);
}
main(){
// t=1;
cin >> n >> l;
for(int i=1;i<=n;i++){
cin>>p[i].f;
}
for(int i=1;i<=n;i++){
cin>>p[i].s;
}
sort(p+1,p+n+1);
for(int i=0;i<=n+1;i++){
for(int j=0;j<=n+1;j++){
for(int k=0;k<=n;k++){
dp[i][j][k][0]=dp[i][j][k][1]=Inf;
}
}
}
dp[n+1][0][0][0]=dp[n+1][0][0][1]=0;
p[n+1].f=l;
for(int i=1;i<=n;i++){
for(int k=0;k<=n;k++){
dp[n+1][i][k][1] = dp[n+1][i-1][k][1] + add(p[i].f,p[i-1].f);
if(k && dp[n+1][i-1][k-1][1] + add(p[i].f,p[i-1].f)<= p[i].s) {
dp[n+1][i][k][1]=min(dp[n+1][i][k][1],dp[n+1][i-1][k-1][1] + add(p[i].f,p[i-1].f));
ans = max(ans,k);
}
}
}
// 1 1
for(int i=n;i>=0;i--){
for(int j=0;j<i;j++){
for(int k=0;k<=n;k++){
if(i==0 && j==0) continue;
dp[i][j][k][0] = min(dp[i+1][j][k][0] + add(p[i+1].f,p[i].f), dp[i+1][j][k][1] + add(p[j].f,p[i].f) );
if(j) dp[i][j][k][1] = min(dp[i][j-1][k][1] + add(p[j-1].f,p[j].f), dp[i][j-1][k][0] + add(p[j].f,p[i].f));
if(k) {
if(j && dp[i][j-1][k-1][0]+add(p[j].f,p[i].f)<=p[j].s){
dp[i][j][k][1]=min(dp[i][j][k][1], dp[i][j-1][k-1][0]+add(p[j].f,p[i].f));
}
if(j && dp[i][j-1][k-1][1]+add(p[j].f,p[j-1].f)<=p[j].s){
dp[i][j][k][1]=min(dp[i][j][k][1],dp[i][j-1][k-1][1]+add(p[j].f,p[j-1].f));
}
if(dp[i+1][j][k-1][0]+add(p[i+1].f,p[i].f)<=p[i].s){
dp[i][j][k][0]=min(dp[i][j][k][0],dp[i+1][j][k-1][0]+add(p[i+1].f,p[i].f));
}
if(dp[i+1][j][k-1][1]+add(p[i].f,p[j].f)<=p[i].s){
dp[i][j][k][0]=min(dp[i][j][k][0],dp[i+1][j][k-1][1]+add(p[j].f,p[i].f));
}
}
if(dp[i][j][k][0]<Inf || dp[i][j][k][1]<Inf) ans=max(ans,k);//cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k][0]<<endl;
}
}
}
cout<<ans;
}
Compilation message
ho_t3.cpp:14:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
14 | main(){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
844 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
844 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
844 KB |
Output is correct |
17 |
Correct |
1 ms |
944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
844 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
844 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
844 KB |
Output is correct |
17 |
Correct |
1 ms |
944 KB |
Output is correct |
18 |
Correct |
1 ms |
1104 KB |
Output is correct |
19 |
Correct |
1 ms |
716 KB |
Output is correct |
20 |
Correct |
1 ms |
844 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
1196 KB |
Output is correct |
24 |
Correct |
1 ms |
972 KB |
Output is correct |
25 |
Correct |
1 ms |
1100 KB |
Output is correct |
26 |
Correct |
1 ms |
1100 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
1 ms |
1232 KB |
Output is correct |
30 |
Correct |
1 ms |
1228 KB |
Output is correct |
31 |
Correct |
1 ms |
1100 KB |
Output is correct |
32 |
Correct |
1 ms |
972 KB |
Output is correct |
33 |
Correct |
1 ms |
1200 KB |
Output is correct |
34 |
Correct |
1 ms |
1196 KB |
Output is correct |
35 |
Correct |
1 ms |
1200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
844 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
844 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
844 KB |
Output is correct |
17 |
Correct |
1 ms |
944 KB |
Output is correct |
18 |
Correct |
69 ms |
106000 KB |
Output is correct |
19 |
Correct |
47 ms |
64568 KB |
Output is correct |
20 |
Correct |
21 ms |
34636 KB |
Output is correct |
21 |
Correct |
37 ms |
60968 KB |
Output is correct |
22 |
Correct |
52 ms |
78924 KB |
Output is correct |
23 |
Correct |
19 ms |
29516 KB |
Output is correct |
24 |
Correct |
15 ms |
23116 KB |
Output is correct |
25 |
Correct |
19 ms |
28864 KB |
Output is correct |
26 |
Correct |
7 ms |
11212 KB |
Output is correct |
27 |
Correct |
20 ms |
30160 KB |
Output is correct |
28 |
Correct |
13 ms |
21580 KB |
Output is correct |
29 |
Correct |
19 ms |
30700 KB |
Output is correct |
30 |
Correct |
15 ms |
24228 KB |
Output is correct |
31 |
Correct |
18 ms |
28844 KB |
Output is correct |
32 |
Correct |
10 ms |
15336 KB |
Output is correct |
33 |
Correct |
18 ms |
28868 KB |
Output is correct |
34 |
Correct |
6 ms |
10412 KB |
Output is correct |
35 |
Correct |
17 ms |
28236 KB |
Output is correct |
36 |
Correct |
10 ms |
13512 KB |
Output is correct |
37 |
Correct |
19 ms |
30156 KB |
Output is correct |
38 |
Correct |
11 ms |
16588 KB |
Output is correct |
39 |
Correct |
20 ms |
31396 KB |
Output is correct |
40 |
Correct |
11 ms |
18508 KB |
Output is correct |
41 |
Correct |
90 ms |
130656 KB |
Output is correct |
42 |
Correct |
56 ms |
88268 KB |
Output is correct |
43 |
Correct |
90 ms |
130696 KB |
Output is correct |
44 |
Correct |
59 ms |
87096 KB |
Output is correct |
45 |
Correct |
90 ms |
130660 KB |
Output is correct |
46 |
Correct |
56 ms |
88132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
844 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
844 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
844 KB |
Output is correct |
17 |
Correct |
1 ms |
944 KB |
Output is correct |
18 |
Correct |
1 ms |
1104 KB |
Output is correct |
19 |
Correct |
1 ms |
716 KB |
Output is correct |
20 |
Correct |
1 ms |
844 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
1196 KB |
Output is correct |
24 |
Correct |
1 ms |
972 KB |
Output is correct |
25 |
Correct |
1 ms |
1100 KB |
Output is correct |
26 |
Correct |
1 ms |
1100 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
1 ms |
1232 KB |
Output is correct |
30 |
Correct |
1 ms |
1228 KB |
Output is correct |
31 |
Correct |
1 ms |
1100 KB |
Output is correct |
32 |
Correct |
1 ms |
972 KB |
Output is correct |
33 |
Correct |
1 ms |
1200 KB |
Output is correct |
34 |
Correct |
1 ms |
1196 KB |
Output is correct |
35 |
Correct |
1 ms |
1200 KB |
Output is correct |
36 |
Correct |
69 ms |
106000 KB |
Output is correct |
37 |
Correct |
47 ms |
64568 KB |
Output is correct |
38 |
Correct |
21 ms |
34636 KB |
Output is correct |
39 |
Correct |
37 ms |
60968 KB |
Output is correct |
40 |
Correct |
52 ms |
78924 KB |
Output is correct |
41 |
Correct |
19 ms |
29516 KB |
Output is correct |
42 |
Correct |
15 ms |
23116 KB |
Output is correct |
43 |
Correct |
19 ms |
28864 KB |
Output is correct |
44 |
Correct |
7 ms |
11212 KB |
Output is correct |
45 |
Correct |
20 ms |
30160 KB |
Output is correct |
46 |
Correct |
13 ms |
21580 KB |
Output is correct |
47 |
Correct |
19 ms |
30700 KB |
Output is correct |
48 |
Correct |
15 ms |
24228 KB |
Output is correct |
49 |
Correct |
18 ms |
28844 KB |
Output is correct |
50 |
Correct |
10 ms |
15336 KB |
Output is correct |
51 |
Correct |
18 ms |
28868 KB |
Output is correct |
52 |
Correct |
6 ms |
10412 KB |
Output is correct |
53 |
Correct |
17 ms |
28236 KB |
Output is correct |
54 |
Correct |
10 ms |
13512 KB |
Output is correct |
55 |
Correct |
19 ms |
30156 KB |
Output is correct |
56 |
Correct |
11 ms |
16588 KB |
Output is correct |
57 |
Correct |
20 ms |
31396 KB |
Output is correct |
58 |
Correct |
11 ms |
18508 KB |
Output is correct |
59 |
Correct |
90 ms |
130656 KB |
Output is correct |
60 |
Correct |
56 ms |
88268 KB |
Output is correct |
61 |
Correct |
90 ms |
130696 KB |
Output is correct |
62 |
Correct |
59 ms |
87096 KB |
Output is correct |
63 |
Correct |
90 ms |
130660 KB |
Output is correct |
64 |
Correct |
56 ms |
88132 KB |
Output is correct |
65 |
Correct |
79 ms |
118064 KB |
Output is correct |
66 |
Correct |
72 ms |
108376 KB |
Output is correct |
67 |
Correct |
67 ms |
103748 KB |
Output is correct |
68 |
Correct |
63 ms |
95792 KB |
Output is correct |
69 |
Correct |
83 ms |
116856 KB |
Output is correct |
70 |
Correct |
79 ms |
112012 KB |
Output is correct |
71 |
Correct |
76 ms |
113224 KB |
Output is correct |
72 |
Correct |
81 ms |
114432 KB |
Output is correct |
73 |
Correct |
74 ms |
106172 KB |
Output is correct |
74 |
Correct |
70 ms |
99172 KB |
Output is correct |
75 |
Correct |
77 ms |
109572 KB |
Output is correct |
76 |
Correct |
88 ms |
125612 KB |
Output is correct |
77 |
Correct |
83 ms |
125492 KB |
Output is correct |
78 |
Correct |
67 ms |
96916 KB |
Output is correct |
79 |
Correct |
68 ms |
100292 KB |
Output is correct |
80 |
Correct |
89 ms |
122980 KB |
Output is correct |
81 |
Correct |
66 ms |
101488 KB |
Output is correct |
82 |
Correct |
70 ms |
107276 KB |
Output is correct |
83 |
Correct |
89 ms |
130648 KB |
Output is correct |
84 |
Correct |
74 ms |
112068 KB |
Output is correct |
85 |
Correct |
83 ms |
121796 KB |
Output is correct |
86 |
Correct |
84 ms |
119228 KB |
Output is correct |
87 |
Correct |
72 ms |
109636 KB |
Output is correct |
88 |
Correct |
116 ms |
132024 KB |
Output is correct |
89 |
Correct |
98 ms |
132032 KB |
Output is correct |
90 |
Correct |
73 ms |
110784 KB |
Output is correct |
91 |
Correct |
93 ms |
132116 KB |
Output is correct |
92 |
Correct |
92 ms |
132008 KB |
Output is correct |
93 |
Correct |
104 ms |
129332 KB |
Output is correct |