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;
#include <bits/extc++.h>
using namespace __gnu_pbds;
#include <ext/rope>
using namespace __gnu_cxx;
typedef tree<long long, null_type, less<long long>,
rb_tree_tag, tree_order_statistics_node_update> pbds;
//less_equal for identical elements
#define DEBUG
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__);
#else
#define debug(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
#define maxn 5005
int n,h[maxn],pos[maxn],ft[maxn],cost[maxn][maxn],pfx[maxn],dp[maxn];
void up(int x){
while(x<=n)++ft[x],x+=x&-x;
}
int qry(int x){
int res=0;
while(x)res+=ft[x],x-=x&-x;
return res;
}
int main(){
sf("%d",&n);
for(int i=1;i<=n;++i){
sf("%d",&h[i]);
pos[h[i]]=i;
}
for(int i=1;i<=n;++i){
memset(ft,0,sizeof ft);
memset(pfx,0,sizeof pfx);
for(int j=1;j<i;++j)++pfx[pos[j]];
for(int j=1;j<=n;++j)pfx[j]+=pfx[j-1];
for(int j=i;j<=n;++j){
//cost to make j...i, only considering stuff in [i,n]
cost[i][j]=cost[i][j-1];
cost[i][j]+=qry(pos[j]); //number of stuff before it
cost[i][j]+=i-1-pfx[pos[j]];
up(pos[j]);
}
}
for(int i=1;i<=n;++i){
dp[i]=INF;
for(int j=1;j<=i;++j){
dp[i]=min(dp[i],dp[j-1]+cost[j][i]);
}
}
pf("%d\n",dp[n]);
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:55:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | sf("%d",&n);
| ^
Main.cpp:57:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | sf("%d",&h[i]);
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |