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;
using lint = long long;
const int MAXN = 205;
int N, L;
int X[MAXN], T[MAXN];
lint dist[MAXN][MAXN][2][MAXN];
struct State {
int left;
int right;
int pos_right;
int stamps;
State() {}
State(int l, int r, int p, int s) : left(l), right(r), pos_right(p), stamps(s) {}
bool operator < (const State &o) const {
return stamps < o.stamps;
}
};
int Distance(int a, int b) {
if (a > b) swap(a, b);
return min(X[b] - X[a], X[a] + L - X[b]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
memset(dist, -1, sizeof(dist));
cin >> N >> L;
for (int i = 1; i <= N; i++) {
cin >> X[i];
}
X[N + 1] = L;
for (int i = 1; i <= N; i++) {
cin >> T[i];
}
priority_queue<pair<lint, State>, vector<pair<lint, State>>, greater<pair<lint, State>>> pq;
pq.emplace(0, State(0, N + 1, 0, 0));
dist[0][N + 1][0][0] = 0;
int ans = 0;
while (!pq.empty()) {
int left = pq.top().second.left;
int right = pq.top().second.right;
int pos_right = pq.top().second.pos_right;
int stamps = pq.top().second.stamps;
lint dt = pq.top().first;
pq.pop();
ans = max(ans, stamps);
if (left > right || dist[left][right][pos_right][stamps] != dt) {
continue;
}
int cur = pos_right ? right : left;
if (left + 1 < right) {
lint &nxt = dist[left + 1][right][0][stamps + (dt + Distance(left + 1, cur) <= T[left + 1])];
if (nxt == -1 || nxt > dt + Distance(left + 1, cur)) {
nxt = dt + Distance(left + 1, cur);
pq.emplace(nxt, State(left + 1, right, 0, stamps + (dt + Distance(left + 1, cur) <= T[left + 1])));
}
}
if (left < right - 1) {
lint &nxt = dist[left][right - 1][1][stamps + (dt + Distance(right - 1, cur) <= T[right - 1])];
if (nxt == -1 || nxt > dt + Distance(right - 1, cur)) {
nxt = dt + Distance(right - 1, cur);
pq.emplace(nxt, State(left, right - 1, 1, stamps + (dt + Distance(right - 1, cur) <= T[right - 1])));
}
}
}
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... |