#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=1e9+7;
const int LIM=1e5+7;
ll T[LIM], n, k, t;
vector<ll>A, B;
vector<pair<ll,ll>>calc(vector<pair<ll,ll>>T) {
ll m=T.size()-1;
vector<pair<ll,ll>>P;
for(int i=(int)T.size()-1; i>=0; --i) {
pair<ll,ll>x={max(0ll, T[i].st), T[i].nd-T[i].st};
while(P.size()>0 && x.nd<0) {
if(P.back().nd<0) break;
pair<ll,ll>y={max(x.st, P.back().st-x.nd), x.nd+P.back().nd};
P.pop_back();
x=y;
}
P.pb(x);
}
reverse(all(P));
return P;
}
bool check(ll x) {
vector<pair<ll,ll>>X, Y;
for(auto i : A) X.pb({i, 2*x*t});
for(auto i : B) Y.pb({i, 2*x*t});
vector<pair<ll,ll>>P=calc(X), Q=calc(Y);
ll l1=0, l2=0, akt=2*x*t;
while(l1<P.size() && l2<Q.size()) {
if(P[l1].st>akt && Q[l2].st>akt) return false;
if(P[l1].st<=akt && P[l1].nd>=0) {
akt+=P[l1].nd;
++l1;
} else if(Q[l2].st<=akt && Q[l2].nd>=0) {
akt+=Q[l2].nd;
++l2;
} else break;
}
if(l1<P.size() && P[l1].st>akt && P[l1].nd>0) return false;
if(l2<Q.size() && Q[l2].st>akt && Q[l2].nd>0) return false;
vector<ll>prefp, prefq;
prefp.pb(0); prefq.pb(0);
for(int i=l1; i<P.size(); ++i) prefp.pb(prefp.back()+P[i].nd);
for(int i=l2; i<Q.size(); ++i) prefq.pb(prefq.back()+Q[i].nd);
int a=prefp.size(), b=prefq.size();
vector<ll>kiedyp(a), kiedyq(b);
rep(i, a-1) if(akt+prefp[a-i-2]<P[P.size()-i-1].st) return false;
rep(i, b-1) if(akt+prefq[b-i-2]<Q[Q.size()-i-1].st) return false;
int l=b-1;
rep(i, a-1) {
while(akt+prefp[a-i-2]+prefq[l]<P[P.size()-i-1].st) --l;
kiedyp[a-i-1]=l;
}
l=a-1;
rep(i, b-1) {
while(akt+prefq[b-i-2]+prefp[l]<Q[Q.size()-i-1].st) --l;
kiedyq[b-i-1]=l;
}
for(int i=1; i<a; ++i) if(kiedyp[i]<b-1 && kiedyq[kiedyp[i]+1]<i) return false;
for(int i=1; i<b; ++i) if(kiedyq[i]<a-1 && kiedyp[kiedyq[i]+1]<i) return false;
return true;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> k >> t;
rep(i, n) cin >> T[i];
rep(i, k-1) A.pb(T[i+1]-T[i]);
for(int i=n-1; i>=k; --i) B.pb(T[i]-T[i-1]);
reverse(all(A));
reverse(all(B));
ll po=0, ko=INF;
while(po<ko) {
ll sr=(po+ko)/2;
if(check(sr)) ko=sr; else po=sr+1;
}
cout << po << '\n';
}
Compilation message
sparklers.cpp: In function 'std::vector<std::pair<long long int, long long int> > calc(std::vector<std::pair<long long int, long long int> >)':
sparklers.cpp:15:6: warning: unused variable 'm' [-Wunused-variable]
15 | ll m=T.size()-1;
| ^
sparklers.cpp: In function 'bool check(ll)':
sparklers.cpp:36:11: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | while(l1<P.size() && l2<Q.size()) {
| ~~^~~~~~~~~
sparklers.cpp:36:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | while(l1<P.size() && l2<Q.size()) {
| ~~^~~~~~~~~
sparklers.cpp:46:8: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | if(l1<P.size() && P[l1].st>akt && P[l1].nd>0) return false;
| ~~^~~~~~~~~
sparklers.cpp:47:8: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | if(l2<Q.size() && Q[l2].st>akt && Q[l2].nd>0) return false;
| ~~^~~~~~~~~
sparklers.cpp:50:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i=l1; i<P.size(); ++i) prefp.pb(prefp.back()+P[i].nd);
| ~^~~~~~~~~
sparklers.cpp:51:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i=l2; i<Q.size(); ++i) prefq.pb(prefq.back()+Q[i].nd);
| ~^~~~~~~~~
# |
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 |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 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 |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 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 |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
66 ms |
6440 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
19 ms |
2040 KB |
Output is correct |
44 |
Correct |
108 ms |
11340 KB |
Output is correct |
45 |
Correct |
100 ms |
8172 KB |
Output is correct |
46 |
Correct |
87 ms |
10416 KB |
Output is correct |
47 |
Correct |
92 ms |
10860 KB |
Output is correct |
48 |
Correct |
101 ms |
8540 KB |
Output is correct |
49 |
Correct |
119 ms |
10024 KB |
Output is correct |
50 |
Correct |
96 ms |
8164 KB |
Output is correct |
51 |
Correct |
92 ms |
10288 KB |
Output is correct |
52 |
Correct |
95 ms |
10216 KB |
Output is correct |
53 |
Correct |
103 ms |
10048 KB |
Output is correct |
54 |
Correct |
93 ms |
9816 KB |
Output is correct |
55 |
Correct |
86 ms |
8240 KB |
Output is correct |
56 |
Correct |
93 ms |
11256 KB |
Output is correct |
57 |
Correct |
102 ms |
9888 KB |
Output is correct |
58 |
Correct |
94 ms |
8512 KB |
Output is correct |
59 |
Correct |
101 ms |
7440 KB |
Output is correct |
60 |
Correct |
89 ms |
10040 KB |
Output is correct |
61 |
Correct |
86 ms |
10516 KB |
Output is correct |
62 |
Correct |
79 ms |
10508 KB |
Output is correct |
63 |
Correct |
88 ms |
9552 KB |
Output is correct |
64 |
Correct |
89 ms |
8300 KB |
Output is correct |
65 |
Correct |
95 ms |
8532 KB |
Output is correct |
66 |
Correct |
90 ms |
8108 KB |
Output is correct |
67 |
Correct |
101 ms |
8504 KB |
Output is correct |
68 |
Correct |
87 ms |
9844 KB |
Output is correct |
69 |
Correct |
91 ms |
10032 KB |
Output is correct |