#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 = a[i].second - a[i].first;
ll nxt_b = b[j].second - 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 |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
484 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
444 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
536 KB |
Output is correct |
38 |
Correct |
1 ms |
440 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
484 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
2 ms |
444 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
536 KB |
Output is correct |
38 |
Correct |
1 ms |
440 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
92 ms |
5976 KB |
Output is correct |
42 |
Correct |
3 ms |
700 KB |
Output is correct |
43 |
Correct |
29 ms |
1892 KB |
Output is correct |
44 |
Correct |
127 ms |
10564 KB |
Output is correct |
45 |
Correct |
135 ms |
7944 KB |
Output is correct |
46 |
Correct |
123 ms |
10068 KB |
Output is correct |
47 |
Correct |
129 ms |
10276 KB |
Output is correct |
48 |
Correct |
109 ms |
8152 KB |
Output is correct |
49 |
Correct |
127 ms |
10664 KB |
Output is correct |
50 |
Correct |
104 ms |
7752 KB |
Output is correct |
51 |
Correct |
118 ms |
10240 KB |
Output is correct |
52 |
Correct |
123 ms |
10052 KB |
Output is correct |
53 |
Correct |
136 ms |
10656 KB |
Output is correct |
54 |
Correct |
135 ms |
10940 KB |
Output is correct |
55 |
Correct |
104 ms |
7896 KB |
Output is correct |
56 |
Correct |
121 ms |
10252 KB |
Output is correct |
57 |
Correct |
154 ms |
10184 KB |
Output is correct |
58 |
Correct |
112 ms |
8188 KB |
Output is correct |
59 |
Correct |
114 ms |
7236 KB |
Output is correct |
60 |
Correct |
117 ms |
10032 KB |
Output is correct |
61 |
Correct |
126 ms |
9764 KB |
Output is correct |
62 |
Correct |
107 ms |
10352 KB |
Output is correct |
63 |
Correct |
122 ms |
9220 KB |
Output is correct |
64 |
Correct |
102 ms |
7580 KB |
Output is correct |
65 |
Correct |
99 ms |
8064 KB |
Output is correct |
66 |
Correct |
103 ms |
7640 KB |
Output is correct |
67 |
Correct |
102 ms |
8356 KB |
Output is correct |
68 |
Correct |
122 ms |
9972 KB |
Output is correct |
69 |
Correct |
125 ms |
10068 KB |
Output is correct |