# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1209664 | nguyn | Group Photo (JOI21_ho_t3) | C++20 | 0 ms | 328 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;
int h[N];
int pos[N];
int f[N];
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;
}
for (int i = 1; i <= n; i++) f[i] = 1e9;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
int cost = 0;
for (int k = i, cur = j; k > j; k--, cur++) {
if (cur < pos[k]) cost += pos[k] - cur;
}
if (i < pos[j]) cost += pos[j] - i;
f[i] = min(f[i], f[j - 1] + cost);
}
}
cout << f[n];
}
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... |