#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 |
111 ms |
250704 KB |
Output is correct |
2 |
Correct |
118 ms |
250852 KB |
Output is correct |
3 |
Correct |
117 ms |
250704 KB |
Output is correct |
4 |
Correct |
113 ms |
250708 KB |
Output is correct |
5 |
Correct |
112 ms |
250900 KB |
Output is correct |
6 |
Correct |
115 ms |
250708 KB |
Output is correct |
7 |
Correct |
121 ms |
250708 KB |
Output is correct |
8 |
Correct |
132 ms |
250708 KB |
Output is correct |
9 |
Correct |
116 ms |
250848 KB |
Output is correct |
10 |
Correct |
113 ms |
250704 KB |
Output is correct |
11 |
Correct |
114 ms |
250704 KB |
Output is correct |
12 |
Correct |
118 ms |
250884 KB |
Output is correct |
13 |
Correct |
115 ms |
250704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
250704 KB |
Output is correct |
2 |
Correct |
118 ms |
250852 KB |
Output is correct |
3 |
Correct |
117 ms |
250704 KB |
Output is correct |
4 |
Correct |
113 ms |
250708 KB |
Output is correct |
5 |
Correct |
112 ms |
250900 KB |
Output is correct |
6 |
Correct |
115 ms |
250708 KB |
Output is correct |
7 |
Correct |
121 ms |
250708 KB |
Output is correct |
8 |
Correct |
132 ms |
250708 KB |
Output is correct |
9 |
Correct |
116 ms |
250848 KB |
Output is correct |
10 |
Correct |
113 ms |
250704 KB |
Output is correct |
11 |
Correct |
114 ms |
250704 KB |
Output is correct |
12 |
Correct |
118 ms |
250884 KB |
Output is correct |
13 |
Correct |
115 ms |
250704 KB |
Output is correct |
14 |
Correct |
126 ms |
250708 KB |
Output is correct |
15 |
Correct |
120 ms |
251020 KB |
Output is correct |
16 |
Correct |
118 ms |
250708 KB |
Output is correct |
17 |
Correct |
117 ms |
250704 KB |
Output is correct |
18 |
Correct |
115 ms |
250704 KB |
Output is correct |
19 |
Correct |
120 ms |
250708 KB |
Output is correct |
20 |
Correct |
120 ms |
251040 KB |
Output is correct |
21 |
Correct |
123 ms |
250920 KB |
Output is correct |
22 |
Correct |
230 ms |
252244 KB |
Output is correct |
23 |
Correct |
124 ms |
251176 KB |
Output is correct |
24 |
Correct |
228 ms |
252240 KB |
Output is correct |
25 |
Correct |
115 ms |
250928 KB |
Output is correct |
26 |
Correct |
119 ms |
250848 KB |
Output is correct |
27 |
Correct |
127 ms |
250964 KB |
Output is correct |
28 |
Correct |
112 ms |
250708 KB |
Output is correct |
29 |
Correct |
130 ms |
251052 KB |
Output is correct |
30 |
Correct |
124 ms |
250964 KB |
Output is correct |
31 |
Correct |
139 ms |
251068 KB |
Output is correct |
32 |
Correct |
122 ms |
250960 KB |
Output is correct |
33 |
Correct |
255 ms |
252248 KB |
Output is correct |
34 |
Correct |
238 ms |
252364 KB |
Output is correct |
35 |
Correct |
231 ms |
252240 KB |
Output is correct |
36 |
Correct |
239 ms |
252048 KB |
Output is correct |
37 |
Correct |
259 ms |
252244 KB |
Output is correct |
38 |
Correct |
238 ms |
252244 KB |
Output is correct |
39 |
Correct |
260 ms |
252240 KB |
Output is correct |
40 |
Correct |
245 ms |
252304 KB |
Output is correct |
41 |
Correct |
250 ms |
252120 KB |
Output is correct |
42 |
Correct |
261 ms |
252244 KB |
Output is correct |
43 |
Correct |
242 ms |
252244 KB |
Output is correct |
44 |
Correct |
252 ms |
252240 KB |
Output is correct |
45 |
Correct |
251 ms |
252064 KB |
Output is correct |
46 |
Correct |
248 ms |
252244 KB |
Output is correct |
47 |
Correct |
218 ms |
251800 KB |
Output is correct |
48 |
Correct |
300 ms |
251984 KB |
Output is correct |
49 |
Correct |
228 ms |
251988 KB |
Output is correct |
50 |
Correct |
250 ms |
252240 KB |
Output is correct |
51 |
Correct |
135 ms |
250960 KB |
Output is correct |
52 |
Correct |
252 ms |
252244 KB |
Output is correct |