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>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() {cerr << endl;}
template<class T, class ...U> void kout (T v, U ...e) { cerr << v << ' ', kout(e...); }
template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// What I should check
// 1. overflow
// 2. corner cases
// Enjoy the problem instead of hurrying to AC
// Good luck !
const int MAX_N = 100010;
const ll inf = 1ll << 55;
int n, k, T;
int pos[MAX_N];
ll a[MAX_N];
bool valid(ll S) {
for (int i = 1;i <= n;++i) {
a[i] = 2 * S * T * i - pos[i];
}
int cl = k, cr = k, gl = min_element(a+1, a+1+k) - a, gr = max_element(a+k, a+1+n) - a;
// a[l] <= a[r] for all time
ll lv = a[cl], rv = a[cr];
while (cl > gl || cr < gr) {
int sl = cl, sr = cr;
while (cl > gl && a[cl-1] <= rv)
chmin(lv, a[--cl]);
while (cr < gr && a[cr+1] >= lv)
chmax(rv, a[++cr]);
if (sl == cl && sr == cr) return false;
}
lv = a[1], rv = a[n];
if (lv > rv) return false;
cl = 1, cr = n;
while (cl < gl || cr > gr) {
int sl = cl, sr = cr;
while (cl < gl && a[cl+1] <= rv)
chmin(lv, a[++cl]);
while (cr > gr && a[cr-1] >= lv)
chmax(rv, a[--cr]);
if (sl == cl && sr == cr) return false;
}
return true;
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> k >> T;
for (int i = 1;i <= n;++i)
cin >> pos[i];
//assert(n < MAX_N);
ll L = 0, R = 2 * pos[n], M;
while (L < R) {
M = L + R >> 1;
if (valid(M))
R = M;
else L = M + 1;
}
cout << L << '\n';
}
Compilation message (stderr)
sparklers.cpp: In function 'int32_t main()':
sparklers.cpp:79:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
79 | M = L + R >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |