#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second
const int MAXN = 1e5 + 10;
const int LOG = 17;
const int INF = 1e9;
int n, a[MAXN], dp[LOG][LOG][MAXN], ans[MAXN];
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
memset(dp, 63, sizeof dp);
memset(ans, 63, sizeof ans);
debug(2*ans[0]+1);
cin >> n;
for (int i = 1; i <= n; i++){
cin >> a[i];
if (a[i] > LOG) continue;
dp[0][a[i]][i] = 0;
if (a[i]) dp[0][a[i]-1][i] = 1;
}
for (int i = 1; i < LOG; i++){
for (int j = 0; j < LOG - 1; j++){
for (int k = 1; k + (1 << i) - 1 <= n; k++){
dp[i][j][k] = min({dp[i][j][k], dp[i-1][j][k] + dp[i-1][j][k + (1 << (i-1))],
dp[i-1][j+1][k] + dp[i-1][j+1][k + (1 << (i-1))] + 1});
}
}
}
ans[n+1] = 0;
for (int i = n; i; i--){
for (int j = 0; i + (1 << j) - 1 <= n; j++){
ans[i] = min(ans[i], dp[j][0][i] + ans[i + (1 << j)]);
}
}
cout << (ans[1] >= INF? -1: ans[1]) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
113808 KB |
Output is correct |
2 |
Correct |
44 ms |
113728 KB |
Output is correct |
3 |
Correct |
44 ms |
113768 KB |
Output is correct |
4 |
Correct |
43 ms |
113744 KB |
Output is correct |
5 |
Correct |
42 ms |
113740 KB |
Output is correct |
6 |
Correct |
45 ms |
113772 KB |
Output is correct |
7 |
Correct |
43 ms |
113704 KB |
Output is correct |
8 |
Correct |
43 ms |
113768 KB |
Output is correct |
9 |
Correct |
44 ms |
113740 KB |
Output is correct |
10 |
Correct |
44 ms |
113800 KB |
Output is correct |
11 |
Correct |
43 ms |
113800 KB |
Output is correct |
12 |
Correct |
46 ms |
113740 KB |
Output is correct |
13 |
Correct |
50 ms |
113804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
113808 KB |
Output is correct |
2 |
Correct |
44 ms |
113728 KB |
Output is correct |
3 |
Correct |
44 ms |
113768 KB |
Output is correct |
4 |
Correct |
43 ms |
113744 KB |
Output is correct |
5 |
Correct |
42 ms |
113740 KB |
Output is correct |
6 |
Correct |
45 ms |
113772 KB |
Output is correct |
7 |
Correct |
43 ms |
113704 KB |
Output is correct |
8 |
Correct |
43 ms |
113768 KB |
Output is correct |
9 |
Correct |
44 ms |
113740 KB |
Output is correct |
10 |
Correct |
44 ms |
113800 KB |
Output is correct |
11 |
Correct |
43 ms |
113800 KB |
Output is correct |
12 |
Correct |
46 ms |
113740 KB |
Output is correct |
13 |
Correct |
50 ms |
113804 KB |
Output is correct |
14 |
Correct |
43 ms |
113788 KB |
Output is correct |
15 |
Correct |
43 ms |
113716 KB |
Output is correct |
16 |
Correct |
43 ms |
113788 KB |
Output is correct |
17 |
Correct |
43 ms |
113760 KB |
Output is correct |
18 |
Correct |
42 ms |
113796 KB |
Output is correct |
19 |
Correct |
45 ms |
113720 KB |
Output is correct |
20 |
Correct |
53 ms |
113848 KB |
Output is correct |
21 |
Correct |
48 ms |
113996 KB |
Output is correct |
22 |
Correct |
97 ms |
114088 KB |
Output is correct |
23 |
Correct |
90 ms |
114576 KB |
Output is correct |
24 |
Correct |
90 ms |
114404 KB |
Output is correct |
25 |
Correct |
43 ms |
113720 KB |
Output is correct |
26 |
Correct |
43 ms |
113736 KB |
Output is correct |
27 |
Correct |
44 ms |
113760 KB |
Output is correct |
28 |
Correct |
43 ms |
113800 KB |
Output is correct |
29 |
Correct |
49 ms |
113820 KB |
Output is correct |
30 |
Correct |
48 ms |
113860 KB |
Output is correct |
31 |
Correct |
49 ms |
113848 KB |
Output is correct |
32 |
Correct |
48 ms |
113808 KB |
Output is correct |
33 |
Correct |
92 ms |
114356 KB |
Output is correct |
34 |
Correct |
91 ms |
114512 KB |
Output is correct |
35 |
Correct |
89 ms |
114492 KB |
Output is correct |
36 |
Correct |
90 ms |
114380 KB |
Output is correct |
37 |
Correct |
89 ms |
114476 KB |
Output is correct |
38 |
Correct |
90 ms |
114508 KB |
Output is correct |
39 |
Correct |
90 ms |
114356 KB |
Output is correct |
40 |
Correct |
90 ms |
114472 KB |
Output is correct |
41 |
Correct |
91 ms |
114348 KB |
Output is correct |
42 |
Correct |
90 ms |
114452 KB |
Output is correct |
43 |
Correct |
95 ms |
114304 KB |
Output is correct |
44 |
Correct |
93 ms |
114416 KB |
Output is correct |
45 |
Correct |
89 ms |
114320 KB |
Output is correct |
46 |
Correct |
91 ms |
114308 KB |
Output is correct |
47 |
Correct |
72 ms |
114224 KB |
Output is correct |
48 |
Correct |
85 ms |
114284 KB |
Output is correct |
49 |
Correct |
82 ms |
114252 KB |
Output is correct |
50 |
Correct |
92 ms |
114428 KB |
Output is correct |
51 |
Correct |
47 ms |
113892 KB |
Output is correct |
52 |
Correct |
87 ms |
114408 KB |
Output is correct |