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 "overtaking.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mxN = 1007;
int n, len, x, m, y, t[mxN][mxN], e[mxN][mxN], gt[mxN][mxN], pref[mxN][mxN];
vector<int> s, vec;
vector<pair<int,int>> arr;
int cur_ind;
bool cmp(int a, int b) {
if (t[a][cur_ind] == t[b][cur_ind]) {
return arr[a].second < arr[b].second;
}
return t[a][cur_ind] < t[b][cur_ind];
}
void init(int32_t L, int32_t N, std::vector<long long> T, std::vector<int32_t> W, int32_t X, int32_t M, std::vector<int32_t> S)
{
n = N, len = L, m = M, x = X;
vector<int> inds;
for (int i = 0; i < n; ++i) {
arr.push_back({T[i], W[i]});
vec.push_back(T[i]);
inds.push_back(i);
}
sort(arr.begin(), arr.end());
sort(vec.begin(), vec.end());
for (int i = 0; i < n; ++i) {
t[i][0] = arr[i].first;
}
for (int i = 0; i < m; ++i) {
s.push_back(S[i]);
}
for (int j = 1; j < m; ++j) {
for (int i = 0; i < n; ++i) {
e[i][j] = t[i][j - 1] + arr[i].second * (s[j] - s[j - 1]);
}
cur_ind = j - 1;
sort(inds.begin(), inds.end(), cmp);
int mx = 0;
for (auto i : inds) {
mx = max(mx, e[i][j]);
t[i][j] = mx;
}
mx = -1;
int id = 0;
for (int i = 0; i < n; ++i) {
if (mx <= t[i][j]) {
mx = t[i][j];
id = i;
}
pref[i][j] = id;
}
}
for (int i = 0; i < n; ++i) {
gt[i][m - 1] = t[i][m - 1];
}
for (int j = m - 2; j >= 0; --j) {
cur_ind = j;
sort(inds.begin(), inds.end(), cmp);
for (int it1 = 0; it1 < n; ++it1) {
int i = inds[it1];
pair<int,int> mn = {LLONG_MAX, LLONG_MAX};
int check = 0;
for (int it2 = 0; it2 < it1; ++it2) {
int k = inds[it2];
int lb = j + 1, rb = m - 1;
while (lb <= rb) {
int mb = (lb + rb) / 2;
if (t[k][mb] >= t[i][j] + x * (s[mb] - s[j])) {
rb = mb - 1;
if (mb < mn.first || (mb == mn.first && t[k][mb] >= mn.second)) {
mn = {mb, t[k][mb]};
check = gt[k][mb];
}
}
else {
lb = mb + 1;
}
}
}
gt[i][j] = (!check ? t[i][j] + x * (len - s[j]) : check);
}
}
}
long long arrival_time(long long Y)
{
y = Y;
int ind = lower_bound(vec.begin(), vec.end(), y) - vec.begin();
if (!ind) {
return y + x * len;
}
ind--;
int lb = 0, rb = m - 1, res = -1;
while (lb <= rb) {
int mb = (lb + rb) / 2;
int id = pref[ind][mb];
int vala = y + s[mb] * x, valb = t[id][mb];
if (valb >= vala) {
rb = mb - 1;
res = gt[id][mb];
}
else {
lb = mb + 1;
}
}
if (res == -1) {
return y + x * len;
}
return res;
}
# | 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... |