# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
870860 | adhityamv | Group Photo (JOI21_ho_t3) | C++17 | 341 ms | 1100 KiB |
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 mp make_pair
#define pii pair<ii,ii>
#define fi first
#define se second
int n;
ll bit[5001];
ll get(int ind){
ll ans=0;
ind++;
while(ind>0){
ans+=bit[ind];
ind-=(ind&(-ind));
}
return ans;
}
void update(int ind,int val){
ind++;
while(ind<=n){
bit[ind]+=val;
ind+=(ind&(-ind));
}
}
void solve(){
cin >> n;
int a[n];
for(int i=0;i<n;i++){
cin >> a[i];
a[i]--;
}
int ainv[n];
for(int i=0;i<n;i++) ainv[a[i]]=i;
ll dp[n+1];
dp[0]=0;
for(ll i=1;i<=n;i++){
int b[i];
int ind=0;
for(int j=0;j<n;j++){
if(a[j]<i){
b[ind]=a[j];
ind++;
}
}
ll binv[i];
for(int j=0;j<i;j++) binv[b[j]]=j;
for(int j=0;j<=i;j++) bit[j]=0;
dp[i]=1000000000000000000;
ll tadd=0;
for(ll j=1;j<=i;j++){
tadd+=get(i-1-binv[i-j]);
update(i-1-binv[i-j],1);
tadd+=(i-j-binv[i-j]);
dp[i]=min(dp[i],tadd+dp[i-j]);
}
}
cout << dp[n];
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
//cin >> t;
t=1;
while(t--) solve();
}
Compilation message (stderr)
# | 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... |