#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
const int LG = 20;
struct node {
long long mn=0;
vector<int> dec;
node() {
dec=vector<int>(LG, -1);
}
};
int n, a[N];
node up[N][LG];
node merge(const node &A, const node &B) {
node res;
res.mn = min(A.mn, B.mn);
for(int i = 0;i<LG;i++){
if(A.dec[i] == -1 or B.dec[i] == -1)continue;
res.dec[i] = A.dec[i]+B.dec[i];
}
if(res.mn > 0) {
for(int i = 0;i+1<LG;i++){
if(A.dec[i+1] == -1 or B.dec[i+1]==-1)continue;
int L = A.dec[i+1];
int R = B.dec[i+1];
if(L == -1 or R == -1)continue;
if(res.dec[i] == -1)res.dec[i] = L+R+1;
else res.dec[i] = min(res.dec[i], L+R+1);
}
}
return res;
}
int main () {
cin >> n;
for(int i = 0;i<n;i++) {
cin >> a[i];
node tmp;
tmp.mn = a[i];
if(a[i] >= LG) {
cout << "-1\n";
return 0;
}
tmp.dec[a[i]] = 0;
if(a[i] >0)tmp.dec[a[i]-1]=1;
up[i][0] = tmp;
}
for(int i = 0;i<n;i++) {
for(int j = 1;j<LG;j++) {
int prv = i-(1<<(j-1));
if(prv<0)break;
up[i][j] = merge(up[i][j-1], up[prv][j-1]);
}
}
long long dp[n+1];
dp[0]=0;
for(int i = 1;i<=n;i++) {
int cnt=0;
dp[i] = 1e18;
for(int j = 1;j<=i;j*=2) {
if(up[i-1][cnt].dec[0]!=-1)
dp[i] = min(dp[i], dp[i-j]+up[i-1][cnt].dec[0]);
cnt++;
}
}
if(dp[n] == 1e18) {
cout << "-1\n";
return 0;
}
else cout << dp[n] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
250916 KB |
Output is correct |
2 |
Correct |
163 ms |
250888 KB |
Output is correct |
3 |
Correct |
135 ms |
250708 KB |
Output is correct |
4 |
Correct |
175 ms |
250944 KB |
Output is correct |
5 |
Correct |
135 ms |
250708 KB |
Output is correct |
6 |
Correct |
142 ms |
250744 KB |
Output is correct |
7 |
Correct |
132 ms |
250876 KB |
Output is correct |
8 |
Correct |
135 ms |
250744 KB |
Output is correct |
9 |
Correct |
139 ms |
250708 KB |
Output is correct |
10 |
Correct |
129 ms |
250708 KB |
Output is correct |
11 |
Correct |
137 ms |
250716 KB |
Output is correct |
12 |
Correct |
138 ms |
250704 KB |
Output is correct |
13 |
Correct |
172 ms |
250804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
250916 KB |
Output is correct |
2 |
Correct |
163 ms |
250888 KB |
Output is correct |
3 |
Correct |
135 ms |
250708 KB |
Output is correct |
4 |
Correct |
175 ms |
250944 KB |
Output is correct |
5 |
Correct |
135 ms |
250708 KB |
Output is correct |
6 |
Correct |
142 ms |
250744 KB |
Output is correct |
7 |
Correct |
132 ms |
250876 KB |
Output is correct |
8 |
Correct |
135 ms |
250744 KB |
Output is correct |
9 |
Correct |
139 ms |
250708 KB |
Output is correct |
10 |
Correct |
129 ms |
250708 KB |
Output is correct |
11 |
Correct |
137 ms |
250716 KB |
Output is correct |
12 |
Correct |
138 ms |
250704 KB |
Output is correct |
13 |
Correct |
172 ms |
250804 KB |
Output is correct |
14 |
Correct |
128 ms |
250916 KB |
Output is correct |
15 |
Correct |
136 ms |
250704 KB |
Output is correct |
16 |
Correct |
133 ms |
250708 KB |
Output is correct |
17 |
Correct |
116 ms |
250704 KB |
Output is correct |
18 |
Correct |
123 ms |
250776 KB |
Output is correct |
19 |
Correct |
135 ms |
250772 KB |
Output is correct |
20 |
Correct |
138 ms |
251048 KB |
Output is correct |
21 |
Correct |
133 ms |
251060 KB |
Output is correct |
22 |
Correct |
269 ms |
252328 KB |
Output is correct |
23 |
Correct |
129 ms |
250872 KB |
Output is correct |
24 |
Correct |
249 ms |
252268 KB |
Output is correct |
25 |
Correct |
123 ms |
250964 KB |
Output is correct |
26 |
Correct |
121 ms |
250708 KB |
Output is correct |
27 |
Correct |
120 ms |
250792 KB |
Output is correct |
28 |
Correct |
119 ms |
250708 KB |
Output is correct |
29 |
Correct |
129 ms |
250892 KB |
Output is correct |
30 |
Correct |
131 ms |
251000 KB |
Output is correct |
31 |
Correct |
132 ms |
250960 KB |
Output is correct |
32 |
Correct |
130 ms |
250864 KB |
Output is correct |
33 |
Correct |
251 ms |
252244 KB |
Output is correct |
34 |
Correct |
267 ms |
252344 KB |
Output is correct |
35 |
Correct |
255 ms |
252240 KB |
Output is correct |
36 |
Correct |
270 ms |
252500 KB |
Output is correct |
37 |
Correct |
257 ms |
252240 KB |
Output is correct |
38 |
Correct |
252 ms |
252244 KB |
Output is correct |
39 |
Correct |
261 ms |
252128 KB |
Output is correct |
40 |
Correct |
259 ms |
252268 KB |
Output is correct |
41 |
Correct |
263 ms |
252276 KB |
Output is correct |
42 |
Correct |
249 ms |
252192 KB |
Output is correct |
43 |
Correct |
244 ms |
252204 KB |
Output is correct |
44 |
Correct |
260 ms |
252244 KB |
Output is correct |
45 |
Correct |
247 ms |
252240 KB |
Output is correct |
46 |
Correct |
273 ms |
252244 KB |
Output is correct |
47 |
Correct |
207 ms |
251756 KB |
Output is correct |
48 |
Correct |
250 ms |
251988 KB |
Output is correct |
49 |
Correct |
243 ms |
251988 KB |
Output is correct |
50 |
Correct |
255 ms |
252296 KB |
Output is correct |
51 |
Correct |
133 ms |
250964 KB |
Output is correct |
52 |
Correct |
301 ms |
252192 KB |
Output is correct |