Submission #1260073

#TimeUsernameProblemLanguageResultExecution timeMemory
1260073minggaGroup Photo (JOI21_ho_t3)C++20
100 / 100
351 ms268380 KiB
// Author: caption_mingle
#include "bits/stdc++.h"

using namespace std;

#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 5005;
int n, a[N], pos[N], dp[N], g[N][N], l[N][N], psg[N][N], psl[N][N];

int cost(int i, int j) {
	return psg[j][j + 1] - psg[i - 1][j + 1] + psl[j][i] - psl[i - 1][i];
}

signed main() {
	cin.tie(0) -> sync_with_stdio(0);
	#define task ""
	if(fopen(task ".INP", "r")) {
		freopen(task ".INP", "r", stdin);
		freopen(task ".OUT", "w", stdout);
	}
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i], pos[a[i]] = i;
	}
	for(int i = 1; i <= n; i++) {
		for(int j = n; j >= i; j--) {
			g[i][j] = g[i][j + 1] + (pos[j] < pos[i]);
			psg[i][j] = psg[i - 1][j] + g[i][j];
		}
		for(int j = i - 1; j > 0; j--) {
			l[i][j] = l[i][j + 1] + (pos[j] < pos[i]);
			psl[i][j] = psl[i - 1][j] + l[i][j];
		}
	}
	for(int i = 1; i <= n; i++) {
		dp[i] = inf;
		for(int j = i; j > 0; j--) {
			dp[i] = min(dp[i], dp[j - 1] + cost(j, i));
		}
	}
	cout << dp[n];
    cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:26:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |                 freopen(task ".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:27:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |                 freopen(task ".OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#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...