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 int ll
#define rep(n) rep1(i,n)
#define rep1(i,n) rep2(i,0,n)
#define rep2(i,a,b) for(int i=a; i<(b); ++i)
#define rep3(i,a,b) for(int i=a; i>=(b); --i)
#define pii pair<int,int>
#define pb push_back
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define inf 1000'000'000'000'000'000
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ": " << #__VA_ARGS__ << "- ",_do(__VA_ARGS__)
template<typename T> void _do(T x){cerr << x << endl;}
template<typename T, typename ... S> void _do(T x, S ...y){cerr << x << ", ";_do(y...);}
#else
#define bug(...) 49
#endif
const int maxn=5005;
int n,a[maxn],dp[maxn],pos[maxn];
int bit[maxn];
void modify(int p, int x){for(int i=p+1; i<maxn; i+=i&-i) bit[i]+=x;}
int query(int r){
int res=0;
for(int i=r+1; i; i-=i&-i) res+=bit[i];
return res;
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0);
cin >> n;
rep(n) cin >> a[i];
rep(n) a[i]--;
rep2(i,2,n+1){
vector<int> vec;
rep1(j,n) if(a[j]<i) vec.pb(j);
rep1(j,sz(vec)) pos[a[vec[j]]]=j;
//rep1(j,sz(vec)) bug(vec[j]);
memset(bit,0,sizeof bit);
int cur=0,sum=0;
dp[i]=inf;
rep3(j,i-1,0){
cur+=i-j-1-query(pos[j]);
sum+=pos[j]+1;
dp[i]=min(dp[i],dp[j]+cur+(i*(i+1)/2-j*(j+1)/2-sum));
modify(pos[j],1);
}
bug(dp[i]);
}
cout << dp[n] << endl;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:21:18: warning: statement has no effect [-Wunused-value]
21 | #define bug(...) 49
| ^~
Main.cpp:55:9: note: in expansion of macro 'bug'
55 | bug(dp[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... |