Submission #718067

#TimeUsernameProblemLanguageResultExecution timeMemory
718067KalashnikovGroup Photo (JOI21_ho_t3)C++17
100 / 100
636 ms67552 KiB
#include <bits/stdc++.h>
 
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
 
using namespace std;
using ll = long long;
 
const int N = 5000+5 , inf = 2e9 + 7;
const ll INF = 1e18 ,   mod = 1e9+7 , P = 6547;

int dp[N], pos[N], a[N], res[N][N];


int f[N];

int get(int x) {
	int res = 0;
	for(; x > 0; x&=x+1, x--) {
		res += f[x];
	}
	return res;
}

void upd(int pos, int val = 1) {
	for(; pos < N; pos |= pos+1) {
		f[pos] += val;
	}
}

int f1[N], db[N];

int get1(int x) {
	int res = 0;
	for(; x > 0; x&=x+1, x--) {
		res += f1[x];
	}
	return res;
}

void upd1(int pos, int val = 1) {
	for(; pos < N; pos |= pos+1) {
		f1[pos] += val;
	}
}

void solve(int tc) {
	int n;
	cin >> n;
	for(int i = 1; i <= n; i ++) {
		cin >> a[i];
		pos[a[i]] = i;
	}
	for(int r = 1; r <= n; r ++) {
		fill(f1, f1+n+1, 0);
		for(int i = r; i >= 1; i --) {
			db[i] = get1(pos[i]);
			upd1(pos[i]);
		}
		upd(pos[r]);
		for(int l = r; l >= 1; l --) {
			if(l == r) {
				res[l][r] = r-get(pos[r]);
			}
			else {
				res[l][r] = res[l+1][r];
				res[l][r] += r-get(pos[l]);
				res[l][r] -= db[l];
			}
		}
	}
	for(int i = 1; i <= n; i ++) {
		dp[i] = inf;
		for(int j = i; j >= 1; j --) {
			dp[i] = min(dp[i], res[j][i] + dp[j-1]);
		}
	}
	cout << dp[n];
}
/*
*/
main() {
    ios;
    int tt = 1 , tc = 0;
    // cin >> tt;
    while(tt --) {
        solve(++tc);
    }
    return 0;
}

Compilation message (stderr)

Main.cpp:85:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   85 | main() {
      | ^~~~
#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...