#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define p(x,y) pair<ll,ll>(x,y)
#define BIT(i,x) ((x>>i)&1)
#define MASK(x) (1<<x)
#define ld long double
#define __builtin_popcount __builtin_popcountll
#define pll pair<ll,ll>
template<class T1,class T2>
bool maximize(T1 &x,const T2 &y)
{
if(x<y)
{
x=y;
return 1;
}
return 0;
}
template<class T1,class T2>
bool minimize(T1 &x,const T2 &y)
{
if(x>y)
{
x=y;
return 1;
}
return 0;
}
void online()
{
std::ios_base::sync_with_stdio(0);
cin.tie(0);
}
struct plane
{
struct line
{
ll m,c;
line(ll _m=0,ll _c=0)
{
this->m = _m;
this->c = _c;
}
ll intersect(line &other)
{
return (this->c - other.c + other.m - this->m +1) / (other.m - this->m);
}
ll vl(ll x)
{
return this->m * x + this->c;
}
};
deque<pair<line,ll>> dq;
void insert(ll m,ll c)
{
line nex(m,c);
if(this->dq.empty())
{
this->dq.push_back(pair<line,ll>(nex,0));
return;
}
while(!this->dq.empty())
{
line pre=this->dq.back().fi;
ll x=this->dq.back().se;
if(nex.m==pre.m)
{
if(nex.c>pre.c) return;
}
else if(nex.intersect(pre)>x) break;
this->dq.pop_back();
}
if(this->dq.empty())
{
this->dq.push_back(pair<line,ll>(nex,0));
return;
}
dq.push_back(pair<line,ll>(nex,nex.intersect(dq.back().fi)));
}
ll query(ll x)
{
ll m = this->dq.size();
while(m > 1 && dq[1].se <= x)
{
this->dq.pop_front();
m--;
}
return this->dq.front().fi.vl(x);
}
};
const ll N=1e6+10;
ll n,k;
ll ar[N],f[N],seg[N];
int main()
{
online();
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>ar[i];
}
seg[1]=1;
for(int i=2;i<=n;i++)
{
if(ar[i]>ar[seg[i-1]]) seg[i]=i;
else seg[i]=seg[i-1];
}
plane P;
for(int i=1;i<=n;i++)
{
P.insert(n-i+1,f[i-1]);
f[i]=P.query(ar[seg[i]]);
}
cout<<f[n];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
94 ms |
23748 KB |
Output is correct |
17 |
Correct |
99 ms |
23720 KB |
Output is correct |
18 |
Correct |
105 ms |
23716 KB |
Output is correct |
19 |
Correct |
105 ms |
23884 KB |
Output is correct |
20 |
Correct |
98 ms |
23696 KB |
Output is correct |
21 |
Correct |
98 ms |
23708 KB |
Output is correct |
22 |
Correct |
96 ms |
23780 KB |
Output is correct |
23 |
Correct |
105 ms |
23736 KB |
Output is correct |
24 |
Correct |
106 ms |
23740 KB |
Output is correct |
25 |
Correct |
106 ms |
23756 KB |
Output is correct |
26 |
Correct |
110 ms |
23808 KB |
Output is correct |
27 |
Correct |
94 ms |
23688 KB |
Output is correct |
28 |
Correct |
106 ms |
23724 KB |
Output is correct |
29 |
Correct |
111 ms |
23772 KB |
Output is correct |
30 |
Correct |
102 ms |
23728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
23732 KB |
Output is correct |
2 |
Correct |
123 ms |
23712 KB |
Output is correct |
3 |
Correct |
134 ms |
23868 KB |
Output is correct |
4 |
Correct |
124 ms |
23692 KB |
Output is correct |
5 |
Correct |
175 ms |
23756 KB |
Output is correct |
6 |
Correct |
118 ms |
23756 KB |
Output is correct |
7 |
Correct |
120 ms |
23884 KB |
Output is correct |
8 |
Correct |
117 ms |
23728 KB |
Output is correct |
9 |
Correct |
118 ms |
23684 KB |
Output is correct |
10 |
Correct |
119 ms |
23680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
0 ms |
284 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
94 ms |
23748 KB |
Output is correct |
31 |
Correct |
99 ms |
23720 KB |
Output is correct |
32 |
Correct |
105 ms |
23716 KB |
Output is correct |
33 |
Correct |
105 ms |
23884 KB |
Output is correct |
34 |
Correct |
98 ms |
23696 KB |
Output is correct |
35 |
Correct |
98 ms |
23708 KB |
Output is correct |
36 |
Correct |
96 ms |
23780 KB |
Output is correct |
37 |
Correct |
105 ms |
23736 KB |
Output is correct |
38 |
Correct |
106 ms |
23740 KB |
Output is correct |
39 |
Correct |
106 ms |
23756 KB |
Output is correct |
40 |
Correct |
110 ms |
23808 KB |
Output is correct |
41 |
Correct |
94 ms |
23688 KB |
Output is correct |
42 |
Correct |
106 ms |
23724 KB |
Output is correct |
43 |
Correct |
111 ms |
23772 KB |
Output is correct |
44 |
Correct |
102 ms |
23728 KB |
Output is correct |
45 |
Correct |
118 ms |
23732 KB |
Output is correct |
46 |
Correct |
123 ms |
23712 KB |
Output is correct |
47 |
Correct |
134 ms |
23868 KB |
Output is correct |
48 |
Correct |
124 ms |
23692 KB |
Output is correct |
49 |
Correct |
175 ms |
23756 KB |
Output is correct |
50 |
Correct |
118 ms |
23756 KB |
Output is correct |
51 |
Correct |
120 ms |
23884 KB |
Output is correct |
52 |
Correct |
117 ms |
23728 KB |
Output is correct |
53 |
Correct |
118 ms |
23684 KB |
Output is correct |
54 |
Correct |
119 ms |
23680 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
0 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
0 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
0 ms |
340 KB |
Output is correct |
64 |
Correct |
0 ms |
340 KB |
Output is correct |
65 |
Correct |
0 ms |
340 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
0 ms |
340 KB |
Output is correct |
70 |
Correct |
0 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
0 ms |
340 KB |
Output is correct |
74 |
Correct |
0 ms |
284 KB |
Output is correct |
75 |
Correct |
0 ms |
340 KB |
Output is correct |
76 |
Correct |
102 ms |
23660 KB |
Output is correct |
77 |
Correct |
101 ms |
23748 KB |
Output is correct |
78 |
Correct |
97 ms |
29132 KB |
Output is correct |
79 |
Correct |
98 ms |
29476 KB |
Output is correct |
80 |
Correct |
111 ms |
30028 KB |
Output is correct |
81 |
Correct |
97 ms |
29240 KB |
Output is correct |
82 |
Correct |
108 ms |
29200 KB |
Output is correct |
83 |
Correct |
103 ms |
29760 KB |
Output is correct |
84 |
Correct |
112 ms |
29644 KB |
Output is correct |
85 |
Correct |
109 ms |
29832 KB |
Output is correct |
86 |
Correct |
98 ms |
29260 KB |
Output is correct |
87 |
Correct |
99 ms |
29136 KB |
Output is correct |
88 |
Correct |
98 ms |
29512 KB |
Output is correct |
89 |
Correct |
100 ms |
29264 KB |
Output is correct |
90 |
Correct |
100 ms |
29148 KB |
Output is correct |
91 |
Correct |
94 ms |
28920 KB |
Output is correct |
92 |
Correct |
109 ms |
30260 KB |
Output is correct |
93 |
Correct |
105 ms |
29648 KB |
Output is correct |
94 |
Correct |
103 ms |
28680 KB |
Output is correct |
95 |
Correct |
109 ms |
30192 KB |
Output is correct |
96 |
Correct |
98 ms |
28960 KB |
Output is correct |
97 |
Correct |
104 ms |
29388 KB |
Output is correct |
98 |
Correct |
100 ms |
29548 KB |
Output is correct |
99 |
Correct |
98 ms |
29160 KB |
Output is correct |
100 |
Correct |
97 ms |
29228 KB |
Output is correct |