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>
using namespace std;
#define ll long long
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define ii pair<ll,ll>
#define iii pair<ll,ii>
#define F first
#define S second
ll exo[100005][18][18];
const ll INF = 1e9;
ll dp[100005];
ll n;
ll solve(ll idx){
if(idx == n){
return 0;
}
if(dp[idx] != -1){
return dp[idx];
}
ll ans = INF;
f(j,0,18){
if(idx + (1<<j) > n){
break;
}
ans = min(ans,solve(idx + (1<<j)) + exo[idx][0][j]);
}
cout<<idx<<" "<<ans<<"\n";
return dp[idx] = ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n;
ll arr[n];
f(i,0,n){
cin>>arr[i];
}
for(ll i = n-1;i >= 0;i--){
for(ll k = 17;k >= 0;k--){
exo[i][k][0] = INF;
if(arr[i] == k){
exo[i][k][0] = 0;
}
if(arr[i] == k+1){
exo[i][k][0] = 1;
}
f(j,1,18){
exo[i][k][j] = INF;
if(i + (1<<(j-1)) < n){
exo[i][k][j] = min(exo[i][k][j],exo[i][k][j-1] + exo[i + (1<<(j-1))][k][j - 1]);
if(k+1 < 18){
exo[i][k][j] = min(exo[i][k][j],1 + exo[i][k+1][j-1] + exo[i + (1<<(j-1))][k+1][j - 1]);
}
}
}
}
}
memset(dp,-1,sizeof dp);
ll ans = solve(0);
if(ans == INF){
ans = -1;
}
cout<<ans<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |