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 <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>
#define f first
#define s second
const long long inf = 1000000000000000007;
const int maxn = 202;
int n, m;
pi a[maxn];
long long dp[maxn][maxn][maxn][2];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> a[i].f;
for(int i = 1; i <= n; i++) cin >> a[i].s;
a[n + 1].f = m;
sort(a + 1, a + n + 1);
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
for(int k = n + 1; k > j; k--) dp[i][j][k][0] = dp[i][j][k][1] = inf;
int ret = 0;
dp[0][0][n + 1][0] = dp[0][0][n + 1][1] = 0;
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
for(int k = n + 1; k > j; k--){
long long x;
if(dp[i][j][k][0] != inf){
ret = i;
x = dp[i][j][k][0] + a[j + 1].f - a[j].f;
dp[i][j + 1][k][0] = min(dp[i][j + 1][k][0], x);
if(x <= a[j + 1].s) dp[i + 1][j + 1][k][0] = min(dp[i + 1][j + 1][k][0], x);
x = dp[i][j][k][0] + a[j].f + m - a[k - 1].f;
dp[i][j][k - 1][1] = min(dp[i][j][k - 1][1], x);
if(x <= a[k - 1].s) dp[i + 1][j][k - 1][1] = min(dp[i + 1][j][k - 1][1], x);
}
if(dp[i][j][k][1] != inf){
ret = i;
x = dp[i][j][k][1] + a[k].f - a[k - 1].f;
dp[i][j][k - 1][1] = min(dp[i][j][k - 1][1], x);
if(x <= a[k - 1].s) dp[i + 1][j][k - 1][1] = min(dp[i + 1][j][k - 1][1], x);
x = dp[i][j][k][1] + a[j + 1].f + m - a[k].f;
dp[i][j + 1][k][0] = min(dp[i][j + 1][k][0], x);
if(x <= a[j + 1].s) dp[i + 1][j + 1][k][0] = min(dp[i + 1][j + 1][k][0], x);
}
}
cout << ret << endl;
return 0;
}
# | 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... |