#include<bits/stdc++.h>
using namespace std;
const long long N=999999999999999999LL;
long long a,b,c,d,e,i,j,ii,jj,zx,xc,f[1000009],dp[1000009],K,B,X,z;
deque <pair <long long, long long > > de;
pair <long long, long long> seg[4000009];
deque <pair <long long, pair <long long, long long> > > SEG;
void upd(long long q, long long w, long long rr){
if(q>w) return;
if(seg[rr]==make_pair(-1LL,-1LL)){
SEG.push_back({rr,{seg[rr].first,seg[rr].second}});
seg[rr]={K,B};
return;
}
long long mid=(q+w)/2;
if(seg[rr].first*mid+seg[rr].second>K*mid+B){
SEG.push_back({rr,{seg[rr].first,seg[rr].second}});
swap(seg[rr].first,K);swap(seg[rr].second,B);
}
if(seg[rr].first*q+seg[rr].second>K*q+B){
upd(q,mid-1,rr*2);
}else{
if(seg[rr].first*w+seg[rr].second>K*w+B){
upd(mid+1,w,rr*2+1);
}
}
}
void read(long long q, long long w, long long rr){
if(q>w) return;
if(q==w){
if(seg[rr]!=make_pair(-1LL,-1LL)) z=min(z,seg[rr].first*X+seg[rr].second);
return;
}
long long mid=(q+w)/2;
if(seg[rr]!=make_pair(-1LL,-1LL)) z=min(z,seg[rr].first*X+seg[rr].second);
if(X<mid){
read(q,mid-1,rr*2);
}
if(X>mid){
read(mid+1,w,rr*2+1);
}
}
void rollback(long long q){
while(SEG.size()>q){
seg[SEG.back().first]=SEG.back().second;
SEG.pop_back();
}
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a;
for(i=1; i<=a; i++){
cin>>f[i];
}
for(i=0; i<=a+1; i++) dp[i]=N;
for(i=0; i<=4000005; i++){
seg[i]={-1,-1};
}
f[a+1]=N;
dp[a+1]=0;
de.push_back({a+1,0});
for(i=a; i>=1; i--){
while(de.size()>0){
c=de.back().first;d=de.back().second;
if(f[c]<=f[i]){
de.pop_back();
rollback(d);
}else{
break;
}
}
if(de.size()){
c=de.back().first;d=de.back().second;
e=SEG.size();
B=dp[c];K=f[i];
upd(1,a,1);
de.push_back({i,e});
}
z=N;
X=a-i+1;
read(1,a,1);
dp[i]=z;
}
/*for(i=1; i<=a; i++){
cout<<dp[i]<<" ";
}
cout<<"\n";*/
cout<<dp[1];
return 0;
}
Compilation message
Discharging.cpp: In function 'void rollback(long long int)':
Discharging.cpp:44:21: warning: comparison of integer expressions of different signedness: 'std::deque<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
44 | while(SEG.size()>q){
| ~~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
62892 KB |
Output is correct |
2 |
Correct |
27 ms |
62896 KB |
Output is correct |
3 |
Correct |
25 ms |
62844 KB |
Output is correct |
4 |
Correct |
28 ms |
62888 KB |
Output is correct |
5 |
Correct |
25 ms |
62920 KB |
Output is correct |
6 |
Correct |
27 ms |
62940 KB |
Output is correct |
7 |
Correct |
26 ms |
62932 KB |
Output is correct |
8 |
Correct |
26 ms |
62880 KB |
Output is correct |
9 |
Correct |
28 ms |
62916 KB |
Output is correct |
10 |
Correct |
29 ms |
62864 KB |
Output is correct |
11 |
Correct |
24 ms |
62940 KB |
Output is correct |
12 |
Correct |
27 ms |
62956 KB |
Output is correct |
13 |
Correct |
28 ms |
62940 KB |
Output is correct |
14 |
Correct |
31 ms |
62828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
62992 KB |
Output is correct |
2 |
Correct |
29 ms |
63008 KB |
Output is correct |
3 |
Correct |
29 ms |
62944 KB |
Output is correct |
4 |
Correct |
25 ms |
62976 KB |
Output is correct |
5 |
Correct |
25 ms |
63004 KB |
Output is correct |
6 |
Correct |
27 ms |
62952 KB |
Output is correct |
7 |
Correct |
29 ms |
62920 KB |
Output is correct |
8 |
Correct |
32 ms |
62928 KB |
Output is correct |
9 |
Correct |
26 ms |
62976 KB |
Output is correct |
10 |
Correct |
27 ms |
62952 KB |
Output is correct |
11 |
Correct |
27 ms |
62904 KB |
Output is correct |
12 |
Correct |
26 ms |
63008 KB |
Output is correct |
13 |
Correct |
31 ms |
62988 KB |
Output is correct |
14 |
Correct |
26 ms |
62992 KB |
Output is correct |
15 |
Correct |
27 ms |
62952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
62992 KB |
Output is correct |
2 |
Correct |
29 ms |
63008 KB |
Output is correct |
3 |
Correct |
29 ms |
62944 KB |
Output is correct |
4 |
Correct |
25 ms |
62976 KB |
Output is correct |
5 |
Correct |
25 ms |
63004 KB |
Output is correct |
6 |
Correct |
27 ms |
62952 KB |
Output is correct |
7 |
Correct |
29 ms |
62920 KB |
Output is correct |
8 |
Correct |
32 ms |
62928 KB |
Output is correct |
9 |
Correct |
26 ms |
62976 KB |
Output is correct |
10 |
Correct |
27 ms |
62952 KB |
Output is correct |
11 |
Correct |
27 ms |
62904 KB |
Output is correct |
12 |
Correct |
26 ms |
63008 KB |
Output is correct |
13 |
Correct |
31 ms |
62988 KB |
Output is correct |
14 |
Correct |
26 ms |
62992 KB |
Output is correct |
15 |
Correct |
27 ms |
62952 KB |
Output is correct |
16 |
Correct |
228 ms |
101360 KB |
Output is correct |
17 |
Correct |
288 ms |
102464 KB |
Output is correct |
18 |
Correct |
229 ms |
97944 KB |
Output is correct |
19 |
Correct |
271 ms |
102152 KB |
Output is correct |
20 |
Correct |
223 ms |
103712 KB |
Output is correct |
21 |
Correct |
236 ms |
102912 KB |
Output is correct |
22 |
Correct |
249 ms |
101416 KB |
Output is correct |
23 |
Correct |
224 ms |
106052 KB |
Output is correct |
24 |
Correct |
284 ms |
105420 KB |
Output is correct |
25 |
Correct |
229 ms |
104772 KB |
Output is correct |
26 |
Correct |
257 ms |
106480 KB |
Output is correct |
27 |
Correct |
227 ms |
100572 KB |
Output is correct |
28 |
Correct |
241 ms |
103148 KB |
Output is correct |
29 |
Correct |
276 ms |
106012 KB |
Output is correct |
30 |
Correct |
229 ms |
107092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
221 ms |
78584 KB |
Output is correct |
2 |
Correct |
254 ms |
78540 KB |
Output is correct |
3 |
Correct |
233 ms |
78676 KB |
Output is correct |
4 |
Correct |
242 ms |
78664 KB |
Output is correct |
5 |
Correct |
226 ms |
78556 KB |
Output is correct |
6 |
Correct |
220 ms |
78624 KB |
Output is correct |
7 |
Correct |
214 ms |
78692 KB |
Output is correct |
8 |
Correct |
225 ms |
78532 KB |
Output is correct |
9 |
Correct |
224 ms |
78588 KB |
Output is correct |
10 |
Correct |
227 ms |
78588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
62892 KB |
Output is correct |
2 |
Correct |
27 ms |
62896 KB |
Output is correct |
3 |
Correct |
25 ms |
62844 KB |
Output is correct |
4 |
Correct |
28 ms |
62888 KB |
Output is correct |
5 |
Correct |
25 ms |
62920 KB |
Output is correct |
6 |
Correct |
27 ms |
62940 KB |
Output is correct |
7 |
Correct |
26 ms |
62932 KB |
Output is correct |
8 |
Correct |
26 ms |
62880 KB |
Output is correct |
9 |
Correct |
28 ms |
62916 KB |
Output is correct |
10 |
Correct |
29 ms |
62864 KB |
Output is correct |
11 |
Correct |
24 ms |
62940 KB |
Output is correct |
12 |
Correct |
27 ms |
62956 KB |
Output is correct |
13 |
Correct |
28 ms |
62940 KB |
Output is correct |
14 |
Correct |
31 ms |
62828 KB |
Output is correct |
15 |
Correct |
30 ms |
62992 KB |
Output is correct |
16 |
Correct |
29 ms |
63008 KB |
Output is correct |
17 |
Correct |
29 ms |
62944 KB |
Output is correct |
18 |
Correct |
25 ms |
62976 KB |
Output is correct |
19 |
Correct |
25 ms |
63004 KB |
Output is correct |
20 |
Correct |
27 ms |
62952 KB |
Output is correct |
21 |
Correct |
29 ms |
62920 KB |
Output is correct |
22 |
Correct |
32 ms |
62928 KB |
Output is correct |
23 |
Correct |
26 ms |
62976 KB |
Output is correct |
24 |
Correct |
27 ms |
62952 KB |
Output is correct |
25 |
Correct |
27 ms |
62904 KB |
Output is correct |
26 |
Correct |
26 ms |
63008 KB |
Output is correct |
27 |
Correct |
31 ms |
62988 KB |
Output is correct |
28 |
Correct |
26 ms |
62992 KB |
Output is correct |
29 |
Correct |
27 ms |
62952 KB |
Output is correct |
30 |
Correct |
29 ms |
62932 KB |
Output is correct |
31 |
Correct |
26 ms |
62936 KB |
Output is correct |
32 |
Correct |
29 ms |
62968 KB |
Output is correct |
33 |
Correct |
25 ms |
62952 KB |
Output is correct |
34 |
Correct |
27 ms |
62880 KB |
Output is correct |
35 |
Correct |
27 ms |
62976 KB |
Output is correct |
36 |
Correct |
25 ms |
62860 KB |
Output is correct |
37 |
Correct |
30 ms |
62932 KB |
Output is correct |
38 |
Correct |
30 ms |
62924 KB |
Output is correct |
39 |
Correct |
26 ms |
62924 KB |
Output is correct |
40 |
Correct |
25 ms |
62956 KB |
Output is correct |
41 |
Correct |
25 ms |
62976 KB |
Output is correct |
42 |
Correct |
26 ms |
62872 KB |
Output is correct |
43 |
Correct |
24 ms |
63004 KB |
Output is correct |
44 |
Correct |
25 ms |
62932 KB |
Output is correct |
45 |
Correct |
26 ms |
62932 KB |
Output is correct |
46 |
Correct |
24 ms |
62916 KB |
Output is correct |
47 |
Correct |
27 ms |
62932 KB |
Output is correct |
48 |
Correct |
25 ms |
62936 KB |
Output is correct |
49 |
Correct |
26 ms |
62932 KB |
Output is correct |
50 |
Correct |
26 ms |
62944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
62892 KB |
Output is correct |
2 |
Correct |
27 ms |
62896 KB |
Output is correct |
3 |
Correct |
25 ms |
62844 KB |
Output is correct |
4 |
Correct |
28 ms |
62888 KB |
Output is correct |
5 |
Correct |
25 ms |
62920 KB |
Output is correct |
6 |
Correct |
27 ms |
62940 KB |
Output is correct |
7 |
Correct |
26 ms |
62932 KB |
Output is correct |
8 |
Correct |
26 ms |
62880 KB |
Output is correct |
9 |
Correct |
28 ms |
62916 KB |
Output is correct |
10 |
Correct |
29 ms |
62864 KB |
Output is correct |
11 |
Correct |
24 ms |
62940 KB |
Output is correct |
12 |
Correct |
27 ms |
62956 KB |
Output is correct |
13 |
Correct |
28 ms |
62940 KB |
Output is correct |
14 |
Correct |
31 ms |
62828 KB |
Output is correct |
15 |
Correct |
30 ms |
62992 KB |
Output is correct |
16 |
Correct |
29 ms |
63008 KB |
Output is correct |
17 |
Correct |
29 ms |
62944 KB |
Output is correct |
18 |
Correct |
25 ms |
62976 KB |
Output is correct |
19 |
Correct |
25 ms |
63004 KB |
Output is correct |
20 |
Correct |
27 ms |
62952 KB |
Output is correct |
21 |
Correct |
29 ms |
62920 KB |
Output is correct |
22 |
Correct |
32 ms |
62928 KB |
Output is correct |
23 |
Correct |
26 ms |
62976 KB |
Output is correct |
24 |
Correct |
27 ms |
62952 KB |
Output is correct |
25 |
Correct |
27 ms |
62904 KB |
Output is correct |
26 |
Correct |
26 ms |
63008 KB |
Output is correct |
27 |
Correct |
31 ms |
62988 KB |
Output is correct |
28 |
Correct |
26 ms |
62992 KB |
Output is correct |
29 |
Correct |
27 ms |
62952 KB |
Output is correct |
30 |
Correct |
228 ms |
101360 KB |
Output is correct |
31 |
Correct |
288 ms |
102464 KB |
Output is correct |
32 |
Correct |
229 ms |
97944 KB |
Output is correct |
33 |
Correct |
271 ms |
102152 KB |
Output is correct |
34 |
Correct |
223 ms |
103712 KB |
Output is correct |
35 |
Correct |
236 ms |
102912 KB |
Output is correct |
36 |
Correct |
249 ms |
101416 KB |
Output is correct |
37 |
Correct |
224 ms |
106052 KB |
Output is correct |
38 |
Correct |
284 ms |
105420 KB |
Output is correct |
39 |
Correct |
229 ms |
104772 KB |
Output is correct |
40 |
Correct |
257 ms |
106480 KB |
Output is correct |
41 |
Correct |
227 ms |
100572 KB |
Output is correct |
42 |
Correct |
241 ms |
103148 KB |
Output is correct |
43 |
Correct |
276 ms |
106012 KB |
Output is correct |
44 |
Correct |
229 ms |
107092 KB |
Output is correct |
45 |
Correct |
221 ms |
78584 KB |
Output is correct |
46 |
Correct |
254 ms |
78540 KB |
Output is correct |
47 |
Correct |
233 ms |
78676 KB |
Output is correct |
48 |
Correct |
242 ms |
78664 KB |
Output is correct |
49 |
Correct |
226 ms |
78556 KB |
Output is correct |
50 |
Correct |
220 ms |
78624 KB |
Output is correct |
51 |
Correct |
214 ms |
78692 KB |
Output is correct |
52 |
Correct |
225 ms |
78532 KB |
Output is correct |
53 |
Correct |
224 ms |
78588 KB |
Output is correct |
54 |
Correct |
227 ms |
78588 KB |
Output is correct |
55 |
Correct |
29 ms |
62932 KB |
Output is correct |
56 |
Correct |
26 ms |
62936 KB |
Output is correct |
57 |
Correct |
29 ms |
62968 KB |
Output is correct |
58 |
Correct |
25 ms |
62952 KB |
Output is correct |
59 |
Correct |
27 ms |
62880 KB |
Output is correct |
60 |
Correct |
27 ms |
62976 KB |
Output is correct |
61 |
Correct |
25 ms |
62860 KB |
Output is correct |
62 |
Correct |
30 ms |
62932 KB |
Output is correct |
63 |
Correct |
30 ms |
62924 KB |
Output is correct |
64 |
Correct |
26 ms |
62924 KB |
Output is correct |
65 |
Correct |
25 ms |
62956 KB |
Output is correct |
66 |
Correct |
25 ms |
62976 KB |
Output is correct |
67 |
Correct |
26 ms |
62872 KB |
Output is correct |
68 |
Correct |
24 ms |
63004 KB |
Output is correct |
69 |
Correct |
25 ms |
62932 KB |
Output is correct |
70 |
Correct |
26 ms |
62932 KB |
Output is correct |
71 |
Correct |
24 ms |
62916 KB |
Output is correct |
72 |
Correct |
27 ms |
62932 KB |
Output is correct |
73 |
Correct |
25 ms |
62936 KB |
Output is correct |
74 |
Correct |
26 ms |
62932 KB |
Output is correct |
75 |
Correct |
26 ms |
62944 KB |
Output is correct |
76 |
Correct |
232 ms |
78560 KB |
Output is correct |
77 |
Correct |
239 ms |
78568 KB |
Output is correct |
78 |
Correct |
232 ms |
78644 KB |
Output is correct |
79 |
Correct |
235 ms |
78588 KB |
Output is correct |
80 |
Correct |
273 ms |
78660 KB |
Output is correct |
81 |
Correct |
243 ms |
78548 KB |
Output is correct |
82 |
Correct |
228 ms |
78508 KB |
Output is correct |
83 |
Correct |
236 ms |
78564 KB |
Output is correct |
84 |
Correct |
231 ms |
78568 KB |
Output is correct |
85 |
Correct |
264 ms |
78524 KB |
Output is correct |
86 |
Correct |
244 ms |
78540 KB |
Output is correct |
87 |
Correct |
239 ms |
78592 KB |
Output is correct |
88 |
Correct |
231 ms |
78588 KB |
Output is correct |
89 |
Correct |
229 ms |
78472 KB |
Output is correct |
90 |
Correct |
234 ms |
78476 KB |
Output is correct |
91 |
Correct |
225 ms |
78588 KB |
Output is correct |
92 |
Correct |
227 ms |
103504 KB |
Output is correct |
93 |
Correct |
228 ms |
102996 KB |
Output is correct |
94 |
Correct |
228 ms |
78588 KB |
Output is correct |
95 |
Correct |
247 ms |
78532 KB |
Output is correct |
96 |
Correct |
246 ms |
78592 KB |
Output is correct |
97 |
Correct |
227 ms |
78580 KB |
Output is correct |
98 |
Correct |
242 ms |
78532 KB |
Output is correct |
99 |
Correct |
229 ms |
78504 KB |
Output is correct |
100 |
Correct |
235 ms |
78592 KB |
Output is correct |