# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1260070 | mingga | Group Photo (JOI21_ho_t3) | C++20 | 5094 ms | 268140 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) {
int ans = 0;
for(int k = i; k <= j; k++) {
ans += g[k][j + 1] + l[k][i];
}
// cerr << i << ' ' << j << ' ' << ans << ln;
return ans;
}
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));
}
// for(int j = 1; j <= n; j++) {
// cerr << i << ' ' << j << ' ' << g[i][j] << ' ' << l[i][j] << ln;
// }
}
cout << dp[n];
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |