# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1260073 | mingga | Group Photo (JOI21_ho_t3) | C++20 | 351 ms | 268380 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;
}
컴파일 시 표준 에러 (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... |