#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 g(int x,int y){
int res=0;
for(int i=y;i>=x;i--)
for(int j=i-1;j>=x;j--)
res+=inv[i][j];
return res;
}
int f(int x,int y,int xx,int yy){
int res=0;
for(int i=x;i<=y;i++)
for(int j=xx;j<=yy;j++)
res+=inv[i][j];
return res;
}
int cost(int a,int b,int c){
int res=g(a+1,b-1)+f(1,a,a+1,b-1)+f(b,b,a+1,b-1);
if(c)res+=f(1,a,c,c)+f(b,b,c,c)+f(a+1,b-1,c,c);
return 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);
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
Main.cpp: In function 'int main()':
Main.cpp:54:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Main.cpp:55:28: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | for(int i=1;i<=n;i++)scanf("%d",arr+i);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
4229 ms |
608 KB |
Output is correct |
22 |
Correct |
4276 ms |
608 KB |
Output is correct |
23 |
Correct |
4224 ms |
612 KB |
Output is correct |
24 |
Correct |
4393 ms |
616 KB |
Output is correct |
25 |
Correct |
4438 ms |
612 KB |
Output is correct |
26 |
Correct |
4392 ms |
612 KB |
Output is correct |
27 |
Correct |
4356 ms |
608 KB |
Output is correct |
28 |
Correct |
4375 ms |
612 KB |
Output is correct |
29 |
Correct |
4362 ms |
608 KB |
Output is correct |
30 |
Correct |
4405 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
4229 ms |
608 KB |
Output is correct |
22 |
Correct |
4276 ms |
608 KB |
Output is correct |
23 |
Correct |
4224 ms |
612 KB |
Output is correct |
24 |
Correct |
4393 ms |
616 KB |
Output is correct |
25 |
Correct |
4438 ms |
612 KB |
Output is correct |
26 |
Correct |
4392 ms |
612 KB |
Output is correct |
27 |
Correct |
4356 ms |
608 KB |
Output is correct |
28 |
Correct |
4375 ms |
612 KB |
Output is correct |
29 |
Correct |
4362 ms |
608 KB |
Output is correct |
30 |
Correct |
4405 ms |
604 KB |
Output is correct |
31 |
Runtime error |
3 ms |
716 KB |
Execution killed with signal 11 |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
4229 ms |
608 KB |
Output is correct |
22 |
Correct |
4276 ms |
608 KB |
Output is correct |
23 |
Correct |
4224 ms |
612 KB |
Output is correct |
24 |
Correct |
4393 ms |
616 KB |
Output is correct |
25 |
Correct |
4438 ms |
612 KB |
Output is correct |
26 |
Correct |
4392 ms |
612 KB |
Output is correct |
27 |
Correct |
4356 ms |
608 KB |
Output is correct |
28 |
Correct |
4375 ms |
612 KB |
Output is correct |
29 |
Correct |
4362 ms |
608 KB |
Output is correct |
30 |
Correct |
4405 ms |
604 KB |
Output is correct |
31 |
Runtime error |
3 ms |
716 KB |
Execution killed with signal 11 |
32 |
Halted |
0 ms |
0 KB |
- |