#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int,int>
#define X first
#define Y second
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define abs(x) ((x)>0?(x):-(x))
#define pb push_back
int n,tab[5005][5005],stab[5005][5005],a[5005],b[5005],dp[5005];
int qry(int l1,int r1,int l2,int r2){
//cout<<l1<<" "<<r1<<" "<<l2<<" "<<r2<<endl;
//cout<<stab[r1][r2]<<" "<<stab[r1][l2-1]<<" "<<stab[l1-1][r2]<<" "<<stab[l1-1][l2-1]<<endl;
return stab[r1][r2]-stab[r1][l2-1]-stab[l1-1][r2]+stab[l1-1][l2-1];
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
F(n)cin>>a[i],b[a[i]]=i;
int s=0;
memset(tab,0,sizeof(tab));
F(n)for(int j=i+1;j<n;j++){
if(b[i+1]<b[j+1])s++,tab[i+1][j+1]=0;
else tab[i+1][j+1]=1;
}
for(int i=0;i<=n;i++)stab[i][0]=stab[0][i]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)stab[i][j]=tab[i][j]+stab[i-1][j]+stab[i][j-1]-stab[i-1][j-1];
}
/*F(n+1){
Fi(j,n+1)cout<<stab[i][j]<<" ";
cout<<endl;
}*/
dp[n]=s;
for(int i=n-1;i>=1;i--){
dp[i]=1e9;
for(int j=i+1;j<=n;j++){
int tt=qry(1,i,i+1,j);
dp[i]=min(dp[i],dp[j]-(i*(j-i))+tt*2);
}
}
/*int no=n;
for(int i=n-1;i>=1;i--){
int tt=qry(1,i,i+1,no);
cout<<tt<<" "<<(i*(no-i))-tt<<endl;
if(tt<(i*(no-i))-tt){
s-=((i*(no-i))-tt*2);
no=i;
}
cout<<s<<endl;
}*/
s=1e9;
F(n)s=min(dp[i+1],s);
cout<<s<<endl;
return 0;
}
/*
5 4
5 3
5 1
4 3
4 1
3 1
2 1
8 7
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
196416 KB |
Output is correct |
2 |
Correct |
86 ms |
196416 KB |
Output is correct |
3 |
Correct |
83 ms |
196364 KB |
Output is correct |
4 |
Correct |
80 ms |
196320 KB |
Output is correct |
5 |
Correct |
83 ms |
196384 KB |
Output is correct |
6 |
Correct |
106 ms |
196320 KB |
Output is correct |
7 |
Correct |
96 ms |
196432 KB |
Output is correct |
8 |
Correct |
81 ms |
196400 KB |
Output is correct |
9 |
Correct |
77 ms |
196352 KB |
Output is correct |
10 |
Correct |
79 ms |
196336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
196416 KB |
Output is correct |
2 |
Correct |
86 ms |
196416 KB |
Output is correct |
3 |
Correct |
83 ms |
196364 KB |
Output is correct |
4 |
Correct |
80 ms |
196320 KB |
Output is correct |
5 |
Correct |
83 ms |
196384 KB |
Output is correct |
6 |
Correct |
106 ms |
196320 KB |
Output is correct |
7 |
Correct |
96 ms |
196432 KB |
Output is correct |
8 |
Correct |
81 ms |
196400 KB |
Output is correct |
9 |
Correct |
77 ms |
196352 KB |
Output is correct |
10 |
Correct |
79 ms |
196336 KB |
Output is correct |
11 |
Correct |
84 ms |
196356 KB |
Output is correct |
12 |
Correct |
90 ms |
196460 KB |
Output is correct |
13 |
Correct |
96 ms |
196476 KB |
Output is correct |
14 |
Correct |
78 ms |
196380 KB |
Output is correct |
15 |
Correct |
85 ms |
196384 KB |
Output is correct |
16 |
Correct |
76 ms |
196532 KB |
Output is correct |
17 |
Correct |
88 ms |
196488 KB |
Output is correct |
18 |
Correct |
91 ms |
196476 KB |
Output is correct |
19 |
Correct |
97 ms |
196432 KB |
Output is correct |
20 |
Correct |
78 ms |
196424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
196416 KB |
Output is correct |
2 |
Correct |
86 ms |
196416 KB |
Output is correct |
3 |
Correct |
83 ms |
196364 KB |
Output is correct |
4 |
Correct |
80 ms |
196320 KB |
Output is correct |
5 |
Correct |
83 ms |
196384 KB |
Output is correct |
6 |
Correct |
106 ms |
196320 KB |
Output is correct |
7 |
Correct |
96 ms |
196432 KB |
Output is correct |
8 |
Correct |
81 ms |
196400 KB |
Output is correct |
9 |
Correct |
77 ms |
196352 KB |
Output is correct |
10 |
Correct |
79 ms |
196336 KB |
Output is correct |
11 |
Correct |
84 ms |
196356 KB |
Output is correct |
12 |
Correct |
90 ms |
196460 KB |
Output is correct |
13 |
Correct |
96 ms |
196476 KB |
Output is correct |
14 |
Correct |
78 ms |
196380 KB |
Output is correct |
15 |
Correct |
85 ms |
196384 KB |
Output is correct |
16 |
Correct |
76 ms |
196532 KB |
Output is correct |
17 |
Correct |
88 ms |
196488 KB |
Output is correct |
18 |
Correct |
91 ms |
196476 KB |
Output is correct |
19 |
Correct |
97 ms |
196432 KB |
Output is correct |
20 |
Correct |
78 ms |
196424 KB |
Output is correct |
21 |
Correct |
79 ms |
197444 KB |
Output is correct |
22 |
Correct |
82 ms |
197476 KB |
Output is correct |
23 |
Correct |
78 ms |
197448 KB |
Output is correct |
24 |
Correct |
87 ms |
197432 KB |
Output is correct |
25 |
Correct |
89 ms |
197448 KB |
Output is correct |
26 |
Correct |
97 ms |
197440 KB |
Output is correct |
27 |
Correct |
81 ms |
197520 KB |
Output is correct |
28 |
Correct |
81 ms |
197420 KB |
Output is correct |
29 |
Correct |
92 ms |
197548 KB |
Output is correct |
30 |
Correct |
100 ms |
197456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
196416 KB |
Output is correct |
2 |
Correct |
86 ms |
196416 KB |
Output is correct |
3 |
Correct |
83 ms |
196364 KB |
Output is correct |
4 |
Correct |
80 ms |
196320 KB |
Output is correct |
5 |
Correct |
83 ms |
196384 KB |
Output is correct |
6 |
Correct |
106 ms |
196320 KB |
Output is correct |
7 |
Correct |
96 ms |
196432 KB |
Output is correct |
8 |
Correct |
81 ms |
196400 KB |
Output is correct |
9 |
Correct |
77 ms |
196352 KB |
Output is correct |
10 |
Correct |
79 ms |
196336 KB |
Output is correct |
11 |
Correct |
84 ms |
196356 KB |
Output is correct |
12 |
Correct |
90 ms |
196460 KB |
Output is correct |
13 |
Correct |
96 ms |
196476 KB |
Output is correct |
14 |
Correct |
78 ms |
196380 KB |
Output is correct |
15 |
Correct |
85 ms |
196384 KB |
Output is correct |
16 |
Correct |
76 ms |
196532 KB |
Output is correct |
17 |
Correct |
88 ms |
196488 KB |
Output is correct |
18 |
Correct |
91 ms |
196476 KB |
Output is correct |
19 |
Correct |
97 ms |
196432 KB |
Output is correct |
20 |
Correct |
78 ms |
196424 KB |
Output is correct |
21 |
Correct |
79 ms |
197444 KB |
Output is correct |
22 |
Correct |
82 ms |
197476 KB |
Output is correct |
23 |
Correct |
78 ms |
197448 KB |
Output is correct |
24 |
Correct |
87 ms |
197432 KB |
Output is correct |
25 |
Correct |
89 ms |
197448 KB |
Output is correct |
26 |
Correct |
97 ms |
197440 KB |
Output is correct |
27 |
Correct |
81 ms |
197520 KB |
Output is correct |
28 |
Correct |
81 ms |
197420 KB |
Output is correct |
29 |
Correct |
92 ms |
197548 KB |
Output is correct |
30 |
Correct |
100 ms |
197456 KB |
Output is correct |
31 |
Correct |
89 ms |
204540 KB |
Output is correct |
32 |
Correct |
86 ms |
204668 KB |
Output is correct |
33 |
Correct |
83 ms |
204552 KB |
Output is correct |
34 |
Correct |
86 ms |
204616 KB |
Output is correct |
35 |
Correct |
90 ms |
204640 KB |
Output is correct |
36 |
Correct |
98 ms |
204780 KB |
Output is correct |
37 |
Correct |
97 ms |
204608 KB |
Output is correct |
38 |
Correct |
87 ms |
204644 KB |
Output is correct |
39 |
Correct |
89 ms |
204612 KB |
Output is correct |
40 |
Correct |
107 ms |
204688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
196416 KB |
Output is correct |
2 |
Correct |
86 ms |
196416 KB |
Output is correct |
3 |
Correct |
83 ms |
196364 KB |
Output is correct |
4 |
Correct |
80 ms |
196320 KB |
Output is correct |
5 |
Correct |
83 ms |
196384 KB |
Output is correct |
6 |
Correct |
106 ms |
196320 KB |
Output is correct |
7 |
Correct |
96 ms |
196432 KB |
Output is correct |
8 |
Correct |
81 ms |
196400 KB |
Output is correct |
9 |
Correct |
77 ms |
196352 KB |
Output is correct |
10 |
Correct |
79 ms |
196336 KB |
Output is correct |
11 |
Correct |
84 ms |
196356 KB |
Output is correct |
12 |
Correct |
90 ms |
196460 KB |
Output is correct |
13 |
Correct |
96 ms |
196476 KB |
Output is correct |
14 |
Correct |
78 ms |
196380 KB |
Output is correct |
15 |
Correct |
85 ms |
196384 KB |
Output is correct |
16 |
Correct |
76 ms |
196532 KB |
Output is correct |
17 |
Correct |
88 ms |
196488 KB |
Output is correct |
18 |
Correct |
91 ms |
196476 KB |
Output is correct |
19 |
Correct |
97 ms |
196432 KB |
Output is correct |
20 |
Correct |
78 ms |
196424 KB |
Output is correct |
21 |
Correct |
79 ms |
197444 KB |
Output is correct |
22 |
Correct |
82 ms |
197476 KB |
Output is correct |
23 |
Correct |
78 ms |
197448 KB |
Output is correct |
24 |
Correct |
87 ms |
197432 KB |
Output is correct |
25 |
Correct |
89 ms |
197448 KB |
Output is correct |
26 |
Correct |
97 ms |
197440 KB |
Output is correct |
27 |
Correct |
81 ms |
197520 KB |
Output is correct |
28 |
Correct |
81 ms |
197420 KB |
Output is correct |
29 |
Correct |
92 ms |
197548 KB |
Output is correct |
30 |
Correct |
100 ms |
197456 KB |
Output is correct |
31 |
Correct |
89 ms |
204540 KB |
Output is correct |
32 |
Correct |
86 ms |
204668 KB |
Output is correct |
33 |
Correct |
83 ms |
204552 KB |
Output is correct |
34 |
Correct |
86 ms |
204616 KB |
Output is correct |
35 |
Correct |
90 ms |
204640 KB |
Output is correct |
36 |
Correct |
98 ms |
204780 KB |
Output is correct |
37 |
Correct |
97 ms |
204608 KB |
Output is correct |
38 |
Correct |
87 ms |
204644 KB |
Output is correct |
39 |
Correct |
89 ms |
204612 KB |
Output is correct |
40 |
Correct |
107 ms |
204688 KB |
Output is correct |
41 |
Correct |
298 ms |
392328 KB |
Output is correct |
42 |
Correct |
314 ms |
392428 KB |
Output is correct |
43 |
Correct |
278 ms |
392392 KB |
Output is correct |
44 |
Correct |
306 ms |
392300 KB |
Output is correct |
45 |
Correct |
293 ms |
392336 KB |
Output is correct |
46 |
Correct |
420 ms |
392392 KB |
Output is correct |
47 |
Correct |
294 ms |
392460 KB |
Output is correct |
48 |
Correct |
277 ms |
392336 KB |
Output is correct |
49 |
Correct |
299 ms |
392356 KB |
Output is correct |
50 |
Correct |
275 ms |
392464 KB |
Output is correct |
51 |
Correct |
338 ms |
392380 KB |
Output is correct |
52 |
Correct |
285 ms |
392352 KB |
Output is correct |
53 |
Correct |
277 ms |
392444 KB |
Output is correct |
54 |
Correct |
323 ms |
392496 KB |
Output is correct |
55 |
Correct |
337 ms |
392436 KB |
Output is correct |