//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
//#define endl '\n'
llo n;
llo it[1000001];
vector<pair<llo,llo>> ss[4*1000001];
llo ind[1000001*4];
vector<pair<llo,llo>> pre[1000001];
llo dp[1000001];
void build(llo no,llo l,llo r){
if(l==r){
pre[r].pb({l,no});
}
else{
pre[r].pb({l,no});
llo mid=(l+r)/2;
build(no*2+1,l,mid);
build(no*2+2,mid+1,r);
}
}
void re(llo no,llo l,llo r){
// cout<<no<<":"<<l<<":"<<r<<endl;
for(llo i=l;i<=r;i++){
//cout<<no<<":"<<i<<endl;
pair<llo,llo> xx={(n-i-1),dp[i]};
while(ss[no].size()>=2){
llo aa=ss[no].back().b-xx.b;
llo bb=ss[no].back().a-xx.a;
llo cc=ss[no][ss[no].size()-2].b-ss[no].back().b;
llo dd=ss[no][ss[no].size()-2].a-ss[no].back().a;
aa=-aa;
cc=-cc;
long double kk=aa*dd;
long double mm=bb*cc;
if(kk<mm){
ss[no].pop_back();
continue;
}
else{
break;
}
}
ss[no].pb(xx);
}
}
llo eval(pair<llo,llo> bb,llo cc){
return bb.a*cc+bb.b;
}
llo qq=0;
llo query(llo no,llo l,llo r,llo aa,llo bb){
if(r<aa or l>bb){
return (llo)1e18;
}
if(aa<=l and r<=bb){
// cout<<no<<":"<<endl;
while(ind[no]<(llo)(ss[no].size())-1){
if(eval(ss[no][ind[no]],qq)>=eval(ss[no][ind[no]+1],qq)){
ind[no]++;
}
else{
break;
}
}
return eval(ss[no][ind[no]],qq);
}
else{
llo mid=(l+r)/2;
return min(query(no*2+1,l,mid,aa,bb),query(no*2+2,mid+1,r,aa,bb));
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(llo i=0;i<n;i++){
cin>>it[i];
}
vector<pair<llo,llo>> ss;
build(0,0,n-1);
for(llo i=0;i<n;i++){
while(ss.size()){
if(ss.back().a<=it[i]){
ss.pop_back();
}
else{
break;
}
}
// cout<<i<<endl;
llo la=-1;
if(ss.size()){
la=ss.back().b;
}
ss.pb({it[i],i});
dp[i]=ss[0].a*n;
if(la!=-1){
dp[i]=min(dp[i],dp[la]);
}
qq=it[i];
if(i>0){
dp[i]=min(dp[i],query(0,0,n-1,max(la,(llo)0),i-1));
}
//cout<<dp[i]<<",";
if(i==n-1){
break;
}
for(auto j:pre[i]){
re(j.b,j.a,i);
}
}
// cout<<endl;
cout<<dp[n-1]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
117732 KB |
Output is correct |
2 |
Correct |
76 ms |
117736 KB |
Output is correct |
3 |
Correct |
78 ms |
117732 KB |
Output is correct |
4 |
Correct |
77 ms |
117736 KB |
Output is correct |
5 |
Correct |
72 ms |
117740 KB |
Output is correct |
6 |
Correct |
81 ms |
117732 KB |
Output is correct |
7 |
Correct |
73 ms |
117732 KB |
Output is correct |
8 |
Correct |
76 ms |
117732 KB |
Output is correct |
9 |
Correct |
72 ms |
117732 KB |
Output is correct |
10 |
Correct |
71 ms |
117732 KB |
Output is correct |
11 |
Correct |
73 ms |
117740 KB |
Output is correct |
12 |
Correct |
72 ms |
117732 KB |
Output is correct |
13 |
Correct |
72 ms |
117732 KB |
Output is correct |
14 |
Correct |
72 ms |
117940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
118116 KB |
Output is correct |
2 |
Correct |
73 ms |
117988 KB |
Output is correct |
3 |
Correct |
75 ms |
118116 KB |
Output is correct |
4 |
Correct |
76 ms |
118116 KB |
Output is correct |
5 |
Correct |
75 ms |
118116 KB |
Output is correct |
6 |
Correct |
76 ms |
118116 KB |
Output is correct |
7 |
Correct |
79 ms |
118116 KB |
Output is correct |
8 |
Correct |
76 ms |
117988 KB |
Output is correct |
9 |
Correct |
80 ms |
117988 KB |
Output is correct |
10 |
Correct |
76 ms |
117988 KB |
Output is correct |
11 |
Correct |
74 ms |
118116 KB |
Output is correct |
12 |
Correct |
74 ms |
118116 KB |
Output is correct |
13 |
Correct |
84 ms |
117988 KB |
Output is correct |
14 |
Correct |
75 ms |
118120 KB |
Output is correct |
15 |
Correct |
76 ms |
118116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
118116 KB |
Output is correct |
2 |
Correct |
73 ms |
117988 KB |
Output is correct |
3 |
Correct |
75 ms |
118116 KB |
Output is correct |
4 |
Correct |
76 ms |
118116 KB |
Output is correct |
5 |
Correct |
75 ms |
118116 KB |
Output is correct |
6 |
Correct |
76 ms |
118116 KB |
Output is correct |
7 |
Correct |
79 ms |
118116 KB |
Output is correct |
8 |
Correct |
76 ms |
117988 KB |
Output is correct |
9 |
Correct |
80 ms |
117988 KB |
Output is correct |
10 |
Correct |
76 ms |
117988 KB |
Output is correct |
11 |
Correct |
74 ms |
118116 KB |
Output is correct |
12 |
Correct |
74 ms |
118116 KB |
Output is correct |
13 |
Correct |
84 ms |
117988 KB |
Output is correct |
14 |
Correct |
75 ms |
118120 KB |
Output is correct |
15 |
Correct |
76 ms |
118116 KB |
Output is correct |
16 |
Execution timed out |
1097 ms |
374904 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
542880 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
117732 KB |
Output is correct |
2 |
Correct |
76 ms |
117736 KB |
Output is correct |
3 |
Correct |
78 ms |
117732 KB |
Output is correct |
4 |
Correct |
77 ms |
117736 KB |
Output is correct |
5 |
Correct |
72 ms |
117740 KB |
Output is correct |
6 |
Correct |
81 ms |
117732 KB |
Output is correct |
7 |
Correct |
73 ms |
117732 KB |
Output is correct |
8 |
Correct |
76 ms |
117732 KB |
Output is correct |
9 |
Correct |
72 ms |
117732 KB |
Output is correct |
10 |
Correct |
71 ms |
117732 KB |
Output is correct |
11 |
Correct |
73 ms |
117740 KB |
Output is correct |
12 |
Correct |
72 ms |
117732 KB |
Output is correct |
13 |
Correct |
72 ms |
117732 KB |
Output is correct |
14 |
Correct |
72 ms |
117940 KB |
Output is correct |
15 |
Correct |
77 ms |
118116 KB |
Output is correct |
16 |
Correct |
73 ms |
117988 KB |
Output is correct |
17 |
Correct |
75 ms |
118116 KB |
Output is correct |
18 |
Correct |
76 ms |
118116 KB |
Output is correct |
19 |
Correct |
75 ms |
118116 KB |
Output is correct |
20 |
Correct |
76 ms |
118116 KB |
Output is correct |
21 |
Correct |
79 ms |
118116 KB |
Output is correct |
22 |
Correct |
76 ms |
117988 KB |
Output is correct |
23 |
Correct |
80 ms |
117988 KB |
Output is correct |
24 |
Correct |
76 ms |
117988 KB |
Output is correct |
25 |
Correct |
74 ms |
118116 KB |
Output is correct |
26 |
Correct |
74 ms |
118116 KB |
Output is correct |
27 |
Correct |
84 ms |
117988 KB |
Output is correct |
28 |
Correct |
75 ms |
118120 KB |
Output is correct |
29 |
Correct |
76 ms |
118116 KB |
Output is correct |
30 |
Correct |
78 ms |
118116 KB |
Output is correct |
31 |
Correct |
79 ms |
118116 KB |
Output is correct |
32 |
Correct |
85 ms |
118116 KB |
Output is correct |
33 |
Correct |
79 ms |
118116 KB |
Output is correct |
34 |
Correct |
79 ms |
118116 KB |
Output is correct |
35 |
Correct |
86 ms |
118116 KB |
Output is correct |
36 |
Correct |
78 ms |
118116 KB |
Output is correct |
37 |
Correct |
80 ms |
118116 KB |
Output is correct |
38 |
Correct |
78 ms |
118112 KB |
Output is correct |
39 |
Correct |
77 ms |
118116 KB |
Output is correct |
40 |
Correct |
79 ms |
118116 KB |
Output is correct |
41 |
Correct |
77 ms |
118112 KB |
Output is correct |
42 |
Correct |
78 ms |
118372 KB |
Output is correct |
43 |
Correct |
79 ms |
118116 KB |
Output is correct |
44 |
Correct |
78 ms |
118116 KB |
Output is correct |
45 |
Correct |
78 ms |
118116 KB |
Output is correct |
46 |
Correct |
77 ms |
118116 KB |
Output is correct |
47 |
Correct |
78 ms |
118116 KB |
Output is correct |
48 |
Correct |
77 ms |
118116 KB |
Output is correct |
49 |
Correct |
78 ms |
118116 KB |
Output is correct |
50 |
Correct |
78 ms |
118116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
117732 KB |
Output is correct |
2 |
Correct |
76 ms |
117736 KB |
Output is correct |
3 |
Correct |
78 ms |
117732 KB |
Output is correct |
4 |
Correct |
77 ms |
117736 KB |
Output is correct |
5 |
Correct |
72 ms |
117740 KB |
Output is correct |
6 |
Correct |
81 ms |
117732 KB |
Output is correct |
7 |
Correct |
73 ms |
117732 KB |
Output is correct |
8 |
Correct |
76 ms |
117732 KB |
Output is correct |
9 |
Correct |
72 ms |
117732 KB |
Output is correct |
10 |
Correct |
71 ms |
117732 KB |
Output is correct |
11 |
Correct |
73 ms |
117740 KB |
Output is correct |
12 |
Correct |
72 ms |
117732 KB |
Output is correct |
13 |
Correct |
72 ms |
117732 KB |
Output is correct |
14 |
Correct |
72 ms |
117940 KB |
Output is correct |
15 |
Correct |
77 ms |
118116 KB |
Output is correct |
16 |
Correct |
73 ms |
117988 KB |
Output is correct |
17 |
Correct |
75 ms |
118116 KB |
Output is correct |
18 |
Correct |
76 ms |
118116 KB |
Output is correct |
19 |
Correct |
75 ms |
118116 KB |
Output is correct |
20 |
Correct |
76 ms |
118116 KB |
Output is correct |
21 |
Correct |
79 ms |
118116 KB |
Output is correct |
22 |
Correct |
76 ms |
117988 KB |
Output is correct |
23 |
Correct |
80 ms |
117988 KB |
Output is correct |
24 |
Correct |
76 ms |
117988 KB |
Output is correct |
25 |
Correct |
74 ms |
118116 KB |
Output is correct |
26 |
Correct |
74 ms |
118116 KB |
Output is correct |
27 |
Correct |
84 ms |
117988 KB |
Output is correct |
28 |
Correct |
75 ms |
118120 KB |
Output is correct |
29 |
Correct |
76 ms |
118116 KB |
Output is correct |
30 |
Execution timed out |
1097 ms |
374904 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |