# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
720025 | khanhhdz191 | Group Photo (JOI21_ho_t3) | C++14 | 423 ms | 196352 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.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
const ll maxn= 5002;
const ll base=3e18;
const ll mod= 1e9+7 ;
/// goal 4/8
ll pos[maxn];
ll dp[maxn];
ll sum[maxn][maxn];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen("cheetos.inp","r"))
{
freopen("cheetos.in","r",stdin);
freopen("cheetos.out","w",stdout);
}
ll n;
cin>> n;
for (int i=1; i<=n; i++)
{
ll x;
cin>> x;
pos[x]=i;
}
ll ans=0;
for (int i=1; i<=n; i++)
for (int j=1; j<i; j++)
{
if (pos[j]>pos[i]) ans++;
}
for (int i=1; i<=n; i++)
for (int j=1; j<=n; j++)
{
if (i<j&&pos[i]>pos[j]) sum[i][j]--;
if (i<j&&pos[i]<pos[j]) sum[i][j]++;
sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
}
for (int i=1; i<=n; i++)
{
dp[i]=base;
for (int j=0; j<i; j++)
{
dp[i]=min(dp[i],dp[j]+sum[i][i]+sum[j][j]-sum[i][j]-sum[j][i]);
}
}
ans+=dp[n];
cout <<ans;
}
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... |