#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define PB push_back
#define PPB pop_back
#define all(x) (x).begin(), (x).end()
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef long double ld;
const int MAXN = 1e6 + 7;
const int logo = 20;
const int off = 1 << logo;
const int trsz = off << 1;
const int mod = 1e9 + 7;
const ll inf = 1e18 + 7;
const ll INF = 1e18 + 7;
ll x[MAXN], n, k, t;
vll lef, rig;
vpll L, R, nl, nr;
bool okej(ll poc, vpll &vec){
for(auto &y : vec){
if(poc + y.Y < 0) return 0;
poc += y.X;
}
return poc >= 0;
}
bool check(ll mid){
lef.clear();
rig.clear();
L.clear();
R.clear();
nl.clear();
nr.clear();
ll sum = 0;
for(int i=k; i<n; i++) rig.PB((ll)2 * mid * t - (ll)(x[i + 1] - (ll)x[i]) / 2), sum += rig.back();
for(int i=1; i<k; i++) lef.PB((ll)2 * mid * t - (ll)(x[i + 1] - (ll)x[i]) / 2), sum += lef.back();
reverse(all(lef));
if(sum < 0) return 0;
ll csu = 0, mi = 0;
int ls = -1;
for(int i=0; i<(int)rig.size(); i++){
csu += rig[i];
mi = min(mi, csu);
if(csu >= 0){
R.PB({csu, mi});
csu = mi = 0;
ls = i;
}
}
csu = mi = 0;
for(int i=(int)rig.size() - 1; i>ls; i--){
csu -= rig[i];
mi = min(mi, csu);
if(csu >= 0){
nr.PB({csu, mi});
csu = mi = 0;
}
}
csu = 0, mi = 0;
ls = -1;
for(int i=0; i<(int)lef.size(); i++){
csu += lef[i];
mi = min(mi, csu);
if(csu >= 0){
L.PB({csu, mi});
csu = mi = 0;
ls = i;
}
}
csu = mi = 0;
for(int i=(int)lef.size() - 1; i>ls; i--){
csu -= lef[i];
mi = min(mi, csu);
if(csu >= 0){
nl.PB({csu, mi});
csu = mi = 0;
}
}
int li = 0, ri = 0;
csu = 0;
while(1){
pll c1 = {-inf, -inf}, c2 = {-inf, -inf};
if(li < (int)L.size()) c1 = L[li];
if(ri < (int)R.size()) c2 = R[ri];
if(c1.X == -inf and c2.X == -inf) break;
if(csu + c1.Y >= 0){
csu += c1.X;
li++;
continue;
}
if(csu + c2.Y >= 0){
csu += c2.X;
ri++;
continue;
}
return 0;
}
csu = sum;
li = ri = 0;
swap(L, nl);
swap(R, nr);
while(1){
pll c1 = {-inf, -inf}, c2 = {-inf, -inf};
if(li < (int)L.size()) c1 = L[li];
if(ri < (int)R.size()) c2 = R[ri];
if(c1.X == -inf and c2.X == -inf) break;
if(csu + c1.Y >= 0){
csu += c1.X;
li++;
continue;
}
if(csu + c2.Y >= 0){
csu += c2.X;
ri++;
continue;
}
return 0;
}
return 1;
}
void solve(){
cin >> n >> k >> t;
for(int i=1; i<=n; i++) cin >> x[i], x[i] *= 2;
int lo = 0, hi = ((1e9 + 1) / t) + 1, ret = -1;
while(lo <= hi){
int mid = (lo + hi) / 2;
if(check(mid)) hi = mid - 1, ret = mid;
else lo = mid + 1;
}
cout << ret << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt = 1;
//cin >> t;
while(tt--) solve();
return 0;
}
/*
-15708 2615 22272 -6638 -35674 -50958 41359 -1318
-33710 -40703 4522 12061
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 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 |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
544 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 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 |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
544 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
25 ms |
6864 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
7 ms |
1596 KB |
Output is correct |
44 |
Correct |
32 ms |
9428 KB |
Output is correct |
45 |
Correct |
31 ms |
8168 KB |
Output is correct |
46 |
Correct |
34 ms |
9172 KB |
Output is correct |
47 |
Correct |
37 ms |
9172 KB |
Output is correct |
48 |
Correct |
38 ms |
8424 KB |
Output is correct |
49 |
Correct |
33 ms |
9060 KB |
Output is correct |
50 |
Correct |
32 ms |
8168 KB |
Output is correct |
51 |
Correct |
35 ms |
9480 KB |
Output is correct |
52 |
Correct |
33 ms |
9824 KB |
Output is correct |
53 |
Correct |
36 ms |
9364 KB |
Output is correct |
54 |
Correct |
40 ms |
9540 KB |
Output is correct |
55 |
Correct |
33 ms |
8164 KB |
Output is correct |
56 |
Correct |
42 ms |
9428 KB |
Output is correct |
57 |
Correct |
34 ms |
9672 KB |
Output is correct |
58 |
Correct |
34 ms |
8424 KB |
Output is correct |
59 |
Correct |
40 ms |
7400 KB |
Output is correct |
60 |
Correct |
33 ms |
9808 KB |
Output is correct |
61 |
Correct |
34 ms |
9548 KB |
Output is correct |
62 |
Correct |
34 ms |
9424 KB |
Output is correct |
63 |
Correct |
30 ms |
9552 KB |
Output is correct |
64 |
Correct |
30 ms |
8064 KB |
Output is correct |
65 |
Correct |
35 ms |
8408 KB |
Output is correct |
66 |
Correct |
34 ms |
8168 KB |
Output is correct |
67 |
Correct |
33 ms |
8420 KB |
Output is correct |
68 |
Correct |
30 ms |
9548 KB |
Output is correct |
69 |
Correct |
31 ms |
9824 KB |
Output is correct |