// In the Name of Allah
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,avx,avx2,sse4.2,popcnt,tune=native")
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
#define pp pop_back
#define all(x) x.begin(), x.end()
const int N = 1e5 + 3, M = 17, inf = 1e9 + 7;
int a[N], n, dp[N], f[N][M][M];
void ip() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
}
void solve() {
memset(f, 63, sizeof f);
for (int i = 1; i <= n; ++i) {
if (a[i] >= M) {
cout << -1;
return;
}
f[i][0][a[i]] = 0;
if (a[i])
f[i][0][a[i] - 1] = 1;
for (int j = 1; i - (1 << j) >= 0; ++j) {
int l = i - (1 << (j - 1));
for (int p = 0; p < M - 1; ++p)
f[i][j][p] = min({f[l][j - 1][p] + f[i][j - 1][p], f[l][j - 1][p + 1] + f[i][j - 1][p + 1] + 1, inf});
f[i][j][M - 1] = min(f[l][j - 1][M - 1] + f[i][j - 1][M - 1], inf);
}
}
for (int i = 1; i <= n; i++) {
dp[i] = inf;
for (int j = 0; i - (1 << j) >= 0; ++j)
dp[i] = min(dp[i], f[i][j][0] + dp[i - (1 << j)]);
}
cout << ((dp[n] >= inf)? -1 : dp[n]);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ip(), solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
113496 KB |
Output is correct |
2 |
Correct |
15 ms |
113552 KB |
Output is correct |
3 |
Correct |
15 ms |
113376 KB |
Output is correct |
4 |
Correct |
15 ms |
113400 KB |
Output is correct |
5 |
Correct |
15 ms |
113500 KB |
Output is correct |
6 |
Correct |
15 ms |
113592 KB |
Output is correct |
7 |
Correct |
15 ms |
113500 KB |
Output is correct |
8 |
Correct |
15 ms |
113352 KB |
Output is correct |
9 |
Correct |
15 ms |
113456 KB |
Output is correct |
10 |
Correct |
15 ms |
113436 KB |
Output is correct |
11 |
Correct |
15 ms |
113500 KB |
Output is correct |
12 |
Correct |
16 ms |
113500 KB |
Output is correct |
13 |
Correct |
15 ms |
113364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
113496 KB |
Output is correct |
2 |
Correct |
15 ms |
113552 KB |
Output is correct |
3 |
Correct |
15 ms |
113376 KB |
Output is correct |
4 |
Correct |
15 ms |
113400 KB |
Output is correct |
5 |
Correct |
15 ms |
113500 KB |
Output is correct |
6 |
Correct |
15 ms |
113592 KB |
Output is correct |
7 |
Correct |
15 ms |
113500 KB |
Output is correct |
8 |
Correct |
15 ms |
113352 KB |
Output is correct |
9 |
Correct |
15 ms |
113456 KB |
Output is correct |
10 |
Correct |
15 ms |
113436 KB |
Output is correct |
11 |
Correct |
15 ms |
113500 KB |
Output is correct |
12 |
Correct |
16 ms |
113500 KB |
Output is correct |
13 |
Correct |
15 ms |
113364 KB |
Output is correct |
14 |
Correct |
15 ms |
113500 KB |
Output is correct |
15 |
Correct |
16 ms |
113384 KB |
Output is correct |
16 |
Correct |
15 ms |
113500 KB |
Output is correct |
17 |
Correct |
15 ms |
113496 KB |
Output is correct |
18 |
Correct |
16 ms |
113496 KB |
Output is correct |
19 |
Correct |
15 ms |
113500 KB |
Output is correct |
20 |
Correct |
17 ms |
113500 KB |
Output is correct |
21 |
Correct |
18 ms |
113500 KB |
Output is correct |
22 |
Correct |
17 ms |
114048 KB |
Output is correct |
23 |
Correct |
18 ms |
114268 KB |
Output is correct |
24 |
Correct |
65 ms |
114516 KB |
Output is correct |
25 |
Correct |
16 ms |
113500 KB |
Output is correct |
26 |
Correct |
15 ms |
113500 KB |
Output is correct |
27 |
Correct |
14 ms |
113500 KB |
Output is correct |
28 |
Correct |
15 ms |
113436 KB |
Output is correct |
29 |
Correct |
18 ms |
113560 KB |
Output is correct |
30 |
Correct |
18 ms |
113460 KB |
Output is correct |
31 |
Correct |
17 ms |
113672 KB |
Output is correct |
32 |
Correct |
18 ms |
113500 KB |
Output is correct |
33 |
Correct |
61 ms |
114768 KB |
Output is correct |
34 |
Correct |
58 ms |
114552 KB |
Output is correct |
35 |
Correct |
57 ms |
114488 KB |
Output is correct |
36 |
Correct |
61 ms |
114420 KB |
Output is correct |
37 |
Correct |
66 ms |
114524 KB |
Output is correct |
38 |
Correct |
62 ms |
114516 KB |
Output is correct |
39 |
Correct |
60 ms |
114516 KB |
Output is correct |
40 |
Correct |
65 ms |
114480 KB |
Output is correct |
41 |
Correct |
62 ms |
114516 KB |
Output is correct |
42 |
Correct |
61 ms |
114516 KB |
Output is correct |
43 |
Correct |
60 ms |
114516 KB |
Output is correct |
44 |
Correct |
63 ms |
114532 KB |
Output is correct |
45 |
Correct |
62 ms |
114516 KB |
Output is correct |
46 |
Correct |
64 ms |
114516 KB |
Output is correct |
47 |
Correct |
43 ms |
114004 KB |
Output is correct |
48 |
Correct |
47 ms |
114260 KB |
Output is correct |
49 |
Correct |
58 ms |
114340 KB |
Output is correct |
50 |
Correct |
61 ms |
114544 KB |
Output is correct |
51 |
Correct |
16 ms |
113500 KB |
Output is correct |
52 |
Correct |
62 ms |
114516 KB |
Output is correct |