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>
#pragma GCC optimize ("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define FOR(i,a,b) for (int i = (a); i<(b); i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define RREP(i,n) for (int i=(n)-1; i>=0; i--)
#define RREP1(i,n) for (int i=(n); i>=1; i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int, int>
#define pdd pair<double ,double>
#define pcc pair<char, char>
#define endl '\n'
//#define TOAD
#ifdef TOAD
#define bug(x) cerr<<__LINE__<<": "<<#x<<" is "<<x<<endl
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#endif
const ll inf = 1ll<<60;
const int iinf=2147483647;
const ll mod = 1e9+7;
const ll maxn=5005;
const double PI=acos(-1);
ll pw(ll x, ll p, ll m=mod){
ll ret=1;
while (p>0){
if (p&1){
ret*=x;
ret%=m;
}
x*=x;
x%=m;
p>>=1;
}
return ret;
}
ll inv(ll a, ll m=mod){
return pw(a,m-2);
}
//=======================================================================================
int costa[maxn][maxn], costb[maxn][maxn], F[maxn][maxn], P[maxn][maxn], S[maxn][maxn];
int tagP[maxn][maxn], tagS[maxn][maxn];
vector<int> dp(maxn);
signed main (){
IOS();
int n; cin>>n;
vector<int> vc(n+1);
REP1(i,n) cin>>vc[i];
REP1(i,n){
FOR(j,i+1,n+1){
tagP[vc[i]][vc[j]]++;
tagS[vc[j]][vc[i]]++;
}
}
REP1(j,n){
REP1(i,n){
P[i][j]=P[i-1][j]+tagP[i][j];
}
}
REP1(i,n){
RREP1(j,n){
S[i][j]=S[i][j+1]+tagS[i][j];
}
}
REP1(i,n){
FOR(j,i,n+1){
F[i][j]=P[j][j]-P[i-1][j];
}
}
REP1(i,n){
FOR(j,i,n+1){
if (i==j){
costa[i][j]=0;
continue;
}
costa[i][j]=costa[i][j-1]+F[i][j];
}
}
RREP1(i,n){
FOR(j,i,n+1){
if (i==j) {
costb[i][j]=S[i][j+1];
continue;
}
costb[i][j]=costb[i+1][j]+S[i][j+1];
}
}
/*REP1(i,n){
FOR(j,i,n+1){
cout<<costb[i][j]<<' ';
}
cout<<endl;
}
*/
REP1(i,n){
dp[i]=inf;
REP(j,i){
dp[i]=min(dp[i], dp[j]+costa[j+1][i]+costb[j+1][i]);
}
}
cout<<dp[n]<<endl;
}
# | 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... |