#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
#define ff
#define ss
void debug(){
cerr << endl;
}
template<typename Head, typename... Tail>
void debug(Head h, Tail... t){
cerr << " " << h << ",";
debug(t...);
}
#define er(...) cerr << __LINE__ << " (" << #__VA_ARGS__ << "):", debug(__VA_ARGS__)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
// #ifndef ONLINE_JUDGE
// freopen("inp.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// #endif
}
const ll mod = 1e9 + 7, maxn = 1e5 + 5, lg = 18, inf = ll(1e9) + 5;
int a[maxn];
int dp[maxn][lg][lg], rmq[maxn][lg], dp2[maxn];
int main(){ IOS();
int n; cin >> n;
rep(i,0,n){
cin >> a[i];
rmq[i][0] = a[i];
rep(j,0,lg) rep(k,0,lg) dp[i][j][k] = inf;
}
rep(i,0,n){
if(a[i] >= lg){
return cout << -1 << '\n', 0;
}
dp[i][0][a[i]] = 0;
if(a[i]) dp[i][0][a[i]-1] = 1;
}
rep(j,1,lg){
rep(i,0,n-(1<<j)+1){
rmq[i][j] = min(rmq[i][j-1], rmq[i+(1<<(j-1))][j-1]);
}
}
rep(j,1,lg){
rep(i,0,n-(1<<j)+1){
int mn = rmq[i][j];
rep(k,0,mn+1){
dp[i][j][k] = min({
dp[i][j-1][k] + dp[i+(1<<(j-1))][j-1][k],
(k < lg-1? dp[i][j-1][k+1] + dp[i+(1<<(j-1))][j-1][k+1] + 1: (int)inf),
(int)inf
});
}
er(i, j, dp[i][j][0]);
}
}
rep(i,0,n){
dp2[i] = inf;
rep(j,0,lg){
int l = i - (1<<j) + 1;
if(l < 0) break;
dp2[i] = min(dp2[i], dp[l][j][0] + (l?dp2[l-1]:0));
}
}
cout << (dp2[n-1] == inf? -1: dp2[n-1]) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
1228 KB |
Output is correct |
2 |
Correct |
70 ms |
1300 KB |
Output is correct |
3 |
Correct |
70 ms |
1276 KB |
Output is correct |
4 |
Correct |
73 ms |
1320 KB |
Output is correct |
5 |
Correct |
69 ms |
1316 KB |
Output is correct |
6 |
Correct |
68 ms |
1288 KB |
Output is correct |
7 |
Correct |
69 ms |
1228 KB |
Output is correct |
8 |
Correct |
71 ms |
1288 KB |
Output is correct |
9 |
Correct |
69 ms |
1312 KB |
Output is correct |
10 |
Correct |
70 ms |
1392 KB |
Output is correct |
11 |
Correct |
71 ms |
1212 KB |
Output is correct |
12 |
Correct |
68 ms |
1200 KB |
Output is correct |
13 |
Correct |
70 ms |
1264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
1228 KB |
Output is correct |
2 |
Correct |
70 ms |
1300 KB |
Output is correct |
3 |
Correct |
70 ms |
1276 KB |
Output is correct |
4 |
Correct |
73 ms |
1320 KB |
Output is correct |
5 |
Correct |
69 ms |
1316 KB |
Output is correct |
6 |
Correct |
68 ms |
1288 KB |
Output is correct |
7 |
Correct |
69 ms |
1228 KB |
Output is correct |
8 |
Correct |
71 ms |
1288 KB |
Output is correct |
9 |
Correct |
69 ms |
1312 KB |
Output is correct |
10 |
Correct |
70 ms |
1392 KB |
Output is correct |
11 |
Correct |
71 ms |
1212 KB |
Output is correct |
12 |
Correct |
68 ms |
1200 KB |
Output is correct |
13 |
Correct |
70 ms |
1264 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
8 ms |
468 KB |
Output is correct |
17 |
Correct |
8 ms |
496 KB |
Output is correct |
18 |
Correct |
124 ms |
1988 KB |
Output is correct |
19 |
Correct |
133 ms |
2000 KB |
Output is correct |
20 |
Correct |
1813 ms |
18968 KB |
Output is correct |
21 |
Correct |
1776 ms |
19024 KB |
Output is correct |
22 |
Execution timed out |
2092 ms |
140748 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |