#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],rap[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];
rap[j][i]=rap[j][i-1]+inv[i][j];
}
//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]+rap[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>a;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
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);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
98468 KB |
Output is correct |
2 |
Correct |
53 ms |
98400 KB |
Output is correct |
3 |
Correct |
48 ms |
98440 KB |
Output is correct |
4 |
Correct |
46 ms |
98500 KB |
Output is correct |
5 |
Correct |
46 ms |
98532 KB |
Output is correct |
6 |
Correct |
46 ms |
98584 KB |
Output is correct |
7 |
Correct |
54 ms |
98604 KB |
Output is correct |
8 |
Correct |
47 ms |
98628 KB |
Output is correct |
9 |
Correct |
47 ms |
98756 KB |
Output is correct |
10 |
Correct |
48 ms |
98628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
98468 KB |
Output is correct |
2 |
Correct |
53 ms |
98400 KB |
Output is correct |
3 |
Correct |
48 ms |
98440 KB |
Output is correct |
4 |
Correct |
46 ms |
98500 KB |
Output is correct |
5 |
Correct |
46 ms |
98532 KB |
Output is correct |
6 |
Correct |
46 ms |
98584 KB |
Output is correct |
7 |
Correct |
54 ms |
98604 KB |
Output is correct |
8 |
Correct |
47 ms |
98628 KB |
Output is correct |
9 |
Correct |
47 ms |
98756 KB |
Output is correct |
10 |
Correct |
48 ms |
98628 KB |
Output is correct |
11 |
Correct |
47 ms |
98872 KB |
Output is correct |
12 |
Correct |
48 ms |
98884 KB |
Output is correct |
13 |
Correct |
47 ms |
98876 KB |
Output is correct |
14 |
Correct |
47 ms |
98884 KB |
Output is correct |
15 |
Correct |
51 ms |
98904 KB |
Output is correct |
16 |
Correct |
49 ms |
98992 KB |
Output is correct |
17 |
Correct |
47 ms |
98936 KB |
Output is correct |
18 |
Correct |
50 ms |
98892 KB |
Output is correct |
19 |
Correct |
54 ms |
98992 KB |
Output is correct |
20 |
Correct |
48 ms |
98920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
98468 KB |
Output is correct |
2 |
Correct |
53 ms |
98400 KB |
Output is correct |
3 |
Correct |
48 ms |
98440 KB |
Output is correct |
4 |
Correct |
46 ms |
98500 KB |
Output is correct |
5 |
Correct |
46 ms |
98532 KB |
Output is correct |
6 |
Correct |
46 ms |
98584 KB |
Output is correct |
7 |
Correct |
54 ms |
98604 KB |
Output is correct |
8 |
Correct |
47 ms |
98628 KB |
Output is correct |
9 |
Correct |
47 ms |
98756 KB |
Output is correct |
10 |
Correct |
48 ms |
98628 KB |
Output is correct |
11 |
Correct |
47 ms |
98872 KB |
Output is correct |
12 |
Correct |
48 ms |
98884 KB |
Output is correct |
13 |
Correct |
47 ms |
98876 KB |
Output is correct |
14 |
Correct |
47 ms |
98884 KB |
Output is correct |
15 |
Correct |
51 ms |
98904 KB |
Output is correct |
16 |
Correct |
49 ms |
98992 KB |
Output is correct |
17 |
Correct |
47 ms |
98936 KB |
Output is correct |
18 |
Correct |
50 ms |
98892 KB |
Output is correct |
19 |
Correct |
54 ms |
98992 KB |
Output is correct |
20 |
Correct |
48 ms |
98920 KB |
Output is correct |
21 |
Correct |
51 ms |
105640 KB |
Output is correct |
22 |
Correct |
52 ms |
105800 KB |
Output is correct |
23 |
Correct |
52 ms |
105804 KB |
Output is correct |
24 |
Correct |
54 ms |
105656 KB |
Output is correct |
25 |
Correct |
52 ms |
105824 KB |
Output is correct |
26 |
Correct |
51 ms |
105656 KB |
Output is correct |
27 |
Correct |
51 ms |
105716 KB |
Output is correct |
28 |
Correct |
51 ms |
105664 KB |
Output is correct |
29 |
Correct |
52 ms |
105812 KB |
Output is correct |
30 |
Correct |
52 ms |
105832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
98468 KB |
Output is correct |
2 |
Correct |
53 ms |
98400 KB |
Output is correct |
3 |
Correct |
48 ms |
98440 KB |
Output is correct |
4 |
Correct |
46 ms |
98500 KB |
Output is correct |
5 |
Correct |
46 ms |
98532 KB |
Output is correct |
6 |
Correct |
46 ms |
98584 KB |
Output is correct |
7 |
Correct |
54 ms |
98604 KB |
Output is correct |
8 |
Correct |
47 ms |
98628 KB |
Output is correct |
9 |
Correct |
47 ms |
98756 KB |
Output is correct |
10 |
Correct |
48 ms |
98628 KB |
Output is correct |
11 |
Correct |
47 ms |
98872 KB |
Output is correct |
12 |
Correct |
48 ms |
98884 KB |
Output is correct |
13 |
Correct |
47 ms |
98876 KB |
Output is correct |
14 |
Correct |
47 ms |
98884 KB |
Output is correct |
15 |
Correct |
51 ms |
98904 KB |
Output is correct |
16 |
Correct |
49 ms |
98992 KB |
Output is correct |
17 |
Correct |
47 ms |
98936 KB |
Output is correct |
18 |
Correct |
50 ms |
98892 KB |
Output is correct |
19 |
Correct |
54 ms |
98992 KB |
Output is correct |
20 |
Correct |
48 ms |
98920 KB |
Output is correct |
21 |
Correct |
51 ms |
105640 KB |
Output is correct |
22 |
Correct |
52 ms |
105800 KB |
Output is correct |
23 |
Correct |
52 ms |
105804 KB |
Output is correct |
24 |
Correct |
54 ms |
105656 KB |
Output is correct |
25 |
Correct |
52 ms |
105824 KB |
Output is correct |
26 |
Correct |
51 ms |
105656 KB |
Output is correct |
27 |
Correct |
51 ms |
105716 KB |
Output is correct |
28 |
Correct |
51 ms |
105664 KB |
Output is correct |
29 |
Correct |
52 ms |
105812 KB |
Output is correct |
30 |
Correct |
52 ms |
105832 KB |
Output is correct |
31 |
Correct |
91 ms |
140100 KB |
Output is correct |
32 |
Correct |
88 ms |
140244 KB |
Output is correct |
33 |
Correct |
89 ms |
140260 KB |
Output is correct |
34 |
Correct |
92 ms |
140404 KB |
Output is correct |
35 |
Correct |
89 ms |
140428 KB |
Output is correct |
36 |
Correct |
86 ms |
140356 KB |
Output is correct |
37 |
Correct |
98 ms |
140240 KB |
Output is correct |
38 |
Correct |
88 ms |
140348 KB |
Output is correct |
39 |
Correct |
97 ms |
140356 KB |
Output is correct |
40 |
Correct |
90 ms |
140296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
98468 KB |
Output is correct |
2 |
Correct |
53 ms |
98400 KB |
Output is correct |
3 |
Correct |
48 ms |
98440 KB |
Output is correct |
4 |
Correct |
46 ms |
98500 KB |
Output is correct |
5 |
Correct |
46 ms |
98532 KB |
Output is correct |
6 |
Correct |
46 ms |
98584 KB |
Output is correct |
7 |
Correct |
54 ms |
98604 KB |
Output is correct |
8 |
Correct |
47 ms |
98628 KB |
Output is correct |
9 |
Correct |
47 ms |
98756 KB |
Output is correct |
10 |
Correct |
48 ms |
98628 KB |
Output is correct |
11 |
Correct |
47 ms |
98872 KB |
Output is correct |
12 |
Correct |
48 ms |
98884 KB |
Output is correct |
13 |
Correct |
47 ms |
98876 KB |
Output is correct |
14 |
Correct |
47 ms |
98884 KB |
Output is correct |
15 |
Correct |
51 ms |
98904 KB |
Output is correct |
16 |
Correct |
49 ms |
98992 KB |
Output is correct |
17 |
Correct |
47 ms |
98936 KB |
Output is correct |
18 |
Correct |
50 ms |
98892 KB |
Output is correct |
19 |
Correct |
54 ms |
98992 KB |
Output is correct |
20 |
Correct |
48 ms |
98920 KB |
Output is correct |
21 |
Correct |
51 ms |
105640 KB |
Output is correct |
22 |
Correct |
52 ms |
105800 KB |
Output is correct |
23 |
Correct |
52 ms |
105804 KB |
Output is correct |
24 |
Correct |
54 ms |
105656 KB |
Output is correct |
25 |
Correct |
52 ms |
105824 KB |
Output is correct |
26 |
Correct |
51 ms |
105656 KB |
Output is correct |
27 |
Correct |
51 ms |
105716 KB |
Output is correct |
28 |
Correct |
51 ms |
105664 KB |
Output is correct |
29 |
Correct |
52 ms |
105812 KB |
Output is correct |
30 |
Correct |
52 ms |
105832 KB |
Output is correct |
31 |
Correct |
91 ms |
140100 KB |
Output is correct |
32 |
Correct |
88 ms |
140244 KB |
Output is correct |
33 |
Correct |
89 ms |
140260 KB |
Output is correct |
34 |
Correct |
92 ms |
140404 KB |
Output is correct |
35 |
Correct |
89 ms |
140428 KB |
Output is correct |
36 |
Correct |
86 ms |
140356 KB |
Output is correct |
37 |
Correct |
98 ms |
140240 KB |
Output is correct |
38 |
Correct |
88 ms |
140348 KB |
Output is correct |
39 |
Correct |
97 ms |
140356 KB |
Output is correct |
40 |
Correct |
90 ms |
140296 KB |
Output is correct |
41 |
Correct |
2009 ms |
788436 KB |
Output is correct |
42 |
Correct |
2058 ms |
788676 KB |
Output is correct |
43 |
Correct |
2069 ms |
788900 KB |
Output is correct |
44 |
Correct |
2045 ms |
788944 KB |
Output is correct |
45 |
Correct |
2075 ms |
788960 KB |
Output is correct |
46 |
Correct |
2087 ms |
789020 KB |
Output is correct |
47 |
Correct |
2040 ms |
788956 KB |
Output is correct |
48 |
Correct |
2036 ms |
789000 KB |
Output is correct |
49 |
Correct |
2025 ms |
788920 KB |
Output is correct |
50 |
Correct |
2035 ms |
788880 KB |
Output is correct |
51 |
Correct |
2031 ms |
789080 KB |
Output is correct |
52 |
Correct |
2082 ms |
788980 KB |
Output is correct |
53 |
Correct |
2040 ms |
788936 KB |
Output is correct |
54 |
Correct |
2040 ms |
788972 KB |
Output is correct |
55 |
Correct |
2037 ms |
788996 KB |
Output is correct |