This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |