#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
/*#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")*/
/*#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")*/
using namespace std;
using ll=long long int;
using ld=long double;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define lc v<<1
#define rc v<<1|1
#define fast_io ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N=2e5+10,LN=17,M=1e5,SQ=350,B=737,inf=1e9+10;
const ll INF=1e18;
const ld ep=1e-7;
const int MH=1000696969,MD=998244353,MOD=1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
ll pow(ll x, ll y, ll mod){
ll ans=1;
while (y != 0) {
if (y & 1) ans = ans * x % mod;
y >>= 1;
x = x * x % mod;
}
return ans;
}
ll n,a[N],dp[N][LN][LN],pd[N];
int main(){
fast_io;
cin >> n;
for(ll i=1; i<=n; i++) cin >> a[i];
for(ll i=1; i<=n; i++){
for(ll j=0; j<LN; j++){
if(a[i]==j+1) dp[i][0][j]=1;
else if(a[i]==j) dp[i][0][j]=0;
else dp[i][0][j]=inf;
}
for(ll j=1; j<LN; j++){
for(ll k=0; k<LN; k++){
if((1<<j)>i){
dp[i][j][k]=inf;
continue;
}
dp[i][j][k]=dp[i][j-1][k]+dp[i-(1<<(j-1))][j-1][k];
if(k<LN-1){
dp[i][j][k]=min(dp[i][j][k],dp[i][j-1][k+1]+dp[i-(1<<(j-1))][j-1][k+1]+1);
}
}
}
}
for(ll i=1; i<=n; i++){
pd[i]=inf;
for(ll j=0; (1<<j)<=i; j++) pd[i]=min(pd[i],dp[i][j][0]+pd[i-(1<<j)]);
}
if(pd[n]>1e8) pd[n]=-1;
cout << pd[n] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
1 ms |
1612 KB |
Output is correct |
8 |
Correct |
2 ms |
1612 KB |
Output is correct |
9 |
Correct |
1 ms |
1608 KB |
Output is correct |
10 |
Correct |
1 ms |
1612 KB |
Output is correct |
11 |
Correct |
1 ms |
1612 KB |
Output is correct |
12 |
Correct |
1 ms |
1612 KB |
Output is correct |
13 |
Correct |
1 ms |
1608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1612 KB |
Output is correct |
7 |
Correct |
1 ms |
1612 KB |
Output is correct |
8 |
Correct |
2 ms |
1612 KB |
Output is correct |
9 |
Correct |
1 ms |
1608 KB |
Output is correct |
10 |
Correct |
1 ms |
1612 KB |
Output is correct |
11 |
Correct |
1 ms |
1612 KB |
Output is correct |
12 |
Correct |
1 ms |
1612 KB |
Output is correct |
13 |
Correct |
1 ms |
1608 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
2508 KB |
Output is correct |
19 |
Correct |
2 ms |
2508 KB |
Output is correct |
20 |
Correct |
18 ms |
23116 KB |
Output is correct |
21 |
Correct |
17 ms |
23128 KB |
Output is correct |
22 |
Correct |
215 ms |
228340 KB |
Output is correct |
23 |
Correct |
208 ms |
228472 KB |
Output is correct |
24 |
Correct |
215 ms |
228284 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
3 ms |
2508 KB |
Output is correct |
29 |
Correct |
19 ms |
23096 KB |
Output is correct |
30 |
Correct |
18 ms |
23072 KB |
Output is correct |
31 |
Correct |
18 ms |
23116 KB |
Output is correct |
32 |
Correct |
18 ms |
23100 KB |
Output is correct |
33 |
Correct |
214 ms |
228256 KB |
Output is correct |
34 |
Correct |
209 ms |
228268 KB |
Output is correct |
35 |
Correct |
226 ms |
228308 KB |
Output is correct |
36 |
Correct |
207 ms |
228188 KB |
Output is correct |
37 |
Correct |
254 ms |
228324 KB |
Output is correct |
38 |
Correct |
211 ms |
228360 KB |
Output is correct |
39 |
Correct |
222 ms |
228264 KB |
Output is correct |
40 |
Correct |
212 ms |
228340 KB |
Output is correct |
41 |
Correct |
210 ms |
228300 KB |
Output is correct |
42 |
Correct |
200 ms |
228292 KB |
Output is correct |
43 |
Correct |
205 ms |
228292 KB |
Output is correct |
44 |
Correct |
208 ms |
228248 KB |
Output is correct |
45 |
Correct |
216 ms |
228188 KB |
Output is correct |
46 |
Correct |
205 ms |
228324 KB |
Output is correct |
47 |
Correct |
131 ms |
152260 KB |
Output is correct |
48 |
Correct |
154 ms |
177640 KB |
Output is correct |
49 |
Correct |
182 ms |
202944 KB |
Output is correct |
50 |
Correct |
212 ms |
228284 KB |
Output is correct |
51 |
Correct |
17 ms |
23124 KB |
Output is correct |
52 |
Correct |
238 ms |
228292 KB |
Output is correct |