// 還要更強
#include<iostream>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<math.h>
#include<cstring>
#include<stack>
#include<string.h>
#include<random>
#include<algorithm>
#include<vector>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i>=b;i--)
#define S second
#define F first
#define INF (ll)(3e18)
#define pb push_back
#define AC ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int n,h[5005],pos[5005],pre[2][5005][5005],dp[5005];
bool ni[2][5005][5005];
signed main(){
AC;
cin>>n;
FOR(i,1,n+1){
cin>>h[i]; pos[h[i]]=i;
}
FOR(i,1,n+1)FOR(j,1,i){
if(pos[i]<pos[j]) ni[0][i][j]=1;
else ni[1][i][j]=1;
}
FOR(i,1,n+1)FOR(j,1,n+1){
pre[0][i][j]=pre[0][i-1][j]+pre[0][i][j-1]-pre[0][i-1][j-1]+ni[0][i][j];
pre[1][i][j]=pre[1][i-1][j]+pre[1][i][j-1]-pre[1][i-1][j-1]+ni[1][i][j];
//cout<<"pre["<<i<<"]["<<j<<"]="<<pre[i][j]<<endl;
}
dp[1]=0;
FOR(i,2,n+1){
dp[i]=pre[1][i][i];
FOR(j,1,i){
int tmp=pre[0][i][j]-pre[0][j][j],tmp2=pre[1][i][i]-pre[1][i][j]-pre[1][j][i]+pre[1][j][j];
//cout<<"i="<<i<<" j="<<j<<" tmp="<<tmp<<" tmp2="<<tmp2<<endl;
dp[i]=min(dp[i],tmp+tmp2+dp[j]);
}
//cout<<"dp["<<i<<"]="<<dp[i]<<endl;
}
cout<<dp[n]<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
448 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
448 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
0 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
448 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
0 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
3788 KB |
Output is correct |
22 |
Correct |
3 ms |
3788 KB |
Output is correct |
23 |
Correct |
2 ms |
3788 KB |
Output is correct |
24 |
Correct |
2 ms |
3776 KB |
Output is correct |
25 |
Correct |
3 ms |
3788 KB |
Output is correct |
26 |
Correct |
2 ms |
3788 KB |
Output is correct |
27 |
Correct |
2 ms |
3788 KB |
Output is correct |
28 |
Correct |
2 ms |
3788 KB |
Output is correct |
29 |
Correct |
2 ms |
3776 KB |
Output is correct |
30 |
Correct |
2 ms |
3788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
448 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
0 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
3788 KB |
Output is correct |
22 |
Correct |
3 ms |
3788 KB |
Output is correct |
23 |
Correct |
2 ms |
3788 KB |
Output is correct |
24 |
Correct |
2 ms |
3776 KB |
Output is correct |
25 |
Correct |
3 ms |
3788 KB |
Output is correct |
26 |
Correct |
2 ms |
3788 KB |
Output is correct |
27 |
Correct |
2 ms |
3788 KB |
Output is correct |
28 |
Correct |
2 ms |
3788 KB |
Output is correct |
29 |
Correct |
2 ms |
3776 KB |
Output is correct |
30 |
Correct |
2 ms |
3788 KB |
Output is correct |
31 |
Correct |
14 ms |
18628 KB |
Output is correct |
32 |
Correct |
14 ms |
18636 KB |
Output is correct |
33 |
Correct |
15 ms |
18628 KB |
Output is correct |
34 |
Correct |
15 ms |
18756 KB |
Output is correct |
35 |
Correct |
14 ms |
18764 KB |
Output is correct |
36 |
Correct |
15 ms |
18764 KB |
Output is correct |
37 |
Correct |
17 ms |
18616 KB |
Output is correct |
38 |
Correct |
15 ms |
18796 KB |
Output is correct |
39 |
Correct |
15 ms |
18688 KB |
Output is correct |
40 |
Correct |
15 ms |
18636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
448 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
0 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
3788 KB |
Output is correct |
22 |
Correct |
3 ms |
3788 KB |
Output is correct |
23 |
Correct |
2 ms |
3788 KB |
Output is correct |
24 |
Correct |
2 ms |
3776 KB |
Output is correct |
25 |
Correct |
3 ms |
3788 KB |
Output is correct |
26 |
Correct |
2 ms |
3788 KB |
Output is correct |
27 |
Correct |
2 ms |
3788 KB |
Output is correct |
28 |
Correct |
2 ms |
3788 KB |
Output is correct |
29 |
Correct |
2 ms |
3776 KB |
Output is correct |
30 |
Correct |
2 ms |
3788 KB |
Output is correct |
31 |
Correct |
14 ms |
18628 KB |
Output is correct |
32 |
Correct |
14 ms |
18636 KB |
Output is correct |
33 |
Correct |
15 ms |
18628 KB |
Output is correct |
34 |
Correct |
15 ms |
18756 KB |
Output is correct |
35 |
Correct |
14 ms |
18764 KB |
Output is correct |
36 |
Correct |
15 ms |
18764 KB |
Output is correct |
37 |
Correct |
17 ms |
18616 KB |
Output is correct |
38 |
Correct |
15 ms |
18796 KB |
Output is correct |
39 |
Correct |
15 ms |
18688 KB |
Output is correct |
40 |
Correct |
15 ms |
18636 KB |
Output is correct |
41 |
Correct |
458 ms |
244052 KB |
Output is correct |
42 |
Correct |
492 ms |
244252 KB |
Output is correct |
43 |
Correct |
494 ms |
244284 KB |
Output is correct |
44 |
Correct |
497 ms |
244152 KB |
Output is correct |
45 |
Correct |
503 ms |
244420 KB |
Output is correct |
46 |
Correct |
482 ms |
244296 KB |
Output is correct |
47 |
Correct |
487 ms |
244412 KB |
Output is correct |
48 |
Correct |
479 ms |
244420 KB |
Output is correct |
49 |
Correct |
497 ms |
244340 KB |
Output is correct |
50 |
Correct |
487 ms |
244456 KB |
Output is correct |
51 |
Correct |
480 ms |
244248 KB |
Output is correct |
52 |
Correct |
491 ms |
244332 KB |
Output is correct |
53 |
Correct |
492 ms |
244304 KB |
Output is correct |
54 |
Correct |
507 ms |
244292 KB |
Output is correct |
55 |
Correct |
500 ms |
244524 KB |
Output is correct |