//Be Name Khoda
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
const ll mxn = 1e5 + 16, lg = 18, INF = 1e16 + 16;
ll n, q, w, e;
ll dp[mxn], mn[mxn][lg], pd[mxn][lg][lg];
vector<ll> g;
bool bomb;
inline void input() {
for(int i = 0; i < mxn; i++) {
dp[i] = INF;
for(int j = 0; j < lg; j++) {
mn[i][j] = INF;
fill(pd[i][j], pd[i][j] + lg, INF);
}
}
cin >> n;
for(int i = 0; i < n; i++) {
cin >> q;
g.push_back(q);
if(q >= lg) bomb = 1;
mn[i][0] = q;
}
}
inline void solve() {
if(bomb) {
cout << "-1\n";
return;
}
for(int j = 1; j < lg; j++) {
for(int i = 0; i < n; i++) {
if(i + (1 << (j - 1)) >= n) break;
mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
}
}
for(int i = 0; i < n; i++) {
for(int k = 0; k < lg; k++) {
if(k == g[i] - 1) {
pd[i][0][k] = 1;
} if(k == g[i]) {
pd[i][0][k] = 0;
}
}
}
for(int j = 1; j < lg; j++) {
for(int i = 0; i < n; i++) {
if(i + (1 << j) > n) break;
for(int k = 0; k < lg; k++) {
if(mn[i][j] < k) break;
pd[i][j][k] = min(INF, pd[i][j - 1][k] + pd[i + (1 << (j - 1))][j - 1][k]);
if(k + 1 < lg && mn[i][j] >= k + 1) {
pd[i][j][k] = min(pd[i][j][k], pd[i][j - 1][k + 1] + pd[i + (1 << (j - 1))][j - 1][k + 1] + 1);
}
}
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; (1 << j) <= i + 1; j++) {
if((1 << j) == i + 1) {
dp[i] = min(dp[i], pd[0][j][0]);
continue;
}
dp[i] = min(dp[i], pd[i - (1 << j) + 1][j][0] + dp[i - (1 << j)]);
}
}
if(dp[n - 1] >= INF) {
cout << "-1\n";
return;
} cout << dp[n - 1] << endl;
return;
}
int main() {
ios::sync_with_stdio(false); cin.tie(NULL);
input(), solve();
return 0;
}
/*
8
2 2 2 0 1 1 2 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
268808 KB |
Output is correct |
2 |
Correct |
111 ms |
268840 KB |
Output is correct |
3 |
Correct |
107 ms |
268748 KB |
Output is correct |
4 |
Correct |
115 ms |
268792 KB |
Output is correct |
5 |
Correct |
115 ms |
268768 KB |
Output is correct |
6 |
Correct |
114 ms |
268860 KB |
Output is correct |
7 |
Correct |
126 ms |
268852 KB |
Output is correct |
8 |
Correct |
110 ms |
268748 KB |
Output is correct |
9 |
Correct |
122 ms |
268772 KB |
Output is correct |
10 |
Correct |
112 ms |
268884 KB |
Output is correct |
11 |
Correct |
112 ms |
268764 KB |
Output is correct |
12 |
Correct |
110 ms |
268756 KB |
Output is correct |
13 |
Correct |
107 ms |
268748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
268808 KB |
Output is correct |
2 |
Correct |
111 ms |
268840 KB |
Output is correct |
3 |
Correct |
107 ms |
268748 KB |
Output is correct |
4 |
Correct |
115 ms |
268792 KB |
Output is correct |
5 |
Correct |
115 ms |
268768 KB |
Output is correct |
6 |
Correct |
114 ms |
268860 KB |
Output is correct |
7 |
Correct |
126 ms |
268852 KB |
Output is correct |
8 |
Correct |
110 ms |
268748 KB |
Output is correct |
9 |
Correct |
122 ms |
268772 KB |
Output is correct |
10 |
Correct |
112 ms |
268884 KB |
Output is correct |
11 |
Correct |
112 ms |
268764 KB |
Output is correct |
12 |
Correct |
110 ms |
268756 KB |
Output is correct |
13 |
Correct |
107 ms |
268748 KB |
Output is correct |
14 |
Correct |
114 ms |
268788 KB |
Output is correct |
15 |
Correct |
114 ms |
268748 KB |
Output is correct |
16 |
Correct |
118 ms |
268732 KB |
Output is correct |
17 |
Correct |
116 ms |
268804 KB |
Output is correct |
18 |
Correct |
126 ms |
268808 KB |
Output is correct |
19 |
Correct |
112 ms |
268812 KB |
Output is correct |
20 |
Correct |
140 ms |
269012 KB |
Output is correct |
21 |
Correct |
125 ms |
269044 KB |
Output is correct |
22 |
Correct |
268 ms |
270232 KB |
Output is correct |
23 |
Correct |
117 ms |
270400 KB |
Output is correct |
24 |
Correct |
340 ms |
270152 KB |
Output is correct |
25 |
Correct |
113 ms |
268736 KB |
Output is correct |
26 |
Correct |
116 ms |
268804 KB |
Output is correct |
27 |
Correct |
110 ms |
268752 KB |
Output is correct |
28 |
Correct |
121 ms |
268760 KB |
Output is correct |
29 |
Correct |
142 ms |
269064 KB |
Output is correct |
30 |
Correct |
124 ms |
269020 KB |
Output is correct |
31 |
Correct |
119 ms |
269012 KB |
Output is correct |
32 |
Correct |
119 ms |
269064 KB |
Output is correct |
33 |
Correct |
257 ms |
270112 KB |
Output is correct |
34 |
Correct |
354 ms |
270252 KB |
Output is correct |
35 |
Correct |
333 ms |
270200 KB |
Output is correct |
36 |
Correct |
255 ms |
270148 KB |
Output is correct |
37 |
Correct |
506 ms |
270348 KB |
Output is correct |
38 |
Correct |
261 ms |
270220 KB |
Output is correct |
39 |
Correct |
239 ms |
270332 KB |
Output is correct |
40 |
Correct |
309 ms |
270156 KB |
Output is correct |
41 |
Correct |
252 ms |
270124 KB |
Output is correct |
42 |
Correct |
340 ms |
270284 KB |
Output is correct |
43 |
Correct |
257 ms |
270208 KB |
Output is correct |
44 |
Correct |
242 ms |
270156 KB |
Output is correct |
45 |
Correct |
248 ms |
270172 KB |
Output is correct |
46 |
Correct |
262 ms |
270200 KB |
Output is correct |
47 |
Correct |
212 ms |
270120 KB |
Output is correct |
48 |
Correct |
195 ms |
270064 KB |
Output is correct |
49 |
Correct |
215 ms |
270072 KB |
Output is correct |
50 |
Correct |
305 ms |
270212 KB |
Output is correct |
51 |
Correct |
118 ms |
269036 KB |
Output is correct |
52 |
Correct |
238 ms |
270096 KB |
Output is correct |