This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int dp[205][205][205][2]; //[L][R][got][where]
int main () {
int n,p;
scanf ("%d %d",&n,&p);
auto dist = [&] (int a, int b) {int d = abs(a-b); return min(d,p-d);};
vector<int> pos(n+1), t(n+1);
for (int i = 1; i <= n; i++) scanf ("%d",&pos[i]);
for (int i = 1; i <= n; i++) scanf ("%d",&t[i]);
memset(dp,0x3f,sizeof dp);
dp[0][0][0][0] = dp[0][0][0][1] = 0; int ret = 0; ++n;
for (int len = 2; len <= n; len++) {
for (int st = 1 - len; st <= 0; st++) {
int l = (st + n) % n, r = (l + len - 1) % n;
for (int taken = 0; taken < len; taken++) {
int l2 = (l + 1) % n, r2 = (r - 1 + n) % n;
dp[l][r][taken][0] = min({dp[l][r][taken][0],dp[l2][r][taken][0] + dist(pos[l2],pos[l]), dp[l2][r][taken][1] + dist(pos[r],pos[l])});
dp[l][r][taken][1] = min({dp[l][r][taken][1],dp[l][r2][taken][0] + dist(pos[l],pos[r]), dp[l][r2][taken][1] + dist(pos[r2],pos[r])});
if (taken) {
if (dp[l2][r][taken-1][0] + dist(pos[l2],pos[l]) <= t[l]) {
dp[l][r][taken][0] = min(dp[l][r][taken][0],dp[l2][r][taken-1][0] + dist(pos[l2],pos[l]));
}
if (dp[l2][r][taken-1][1] + dist(pos[l],pos[r]) <= t[l]) {
dp[l][r][taken][0] = min(dp[l][r][taken][0],dp[l2][r][taken-1][1] + dist(pos[l],pos[r]));
}
if (dp[l][r2][taken-1][0] + dist(pos[l],pos[r]) <= t[r]) {
dp[l][r][taken][1] = min(dp[l][r][taken][1],dp[l][r2][taken-1][0] + dist(pos[l],pos[r]));
}
if (dp[l][r2][taken-1][1] + dist(pos[r2],pos[r]) <= t[r]) {
dp[l][r][taken][1] = min(dp[l][r][taken][1],dp[l][r2][taken-1][1] + dist(pos[r2],pos[r]));
}
}
if (min(dp[l][r][taken][0],dp[l][r][taken][1]) <= 1'000'000'000) ret = max(ret,taken);
}
}
}
printf ("%d\n",ret);
return 0;
}
Compilation message (stderr)
ho_t3.cpp: In function 'int main()':
ho_t3.cpp:6:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
6 | scanf ("%d %d",&n,&p);
| ~~~~~~^~~~~~~~~~~~~~~
ho_t3.cpp:9:37: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
9 | for (int i = 1; i <= n; i++) scanf ("%d",&pos[i]);
| ~~~~~~^~~~~~~~~~~~~~
ho_t3.cpp:10:37: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
10 | for (int i = 1; i <= n; i++) scanf ("%d",&t[i]);
| ~~~~~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |