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 <vector>
using namespace std;
#define int long long
typedef long long ll;
const int inf = 2e9;
int n, k, t;
vector<int> x;
vector<pair<ll, ll>> make_stack(vector<ll> v) {
int N = v.size();
vector<int> important(N);
vector<ll> pref(N), mx_pr(N), mn_pr(N);
ll mx_pref = 0, current_pref = 0;
for (int i = 0; i < N; ++i) {
current_pref += v[i];
pref[i] = current_pref;
mx_pref = max(mx_pref, pref[i]);
if (pref[i] == mx_pref) important[i] = 1;
}
mx_pr[N - 1] = mn_pr[N - 1] = pref[N - 1];
for (int i = N - 2; i >= 0; --i) {
mx_pr[i] = max(pref[i], mx_pr[i + 1]);
mn_pr[i] = min(pref[i], mn_pr[i + 1]);
}
vector<pair<ll, ll>> ret = {{0, 0}};
for (int i = 0; i < N; ++i) {
ret.back().first = min(ret.back().first, pref[i]);
ret.back().second = pref[i];
if (i < N - 1) {
if (
important[i] ||
(pref[i] == mx_pr[i] &&
mx_pr[i] > mx_pr[i + 1] &&
mx_pr[i] != mx_pref &&
ret.back().first < mn_pr[i])
)
ret.push_back({pref[i], pref[i]});
}
}
return ret;
}
bool check(int v) {
if ((ll) v * t > inf) return true;
vector<ll> aa, bb;
for (int i = k - 1; i >= 0; --i) {
aa.push_back(2LL * v * t - (x[i + 1] - x[i]));
}
for (int i = k + 1; i < n; ++i) {
bb.push_back(2LL * v * t - (x[i] - x[i - 1]));
}
// K = 1 or K = N
aa.push_back(0); bb.push_back(0);
vector<pair<ll, ll>> a = make_stack(aa);
vector<pair<ll, ll>> b = make_stack(bb);
int i = 0, j = 0;
ll sum = 0;
ll tmp_a = 0, tmp_b = 0;
while (i < (int) a.size() || j < (int) b.size()) {
bool can_a = false;
bool can_b = false;
if (i < (int) a.size() && tmp_a - a[i].first <= sum)
can_a = true;
if (j < (int) b.size() && tmp_b - b[j].first <= sum)
can_b = true;
if (can_a == can_b) {
if (!can_a) return false;
if (a[i].second >= tmp_a) can_b = false;
else if (b[j].second >= tmp_b) can_a = false;
else {
ll nxt_a = tmp_a - a[i].first;
ll nxt_b = tmp_b - b[j].first;
if (nxt_a >= nxt_b) can_b = false;
else can_a = false;
}
}
if (can_a) {
sum += a[i].second - tmp_a;
tmp_a = a[i++].second;
}
else {
sum += b[j].second - tmp_b;
tmp_b = b[j++].second;
}
// cout << sum << " " << i << " " << j << endl << endl;
}
return true;
}
signed main() {
cin >> n >> k >> t; --k;
for (int i = 0; i < n; ++i) {
int pos; cin >> pos;
x.push_back(pos);
}
int lo = 0, hi = inf;
while (lo < hi) {
int mid = (lo + hi) >> 1;
if (check(mid)) {
hi = mid;
}
else {
lo = mid + 1;
}
}
cout << lo << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |