#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define wall cerr<<"-------------------------------------"<<endl
typedef long long ll;
const ll INF = 1e17;
const ll maxn = 1e5 + 10;
ll n , a[maxn] , mn[maxn] , res[maxn][22];
vector<pll> seg[maxn];
ll dp[22][maxn][22];
int main()
{
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
cin>>n;
for(int i=1; i<=n; i++){
cin>>a[i];
}
for(int i=0; i<20; i++){
for(int l=1; l<=n; l++){
for(int k=0; k<20; k++){
dp[i][l][k] = INF;
dp[i][l][k] = INF;
}
}
}
for(int pos=1; pos<=n; pos++){
dp[0][pos][a[pos]] = 0;
if(a[pos]<1)continue;
dp[0][pos][a[pos]-1] = 1;
}
for(int i=1; i<20; i++){
for(int l=1; l+(1<<i)-1<=n; l++){
int mid=l+(1<<(i-1));
for(int k=0; k<20; k++){
dp[i][l][k] = min(dp[i][l][k] , dp[i-1][l][k]+dp[i-1][mid][k]);
dp[i][l][k] = min(dp[i][l][k] , dp[i-1][l][k+1]+dp[i-1][mid][k+1]+1);
}
}
}
/**
for(int i=0; i<4; i++){
for(int l=1; l+(1<<i)-1<=n; l++){
for(int k=0; k<4; k++){
cout<<l<<'-'<<l+(1<<i)-1<<" , +"<<k<<" : "<<dp[i][l][k]<<endl;
}
wall;
}
wall;
wall;
}
**/
for(int i=1; i<=n; i++){
for(int j=0; j<20; j++){
res[i][j] = INF;
}
}
for(int i=0; i<20; i++){
for(int l=1; l+(1<<i)-1<=n; l++){
int r=l+(1<<i)-1;
res[r][i] = dp[i][l][0];
}
}
mn[0] = 0;
for(int i=1; i<=n; i++)mn[i] = INF;
for(int i=1; i<=n; i++){
for(int j=0; j<20; j++){
int l = i-(1<<j) , w = res[i][j];
if(l<0)continue;
mn[i] = min(mn[i] , mn[l]+w);
}
}
if(mn[n]>=(20*n)){
cout<<-1;
return 0;
}else cout<<mn[n];
return 0;
}
/**
10
0 0 0 0 0 0 0 0 0 0
*/
// ans = 5;
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4988 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4988 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4988 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4988 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
1 ms |
2772 KB |
Output is correct |
15 |
Correct |
1 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
3156 KB |
Output is correct |
17 |
Correct |
1 ms |
3156 KB |
Output is correct |
18 |
Correct |
3 ms |
6392 KB |
Output is correct |
19 |
Correct |
3 ms |
6356 KB |
Output is correct |
20 |
Correct |
21 ms |
39168 KB |
Output is correct |
21 |
Correct |
21 ms |
39140 KB |
Output is correct |
22 |
Correct |
245 ms |
366140 KB |
Output is correct |
23 |
Correct |
215 ms |
366272 KB |
Output is correct |
24 |
Correct |
202 ms |
366020 KB |
Output is correct |
25 |
Correct |
2 ms |
2812 KB |
Output is correct |
26 |
Correct |
3 ms |
3284 KB |
Output is correct |
27 |
Correct |
2 ms |
3156 KB |
Output is correct |
28 |
Correct |
3 ms |
6356 KB |
Output is correct |
29 |
Correct |
23 ms |
39036 KB |
Output is correct |
30 |
Correct |
23 ms |
39160 KB |
Output is correct |
31 |
Correct |
31 ms |
39064 KB |
Output is correct |
32 |
Correct |
23 ms |
39176 KB |
Output is correct |
33 |
Correct |
194 ms |
366084 KB |
Output is correct |
34 |
Correct |
203 ms |
366256 KB |
Output is correct |
35 |
Correct |
200 ms |
366164 KB |
Output is correct |
36 |
Correct |
197 ms |
366088 KB |
Output is correct |
37 |
Correct |
212 ms |
366240 KB |
Output is correct |
38 |
Correct |
195 ms |
366072 KB |
Output is correct |
39 |
Correct |
197 ms |
366092 KB |
Output is correct |
40 |
Correct |
208 ms |
366136 KB |
Output is correct |
41 |
Correct |
196 ms |
365992 KB |
Output is correct |
42 |
Correct |
198 ms |
366124 KB |
Output is correct |
43 |
Correct |
196 ms |
366092 KB |
Output is correct |
44 |
Correct |
195 ms |
366036 KB |
Output is correct |
45 |
Correct |
199 ms |
366024 KB |
Output is correct |
46 |
Correct |
198 ms |
366092 KB |
Output is correct |
47 |
Correct |
137 ms |
245100 KB |
Output is correct |
48 |
Correct |
155 ms |
285452 KB |
Output is correct |
49 |
Correct |
178 ms |
325836 KB |
Output is correct |
50 |
Correct |
196 ms |
366092 KB |
Output is correct |
51 |
Correct |
23 ms |
39124 KB |
Output is correct |
52 |
Correct |
196 ms |
366012 KB |
Output is correct |