#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=5002;
int n,arr[N],inv[N][N],dp[N][N],odp[N][N],suf[N][N];
int F[N][N],AB[N][N],BA[N][N],par[N][N],G[N][N];
int g(int x,int y){
if(x>=y)return 0;
if(~odp[x][y])return odp[x][y];
return odp[x][y]=g(x,y-1)+par[y][y]-par[x-1][y];
}
int cost(int a,int b){
return g(a+1,b-1)+F[a][b-1]+(BA[b][b-1]-BA[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]]++;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
par[i][j]=par[i-1][j]+inv[j][i];
for(int z=1;z<=n;z++)
for(int x=1;x<=n;x++)
G[x][z]=G[x-1][z]+inv[x][z];
for(int y=1;y<=n;y++)
for(int z=y+1;z<=n;z++)
F[y][z]=F[y][z-1]+G[y][z];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
BA[i][j]=BA[i][j-1]+inv[i][j];
AB[i][j]=AB[i][j-1]+inv[j][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]=suf[b][b+1]+cost(a,b);
}
suf[a][n+1]=INF;
for(int i=n;i>a;i--)
suf[a][i]=min(suf[a][i+1],dp[a][i]+AB[i][a]);
}
int ans=INF;
for(int i=1;i<=n;i++)
umin(ans,dp[0][i]);
printf("%d\n",ans);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Main.cpp:35:28: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | for(int i=1;i<=n;i++)scanf("%d",arr+i);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
98276 KB |
Output is correct |
2 |
Correct |
47 ms |
98312 KB |
Output is correct |
3 |
Correct |
42 ms |
98312 KB |
Output is correct |
4 |
Correct |
45 ms |
98416 KB |
Output is correct |
5 |
Correct |
42 ms |
98372 KB |
Output is correct |
6 |
Correct |
41 ms |
98456 KB |
Output is correct |
7 |
Correct |
42 ms |
98408 KB |
Output is correct |
8 |
Correct |
42 ms |
98436 KB |
Output is correct |
9 |
Correct |
41 ms |
98432 KB |
Output is correct |
10 |
Correct |
43 ms |
98500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
98276 KB |
Output is correct |
2 |
Correct |
47 ms |
98312 KB |
Output is correct |
3 |
Correct |
42 ms |
98312 KB |
Output is correct |
4 |
Correct |
45 ms |
98416 KB |
Output is correct |
5 |
Correct |
42 ms |
98372 KB |
Output is correct |
6 |
Correct |
41 ms |
98456 KB |
Output is correct |
7 |
Correct |
42 ms |
98408 KB |
Output is correct |
8 |
Correct |
42 ms |
98436 KB |
Output is correct |
9 |
Correct |
41 ms |
98432 KB |
Output is correct |
10 |
Correct |
43 ms |
98500 KB |
Output is correct |
11 |
Correct |
41 ms |
98816 KB |
Output is correct |
12 |
Correct |
43 ms |
98760 KB |
Output is correct |
13 |
Correct |
42 ms |
98784 KB |
Output is correct |
14 |
Correct |
42 ms |
98840 KB |
Output is correct |
15 |
Correct |
44 ms |
98888 KB |
Output is correct |
16 |
Correct |
42 ms |
98872 KB |
Output is correct |
17 |
Correct |
41 ms |
98884 KB |
Output is correct |
18 |
Correct |
44 ms |
98884 KB |
Output is correct |
19 |
Correct |
50 ms |
98884 KB |
Output is correct |
20 |
Correct |
43 ms |
98952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
98276 KB |
Output is correct |
2 |
Correct |
47 ms |
98312 KB |
Output is correct |
3 |
Correct |
42 ms |
98312 KB |
Output is correct |
4 |
Correct |
45 ms |
98416 KB |
Output is correct |
5 |
Correct |
42 ms |
98372 KB |
Output is correct |
6 |
Correct |
41 ms |
98456 KB |
Output is correct |
7 |
Correct |
42 ms |
98408 KB |
Output is correct |
8 |
Correct |
42 ms |
98436 KB |
Output is correct |
9 |
Correct |
41 ms |
98432 KB |
Output is correct |
10 |
Correct |
43 ms |
98500 KB |
Output is correct |
11 |
Correct |
41 ms |
98816 KB |
Output is correct |
12 |
Correct |
43 ms |
98760 KB |
Output is correct |
13 |
Correct |
42 ms |
98784 KB |
Output is correct |
14 |
Correct |
42 ms |
98840 KB |
Output is correct |
15 |
Correct |
44 ms |
98888 KB |
Output is correct |
16 |
Correct |
42 ms |
98872 KB |
Output is correct |
17 |
Correct |
41 ms |
98884 KB |
Output is correct |
18 |
Correct |
44 ms |
98884 KB |
Output is correct |
19 |
Correct |
50 ms |
98884 KB |
Output is correct |
20 |
Correct |
43 ms |
98952 KB |
Output is correct |
21 |
Correct |
45 ms |
105608 KB |
Output is correct |
22 |
Correct |
48 ms |
105572 KB |
Output is correct |
23 |
Correct |
46 ms |
105584 KB |
Output is correct |
24 |
Correct |
48 ms |
105676 KB |
Output is correct |
25 |
Correct |
45 ms |
105692 KB |
Output is correct |
26 |
Correct |
45 ms |
105668 KB |
Output is correct |
27 |
Correct |
46 ms |
105656 KB |
Output is correct |
28 |
Correct |
46 ms |
105688 KB |
Output is correct |
29 |
Correct |
45 ms |
105676 KB |
Output is correct |
30 |
Correct |
46 ms |
105760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
98276 KB |
Output is correct |
2 |
Correct |
47 ms |
98312 KB |
Output is correct |
3 |
Correct |
42 ms |
98312 KB |
Output is correct |
4 |
Correct |
45 ms |
98416 KB |
Output is correct |
5 |
Correct |
42 ms |
98372 KB |
Output is correct |
6 |
Correct |
41 ms |
98456 KB |
Output is correct |
7 |
Correct |
42 ms |
98408 KB |
Output is correct |
8 |
Correct |
42 ms |
98436 KB |
Output is correct |
9 |
Correct |
41 ms |
98432 KB |
Output is correct |
10 |
Correct |
43 ms |
98500 KB |
Output is correct |
11 |
Correct |
41 ms |
98816 KB |
Output is correct |
12 |
Correct |
43 ms |
98760 KB |
Output is correct |
13 |
Correct |
42 ms |
98784 KB |
Output is correct |
14 |
Correct |
42 ms |
98840 KB |
Output is correct |
15 |
Correct |
44 ms |
98888 KB |
Output is correct |
16 |
Correct |
42 ms |
98872 KB |
Output is correct |
17 |
Correct |
41 ms |
98884 KB |
Output is correct |
18 |
Correct |
44 ms |
98884 KB |
Output is correct |
19 |
Correct |
50 ms |
98884 KB |
Output is correct |
20 |
Correct |
43 ms |
98952 KB |
Output is correct |
21 |
Correct |
45 ms |
105608 KB |
Output is correct |
22 |
Correct |
48 ms |
105572 KB |
Output is correct |
23 |
Correct |
46 ms |
105584 KB |
Output is correct |
24 |
Correct |
48 ms |
105676 KB |
Output is correct |
25 |
Correct |
45 ms |
105692 KB |
Output is correct |
26 |
Correct |
45 ms |
105668 KB |
Output is correct |
27 |
Correct |
46 ms |
105656 KB |
Output is correct |
28 |
Correct |
46 ms |
105688 KB |
Output is correct |
29 |
Correct |
45 ms |
105676 KB |
Output is correct |
30 |
Correct |
46 ms |
105760 KB |
Output is correct |
31 |
Correct |
86 ms |
139968 KB |
Output is correct |
32 |
Correct |
85 ms |
140080 KB |
Output is correct |
33 |
Correct |
84 ms |
140100 KB |
Output is correct |
34 |
Correct |
86 ms |
140204 KB |
Output is correct |
35 |
Correct |
84 ms |
140264 KB |
Output is correct |
36 |
Correct |
85 ms |
140228 KB |
Output is correct |
37 |
Correct |
86 ms |
140168 KB |
Output is correct |
38 |
Correct |
96 ms |
140228 KB |
Output is correct |
39 |
Correct |
103 ms |
140208 KB |
Output is correct |
40 |
Correct |
86 ms |
140164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
98276 KB |
Output is correct |
2 |
Correct |
47 ms |
98312 KB |
Output is correct |
3 |
Correct |
42 ms |
98312 KB |
Output is correct |
4 |
Correct |
45 ms |
98416 KB |
Output is correct |
5 |
Correct |
42 ms |
98372 KB |
Output is correct |
6 |
Correct |
41 ms |
98456 KB |
Output is correct |
7 |
Correct |
42 ms |
98408 KB |
Output is correct |
8 |
Correct |
42 ms |
98436 KB |
Output is correct |
9 |
Correct |
41 ms |
98432 KB |
Output is correct |
10 |
Correct |
43 ms |
98500 KB |
Output is correct |
11 |
Correct |
41 ms |
98816 KB |
Output is correct |
12 |
Correct |
43 ms |
98760 KB |
Output is correct |
13 |
Correct |
42 ms |
98784 KB |
Output is correct |
14 |
Correct |
42 ms |
98840 KB |
Output is correct |
15 |
Correct |
44 ms |
98888 KB |
Output is correct |
16 |
Correct |
42 ms |
98872 KB |
Output is correct |
17 |
Correct |
41 ms |
98884 KB |
Output is correct |
18 |
Correct |
44 ms |
98884 KB |
Output is correct |
19 |
Correct |
50 ms |
98884 KB |
Output is correct |
20 |
Correct |
43 ms |
98952 KB |
Output is correct |
21 |
Correct |
45 ms |
105608 KB |
Output is correct |
22 |
Correct |
48 ms |
105572 KB |
Output is correct |
23 |
Correct |
46 ms |
105584 KB |
Output is correct |
24 |
Correct |
48 ms |
105676 KB |
Output is correct |
25 |
Correct |
45 ms |
105692 KB |
Output is correct |
26 |
Correct |
45 ms |
105668 KB |
Output is correct |
27 |
Correct |
46 ms |
105656 KB |
Output is correct |
28 |
Correct |
46 ms |
105688 KB |
Output is correct |
29 |
Correct |
45 ms |
105676 KB |
Output is correct |
30 |
Correct |
46 ms |
105760 KB |
Output is correct |
31 |
Correct |
86 ms |
139968 KB |
Output is correct |
32 |
Correct |
85 ms |
140080 KB |
Output is correct |
33 |
Correct |
84 ms |
140100 KB |
Output is correct |
34 |
Correct |
86 ms |
140204 KB |
Output is correct |
35 |
Correct |
84 ms |
140264 KB |
Output is correct |
36 |
Correct |
85 ms |
140228 KB |
Output is correct |
37 |
Correct |
86 ms |
140168 KB |
Output is correct |
38 |
Correct |
96 ms |
140228 KB |
Output is correct |
39 |
Correct |
103 ms |
140208 KB |
Output is correct |
40 |
Correct |
86 ms |
140164 KB |
Output is correct |
41 |
Correct |
2431 ms |
788092 KB |
Output is correct |
42 |
Correct |
2312 ms |
788164 KB |
Output is correct |
43 |
Correct |
2339 ms |
788340 KB |
Output is correct |
44 |
Correct |
2256 ms |
788316 KB |
Output is correct |
45 |
Correct |
2292 ms |
788292 KB |
Output is correct |
46 |
Correct |
2272 ms |
788632 KB |
Output is correct |
47 |
Correct |
2257 ms |
788520 KB |
Output is correct |
48 |
Correct |
2243 ms |
788520 KB |
Output is correct |
49 |
Correct |
2283 ms |
788564 KB |
Output is correct |
50 |
Correct |
2277 ms |
788420 KB |
Output is correct |
51 |
Correct |
2284 ms |
788692 KB |
Output is correct |
52 |
Correct |
2299 ms |
788564 KB |
Output is correct |
53 |
Correct |
2304 ms |
788516 KB |
Output is correct |
54 |
Correct |
2304 ms |
788432 KB |
Output is correct |
55 |
Correct |
2262 ms |
788568 KB |
Output is correct |