제출 #522255

#제출 시각아이디문제언어결과실행 시간메모리
522255AdamGSGroup Photo (JOI21_ho_t3)C++17
100 / 100
639 ms263584 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=5e3+7, INF=1e9+7;
int T[LIM], mniejsze[LIM], ile[LIM][LIM], ile2[LIM][LIM], dp[LIM][LIM], mi[LIM];
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	rep(i, n) {
		cin >> T[i]; --T[i];
	}
	rep(i, n) {
		rep(j, i) ++ile[T[i]][T[j]];
		for(int j=i+1; j<n; ++j) ++ile2[T[i]][T[j]];
		for(int j=n-2; j>=0; --j) {
			ile[T[i]][j]+=ile[T[i]][j+1];
			ile2[T[i]][j]+=ile2[T[i]][j+1];
		}
		for(int j=i; j<n; ++j) if(T[i]>T[j]) ++mniejsze[T[i]];
	}
	rep(i, n) {
		int akt=0;
		for(int j=i; j>0; --j) {
			akt+=mniejsze[j];
			akt-=ile[j][j]-ile[j][i+1];
			akt+=ile2[j][j]-ile2[j][i+1];
			dp[i][j]=mi[j-1]+akt;
		}
		akt-=ile[0][0]-ile[0][i+1];
		akt+=ile2[0][0]-ile2[0][i+1];
		dp[i][0]=akt;
		mi[i]=INF;
		rep(j, i+1) mi[i]=min(mi[i], dp[i][j]);
	}
	int ans=INF;
	rep(i, n) ans=min(ans, dp[n-1][i]);
	cout << ans << '\n';
}
#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...