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 <algorithm>
#include <array>
#include <iostream>
#include <vector>
int main() {
int n, l;
std::cin >> n >> l;
std::vector<int> x(n + 2, 0), t(n + 2, 0);
for (int i = 1; i <= n; ++i) std::cin >> x[i];
for (int i = 1; i <= n; ++i) std::cin >> t[i];
x[n + 1] = l;
std::vector<std::vector<std::vector<std::array<int, 2>>>> dp(
n + 2, std::vector<std::vector<std::array<int, 2>>>(
n + 2, std::vector<std::array<int, 2>>(n + 2)));
constexpr int kInf = 1'000'000'000;
for (int i = 0; i <= n + 1; ++i) {
for (int j = 0; j <= n + 1; ++j) {
for (int k = 0; k <= n + 1; ++k) {
dp[i][j][k][0] = kInf;
dp[i][j][k][1] = kInf;
}
}
}
dp[0][n + 1][0][0] = 0;
dp[0][n + 1][0][1] = 0;
for (int i = 0; i <= n + 1; ++i) {
for (int j = n + 1; j >= 0; --j) {
for (int k = 0; k <= n + 1; ++k) {
if (dp[i][j][k][0] != kInf) {
if (i + 1 < j) {
int g = dp[i][j][k][0] + x[i + 1] - x[i];
if (g <= t[i + 1] && k <= n) {
dp[i + 1][j][k + 1][0] = std::min(dp[i + 1][j][k + 1][0], g);
}
dp[i + 1][j][k][0] = std::min(dp[i + 1][j][k][0], g);
}
if (j - 1 > i) {
int g = dp[i][j][k][0] + l - (x[j - 1] - x[i]);
if (g <= t[j - 1] && k <= n) {
dp[i][j - 1][k + 1][1] = std::min(dp[i][j - 1][k + 1][1], g);
}
dp[i][j - 1][k][1] = std::min(dp[i][j - 1][k][1], g);
}
}
if (dp[i][j][k][1] != kInf) {
if (i + 1 < j) {
int g = dp[i][j][k][1] + l - (x[j] - x[i + 1]);
if (g <= t[i + 1] && k <= n) {
dp[i + 1][j][k + 1][0] = std::min(dp[i + 1][j][k + 1][0], g);
}
dp[i + 1][j][k][0] = std::min(dp[i + 1][j][k][0], g);
}
if (j - 1 > i) {
int g = dp[i][j][k][1] + x[j] - x[j - 1];
if (g <= t[j - 1] && k <= n) {
dp[i][j - 1][k + 1][1] = std::min(dp[i][j - 1][k + 1][1], g);
}
dp[i][j - 1][k][1] = std::min(dp[i][j - 1][k][1], g);
}
}
}
}
}
int ans = 0;
for (int i = 0; i <= n + 1; ++i) {
for (int j = 0; j <= n + 1; ++j) {
for (int k = 0; k <= n + 1; ++k) {
if (dp[i][j][k][0] != kInf) ans = std::max(ans, k);
if (dp[i][j][k][1] != kInf) ans = std::max(ans, k);
}
}
}
std::cout << ans << "\n";
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... |