제출 #386866

#제출 시각아이디문제언어결과실행 시간메모리
386866nikatamlianiGroup Photo (JOI21_ho_t3)C++14
100 / 100
760 ms313800 KiB
#include <iostream>
#define ll long long
using namespace std;
const int N = 5e3+5;
ll n, a[N], id[N], dp[N], cnt[N][N], cost[N][N];
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
		id[a[i]] = i;
	}
	for(int i = 1; i <= n; ++i) {
		for(int j = n; j >= 1; --j) {
			cnt[i][j] = cnt[i][j+1] + (id[j] < id[i]);
		}
	}
	for(int L = 1; L <= n; ++L) {
		for(int R = L; R <= n; ++R) {
			cost[L][R] = cost[L][R - 1] + cnt[R][L] - (R - L - (cnt[R][L] - cnt[R][R+1]));
		}
	}
	for(int i = 1; i <= n; ++i) {
		dp[i] = 1e15;
		for(int j = 0; j < i; ++j) {
			dp[i] = min(dp[i], dp[j] + cost[j+1][i]);
		}
	}
	cout << dp[n] << endl;
}
#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...