#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 100005
#define LOG 17
int nArr;
int A[MAX];
int cost[MAX][LOG];
int dp[LOG + 2][MAX][LOG + 2];
int new_dp[MAX];
void process(void){
cin >> nArr;
for (int i = 1; i <= nArr; ++i) cin >> A[i];
for (int i = 1; i <= nArr; ++i){
if(A[i] > LOG) return void(cout << -1);
}
memset(dp, 0x3f, sizeof dp);
for (int i = 1; i <= nArr; ++i){
dp[0][i][A[i]] = 0;
if(A[i] > 0)
dp[0][i][A[i] - 1] = 1;
}
for (int l = 1; MASK(l) <= nArr; ++l){
for (int i = 1; i <= nArr; ++i){
int m = i + MASK(l - 1);
for (int value = 0; value < LOG; ++value){
minimize(dp[l][i][value], dp[l - 1][i][value + 1] + dp[l - 1][m][value + 1] + 1);
minimize(dp[l][i][value], dp[l - 1][i][value] + dp[l - 1][m][value]);
}
}
}
memset(new_dp, 0x3f, sizeof new_dp);
new_dp[0] = 0;
for (int i = 1; i <= nArr; ++i){
for (int l = 0; i - MASK(l) >= 0; ++l){
minimize(new_dp[i], new_dp[i - MASK(l)] + dp[l][i - MASK(l) + 1][0]);
}
}
cout << (new_dp[nArr] >= INF ? -1 : new_dp[nArr]);
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
283720 KB |
Output is correct |
2 |
Correct |
147 ms |
283836 KB |
Output is correct |
3 |
Correct |
130 ms |
283652 KB |
Output is correct |
4 |
Correct |
129 ms |
283744 KB |
Output is correct |
5 |
Correct |
129 ms |
283764 KB |
Output is correct |
6 |
Correct |
160 ms |
283720 KB |
Output is correct |
7 |
Correct |
153 ms |
283632 KB |
Output is correct |
8 |
Correct |
131 ms |
283624 KB |
Output is correct |
9 |
Correct |
128 ms |
283724 KB |
Output is correct |
10 |
Correct |
150 ms |
283720 KB |
Output is correct |
11 |
Correct |
143 ms |
283720 KB |
Output is correct |
12 |
Correct |
125 ms |
283720 KB |
Output is correct |
13 |
Correct |
127 ms |
283720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
283720 KB |
Output is correct |
2 |
Correct |
147 ms |
283836 KB |
Output is correct |
3 |
Correct |
130 ms |
283652 KB |
Output is correct |
4 |
Correct |
129 ms |
283744 KB |
Output is correct |
5 |
Correct |
129 ms |
283764 KB |
Output is correct |
6 |
Correct |
160 ms |
283720 KB |
Output is correct |
7 |
Correct |
153 ms |
283632 KB |
Output is correct |
8 |
Correct |
131 ms |
283624 KB |
Output is correct |
9 |
Correct |
128 ms |
283724 KB |
Output is correct |
10 |
Correct |
150 ms |
283720 KB |
Output is correct |
11 |
Correct |
143 ms |
283720 KB |
Output is correct |
12 |
Correct |
125 ms |
283720 KB |
Output is correct |
13 |
Correct |
127 ms |
283720 KB |
Output is correct |
14 |
Correct |
136 ms |
283720 KB |
Output is correct |
15 |
Correct |
134 ms |
283728 KB |
Output is correct |
16 |
Correct |
131 ms |
283720 KB |
Output is correct |
17 |
Correct |
136 ms |
283724 KB |
Output is correct |
18 |
Correct |
136 ms |
283720 KB |
Output is correct |
19 |
Correct |
140 ms |
283720 KB |
Output is correct |
20 |
Correct |
121 ms |
283948 KB |
Output is correct |
21 |
Correct |
122 ms |
283820 KB |
Output is correct |
22 |
Correct |
180 ms |
284744 KB |
Output is correct |
23 |
Correct |
6 ms |
1400 KB |
Output is correct |
24 |
Correct |
164 ms |
284744 KB |
Output is correct |
25 |
Correct |
122 ms |
283724 KB |
Output is correct |
26 |
Correct |
123 ms |
283720 KB |
Output is correct |
27 |
Correct |
129 ms |
283720 KB |
Output is correct |
28 |
Correct |
132 ms |
283720 KB |
Output is correct |
29 |
Correct |
124 ms |
283720 KB |
Output is correct |
30 |
Correct |
122 ms |
283720 KB |
Output is correct |
31 |
Correct |
125 ms |
283720 KB |
Output is correct |
32 |
Correct |
127 ms |
283732 KB |
Output is correct |
33 |
Correct |
166 ms |
284644 KB |
Output is correct |
34 |
Correct |
169 ms |
284744 KB |
Output is correct |
35 |
Correct |
199 ms |
284652 KB |
Output is correct |
36 |
Correct |
187 ms |
284744 KB |
Output is correct |
37 |
Correct |
200 ms |
284744 KB |
Output is correct |
38 |
Correct |
185 ms |
284748 KB |
Output is correct |
39 |
Correct |
194 ms |
284756 KB |
Output is correct |
40 |
Correct |
201 ms |
284744 KB |
Output is correct |
41 |
Correct |
194 ms |
284744 KB |
Output is correct |
42 |
Correct |
198 ms |
284792 KB |
Output is correct |
43 |
Correct |
192 ms |
284696 KB |
Output is correct |
44 |
Correct |
191 ms |
284744 KB |
Output is correct |
45 |
Correct |
184 ms |
284744 KB |
Output is correct |
46 |
Correct |
187 ms |
284744 KB |
Output is correct |
47 |
Correct |
201 ms |
284488 KB |
Output is correct |
48 |
Correct |
185 ms |
284488 KB |
Output is correct |
49 |
Correct |
172 ms |
284488 KB |
Output is correct |
50 |
Correct |
181 ms |
284744 KB |
Output is correct |
51 |
Correct |
147 ms |
283720 KB |
Output is correct |
52 |
Correct |
183 ms |
284732 KB |
Output is correct |