Submission #937087

#TimeUsernameProblemLanguageResultExecution timeMemory
937087koukirocksGroup Photo (JOI21_ho_t3)C++17
100 / 100
355 ms852 KiB
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
 
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
const ll MAX=5e3+10,P=998244353;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;

int n;
int a[MAX];
ll dp[MAX];
int BIT[MAX];

void update(int x,int val) {
	while (x<MAX) {
		BIT[x]+=val;
		x+=(-x&x);
	}
}

int query(int x) {
	int ans=0;
	while (x) {
		ans+=BIT[x];
		x-=(-x&x);
	}
	return ans;
}

int main() {
	speed;
	cin>>n;
	for (int i=1;i<=n;i++) {
		cin>>a[i];
	}
	for (int i=1;i<=n;i++) {
		vector<int> arr;
		arr.push_back(0);
		for (int j=1;j<=n;j++) {
			if (a[j]>=n-i+1) arr.push_back(a[j]-(n-i));
		}
		vector<int> pos(i+1);
		for (int j=1;j<=i;j++) pos[arr[j]]=j;
		memset(BIT,0,sizeof(BIT));
		ll now=0;
		ll ans=oo;
		for (int j=1;j<=i;j++) {
			now-=j-1;
			now+=query(pos[j]);
			update(pos[j],1);
			now+=pos[j]-1;
			ans=min(ans,now+dp[i-j]);
		}
		dp[i]=ans;
//		cout<<dp[i]<<" ";
	}
	cout<<dp[n]<<"\n";
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...