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"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N=5005;
int n,arr[N],inv[N][N],dp[N][N];
int odp[N][N],par[N][N],F[N][N];
int A[N][N],B[N][N],sdp[N][N];
//inv[x][x]=0
int g(int x,int y){
if(x>=y)return 0;
int &ret=odp[x][y];
if(~ret)return ret;
return ret=g(x,y-1)+par[y][y]-par[y][x-1];
}
int cost(int a,int b){
return g(a+1,b-1)+F[a][b-1]+(B[b][b-1]-B[b][a]);
}
int main(){
//freopen("file.in", "r", stdin);
memset(odp,-1,sizeof odp);
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",arr+i);
for(int i=2;i<=n;i++)
for(int j=1;j<i;j++)
inv[arr[i]][arr[j]]=1;
for(int j=1;j<=n;j++)
for(int i=1;i<=n;i++)
par[j][i]=par[j][i-1]+inv[j][i];
//f(1,a,a+1,b-1) = F[a][b-1]
for(int a=1;a<=n;a++)
for(int y=a+1;y<=n;y++)
F[a][y]=F[a][y-1]+par[y][a];
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++){
A[k][i]=A[k][i-1]+inv[i][k];
B[k][i]=B[k][i-1]+inv[k][i];
}
for(int a=n;a>=0;a--){
for(int b=a+1;b<=n;b++){
if(b==n) dp[a][b]=cost(a,b);
else dp[a][b]=sdp[b][b+1]+cost(a,b);
}
sdp[a][n+1]=INF;
for(int b=n;b>=1;b--)
sdp[a][b]=min(sdp[a][b+1],dp[a][b]+A[b][a]);
}
int ans=INF;
for(int i=1;i<=n;i++)
umin(ans,dp[0][i]);
printf("%d\n",ans);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:37:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Main.cpp:38:28: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | for(int i=1;i<=n;i++)scanf("%d",arr+i);
| ~~~~~^~~~~~~~~~~~
# | 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... |