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>
#include "overtaking.h"
using namespace std;
using ll = long long;
const int N = 1009;
ll n, m, x, w[N], s[N], t[N][N], f[N][N];
void init(int L, int N1, vector < ll > T, vector < int > W, int X, int M, vector < int > S) {
n = N1, m = M, x = X;
vector < ll > nw, nt;
for (int i = 0; i < n; i++) if (W[i] > x) {
nw.push_back(W[i]);
nt.push_back(T[i]);
}
n = (int)nt.size();
for (int i = 0; i < n; i++) T[i] = nt[i], w[i] = nw[i];
for (int i = 0; i < m; i++) s[i] = S[i];
for (int i = 0; i < n; i++) t[0][i] = T[i];
for (int i = 1; i < m; i++) {
ll d = s[i] - s[i - 1];
vector < vector < ll > > vec;
for (int j = 0; j < n; j++) vec.push_back({t[i - 1][j], t[i - 1][j] + w[j] * d, j});
sort(vec.begin(), vec.end());
ll j = 0, mx = 0;
while (j < n) {
int l = j;
while (l + 1 < n && vec[l][0] == vec[l + 1][0]) l++;
for (int p = j; p <= l; p++) t[i][vec[p][2]] = max(mx, t[i - 1][vec[p][2]] + w[vec[p][2]] * d);
while (j <= l) {
mx = max(mx, vec[j][1]);
j++;
}
}
}
for (int i = 0; i < m; i++) sort(t[i], t[i] + n);
for (int i = 0; i < n; i++) f[m - 1][i] = t[m - 1][i];
for (int i = m - 2; i >= 0; i--) {
f[i][0] = t[i][0] + x * (s[m - 1] - s[i]);
for (int j = 1; j < n; j++) {
int l = i + 1, r = m - 1;
while (l < r) {
int mid = (l + r) >> 1;
if (t[i][j] + x * (s[mid] - s[i]) <= t[mid][j - 1]) r = mid;
else l = mid + 1;
}
int pt = j - 1;
while (pt > 0 && t[l][pt - 1] == t[l][pt]) pt--;
if (t[i][j] + x * (s[l] - s[i]) <= t[l][j - 1]) f[i][j] = f[l][pt];
else f[i][j] = t[i][j] + x * (s[m - 1] - s[i]);
}
}
}
ll arrival_time(ll y) {
if (y <= t[0][0]) return y + x * s[m - 1];
int l = 0, r = n - 1;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (t[0][mid] < y) l = mid;
else r = mid - 1;
}
int lg = 1, rg = m - 1;
while (lg < rg) {
int mid = (lg + rg) >> 1;
if (y + x * s[mid] <= t[mid][l]) rg = mid;
else lg = mid + 1;
}
while (l > 0 && t[lg][l - 1] == t[lg][l]) l--;
if (y + x * s[lg] <= t[lg][l]) return f[lg][l];
return y + x * s[m - 1];
}
/*int main() {
//ios_base::sync_with_stdio(0); cin.tie(0);
ifstream cin("3-21.in");
ifstream fin("3-21.out");
int l, q;
cin >> l >> n >> x >> m >> q;
vector < ll > T(n);
vector < int > W(n), S(m);
for (int i = 0; i < n; i++) cin >> T[i];
for (int i = 0; i < n; i++) cin >> W[i];
for (int i = 0; i < m; i++) cin >> S[i];
init(l, n, T, W, x, m, S);
for (int it = 1; it <= q; it++) {
ll y;
cin >> y;
ll crr = arrival_time(y, (it == 65 || it == 75));
ll c;
fin >> c;
if (c != crr) cout << "bad! " << it << " " << crr << " " << c << "\n";
}
}*/
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |