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=202;
int n,arr[N],inv[N][N],dp[N][N];
int odp[N][N][N];
int cost(int a,int b,int c){
if(~odp[a][b][c])return odp[a][b][c];
vector<int>v;
for(int i=b;i>a;i--)v.pb(i);
if(c)v.pb(c);
int res=0;
for(int i=1;i<=a;i++)
for(int j=!c;j<int(v.size());j++)
res+=inv[i][v[j]];
for(int i=0;i<int(v.size());i++)
for(int j=i+1;j<int(v.size());j++)
res+=inv[v[i]][v[j]];
return odp[a][b][c]=res;
}
int rec(int a,int b){
if(b==n)return cost(a,b,0);
int &ret=dp[a][b];
if(~ret)return ret;ret=INF;
for(int i=b+1;i<=n;i++)
umin(ret,rec(b,i)+cost(a,b,i));
return ret;
}
int main(){
//freopen("file.in", "r", stdin);
memset(dp,-1,sizeof dp);
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]]++;
int ans=INF;
for(int i=1;i<=n;i++)
umin(ans,rec(0,i));
printf("%d\n",ans);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int rec(int, int)':
Main.cpp:40:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
40 | if(~ret)return ret;ret=INF;
| ^~
Main.cpp:40:21: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
40 | if(~ret)return ret;ret=INF;
| ^~~
Main.cpp: In function 'int main()':
Main.cpp:49:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Main.cpp:50:28: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | 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... |