# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1209738 | nguyn | Group Photo (JOI21_ho_t3) | C++20 | 272 ms | 98444 KiB |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define pb push_back
#define pii pair<int,int>
const int N = 5e3 + 5;
int n, h[N], pos[N], rev[N][N], f[N];
int cost(int l, int r) {
return rev[r][r] - rev[l - 1][r] - rev[r][l - 1] + rev[l - 1][l - 1];
}
signed main(){
ios_base::sync_with_stdio(false) ;
cin.tie(0) ; cout.tie(0) ;
if (fopen("INP.INP" ,"r")) {
freopen("INP.INP" ,"r" , stdin) ;
freopen("OUT.OUT" , "w" , stdout) ;
}
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i];
pos[h[i]] = i;
}
int res = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (pos[i] > pos[j]) {
rev[i][j] = -1;
res++;
}
else {
rev[i][j] = 1;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
rev[i][j] += rev[i - 1][j] + rev[i][j - 1] - rev[i - 1][j - 1];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
f[i] = min(f[i], f[j - 1] + cost(j, i));
}
}
cout << res + f[n];
}
컴파일 시 표준 에러 (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... |