# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
447807 | cpp219 | Group Photo (JOI21_ho_t3) | C++14 | 286 ms | 67400 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimization O2
#pragma GCC optimization "unroll-loop"
#pragma target ("avx2")
#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 5e3 + 9;
const ll Log2 = 21;
const ll inf = 1e9 + 7;
ll n,a[N],pos[N],inv[N][N],dp[N];
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define task "tst"
if (fopen(task".INP","r")){
freopen(task".INP","r",stdin);
//freopen(task".OUT","w",stdout);
}
cin>>n;
for (ll i = 1;i <= n;i++) cin>>a[i],pos[a[i]] = i;
for (ll j = 2;j <= n;j++){
ll now = 0;
for (ll i = j - 1;i;i--){
now += (pos[i] > pos[j]);
inv[i][j] = inv[i][j - 1] + now;
}
}
for (ll i = 1;i <= n;i++){
dp[i] = inf;
for (ll j = i - 1;j >= 0;j--)
dp[i] = min(dp[i], dp[j] + inv[1][i] - inv[1][j] + ((i - j)*(i - j - 1))/2 - 2 * inv[j + 1][i]);
}
cout<<dp[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... |