#include<bits/stdc++.h>
using namespace std;
const int N = 5e3;
const int inf = 2e9;
int v[N],pos[N];
int dp[N];
int fen[N][3];
int n;
void add(int pos,int x,int id){
for(int i = pos;i < n;i|=(i + 1)){
fen[i][id]+=x;
}
}
int get(int pos,int id){
int r = 0;
for(int i = pos;i >= 0;i&=(i + 1),i--){
r+=fen[i][id];
}
return r;
}
int main(){
cin>>n;
for(int i = 0;i < n;i++){
cin>>v[i];
v[i]--;
pos[v[i]] = i;
dp[i] = inf;
}
int sum2 = 0;
for(int i = n - 1;i >= 0;i--){
sum2+=get(n - 1 - pos[i],0);
dp[i] = min(dp[i],sum2);
//cout<<sum2<<' ';
add(n - 1 - pos[i],1,0);
add(pos[i],1,2);
int sum = 0;
for(int j = i - 1;j >= 0;j--){
sum+=get(pos[j],2);
sum+=get(n - 1 - pos[j],1);
add(n - 1 - pos[j],1,1);
//cout<<j<<' '<<i<<' '<<sum<<'\n';
dp[j] = min(dp[j], dp[i] + sum);
}
for(int j = i - 1;j >= 0;j--){
add(n - 1 - pos[j],-1,1);
}
//cout<<dp[i]<<'\n';
}
cout<<dp[0];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
444 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
444 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
444 KB |
Output is correct |
13 |
Correct |
0 ms |
444 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
372 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
444 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
444 KB |
Output is correct |
13 |
Correct |
0 ms |
444 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
372 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
444 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
444 KB |
Output is correct |
13 |
Correct |
0 ms |
444 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
372 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
5 ms |
348 KB |
Output is correct |
32 |
Correct |
5 ms |
476 KB |
Output is correct |
33 |
Correct |
6 ms |
440 KB |
Output is correct |
34 |
Correct |
5 ms |
440 KB |
Output is correct |
35 |
Correct |
7 ms |
348 KB |
Output is correct |
36 |
Correct |
6 ms |
476 KB |
Output is correct |
37 |
Correct |
5 ms |
472 KB |
Output is correct |
38 |
Correct |
6 ms |
348 KB |
Output is correct |
39 |
Correct |
5 ms |
348 KB |
Output is correct |
40 |
Correct |
7 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
444 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
444 KB |
Output is correct |
13 |
Correct |
0 ms |
444 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
372 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
5 ms |
348 KB |
Output is correct |
32 |
Correct |
5 ms |
476 KB |
Output is correct |
33 |
Correct |
6 ms |
440 KB |
Output is correct |
34 |
Correct |
5 ms |
440 KB |
Output is correct |
35 |
Correct |
7 ms |
348 KB |
Output is correct |
36 |
Correct |
6 ms |
476 KB |
Output is correct |
37 |
Correct |
5 ms |
472 KB |
Output is correct |
38 |
Correct |
6 ms |
348 KB |
Output is correct |
39 |
Correct |
5 ms |
348 KB |
Output is correct |
40 |
Correct |
7 ms |
468 KB |
Output is correct |
41 |
Correct |
425 ms |
556 KB |
Output is correct |
42 |
Correct |
457 ms |
556 KB |
Output is correct |
43 |
Correct |
430 ms |
348 KB |
Output is correct |
44 |
Correct |
428 ms |
552 KB |
Output is correct |
45 |
Correct |
455 ms |
552 KB |
Output is correct |
46 |
Correct |
431 ms |
348 KB |
Output is correct |
47 |
Correct |
442 ms |
592 KB |
Output is correct |
48 |
Correct |
432 ms |
600 KB |
Output is correct |
49 |
Correct |
452 ms |
856 KB |
Output is correct |
50 |
Correct |
437 ms |
348 KB |
Output is correct |
51 |
Correct |
436 ms |
552 KB |
Output is correct |
52 |
Correct |
434 ms |
348 KB |
Output is correct |
53 |
Correct |
432 ms |
552 KB |
Output is correct |
54 |
Correct |
433 ms |
344 KB |
Output is correct |
55 |
Correct |
434 ms |
568 KB |
Output is correct |