#include<bits/stdc++.h>
#define f first
#define int long long
#define s second
using namespace std;
const int N=5005,mod=1e9+7,Inf=1e9;
int t,ind[N],dp[N],inv[N][N],p[N][N],n,a[N],I[N][N];
string s;
main(){
// t=1;
cin >> n;
for(int i=1;i<=n;i++){
cin>>a[i];
ind[a[i]] = i;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) {
inv[i][j] = inv[i][j-1] + (ind[j]>ind[i]);
p[i][j] = p[i-1][j] + inv[i][j];
}
}
for(int i=1;i<=n;i++) {
dp[i] = Inf;
for(int j=i;j<=n;j++){
I[i][j] = I[i][j-1] + (j-i) - (inv[j][j] - inv[j][i-1]);
}
}
for(int i=1;i<=n;i++) {
for(int j=i;j<=n;j++) {
// i-1>>>>>1 j>>>>>i
dp[j] = min(dp[j],dp[i-1] + (p[j][i-1] - p[i-1][i-1]) + I[i][j] );
}
}
cout<<dp[n];
}
Compilation message
Main.cpp:9:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
9 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 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 |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 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 |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 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 |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
3 ms |
3404 KB |
Output is correct |
22 |
Correct |
5 ms |
3532 KB |
Output is correct |
23 |
Correct |
3 ms |
3532 KB |
Output is correct |
24 |
Correct |
4 ms |
3532 KB |
Output is correct |
25 |
Correct |
4 ms |
3532 KB |
Output is correct |
26 |
Correct |
4 ms |
3488 KB |
Output is correct |
27 |
Correct |
4 ms |
3532 KB |
Output is correct |
28 |
Correct |
5 ms |
3532 KB |
Output is correct |
29 |
Correct |
3 ms |
3532 KB |
Output is correct |
30 |
Correct |
3 ms |
3532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 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 |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
3 ms |
3404 KB |
Output is correct |
22 |
Correct |
5 ms |
3532 KB |
Output is correct |
23 |
Correct |
3 ms |
3532 KB |
Output is correct |
24 |
Correct |
4 ms |
3532 KB |
Output is correct |
25 |
Correct |
4 ms |
3532 KB |
Output is correct |
26 |
Correct |
4 ms |
3488 KB |
Output is correct |
27 |
Correct |
4 ms |
3532 KB |
Output is correct |
28 |
Correct |
5 ms |
3532 KB |
Output is correct |
29 |
Correct |
3 ms |
3532 KB |
Output is correct |
30 |
Correct |
3 ms |
3532 KB |
Output is correct |
31 |
Correct |
21 ms |
22412 KB |
Output is correct |
32 |
Correct |
23 ms |
22536 KB |
Output is correct |
33 |
Correct |
25 ms |
22568 KB |
Output is correct |
34 |
Correct |
19 ms |
22588 KB |
Output is correct |
35 |
Correct |
22 ms |
22544 KB |
Output is correct |
36 |
Correct |
24 ms |
22588 KB |
Output is correct |
37 |
Correct |
23 ms |
22608 KB |
Output is correct |
38 |
Correct |
23 ms |
22596 KB |
Output is correct |
39 |
Correct |
17 ms |
22572 KB |
Output is correct |
40 |
Correct |
17 ms |
22616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 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 |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
3 ms |
3404 KB |
Output is correct |
22 |
Correct |
5 ms |
3532 KB |
Output is correct |
23 |
Correct |
3 ms |
3532 KB |
Output is correct |
24 |
Correct |
4 ms |
3532 KB |
Output is correct |
25 |
Correct |
4 ms |
3532 KB |
Output is correct |
26 |
Correct |
4 ms |
3488 KB |
Output is correct |
27 |
Correct |
4 ms |
3532 KB |
Output is correct |
28 |
Correct |
5 ms |
3532 KB |
Output is correct |
29 |
Correct |
3 ms |
3532 KB |
Output is correct |
30 |
Correct |
3 ms |
3532 KB |
Output is correct |
31 |
Correct |
21 ms |
22412 KB |
Output is correct |
32 |
Correct |
23 ms |
22536 KB |
Output is correct |
33 |
Correct |
25 ms |
22568 KB |
Output is correct |
34 |
Correct |
19 ms |
22588 KB |
Output is correct |
35 |
Correct |
22 ms |
22544 KB |
Output is correct |
36 |
Correct |
24 ms |
22588 KB |
Output is correct |
37 |
Correct |
23 ms |
22608 KB |
Output is correct |
38 |
Correct |
23 ms |
22596 KB |
Output is correct |
39 |
Correct |
17 ms |
22572 KB |
Output is correct |
40 |
Correct |
17 ms |
22616 KB |
Output is correct |
41 |
Correct |
702 ms |
508908 KB |
Output is correct |
42 |
Correct |
715 ms |
508968 KB |
Output is correct |
43 |
Correct |
694 ms |
508988 KB |
Output is correct |
44 |
Correct |
641 ms |
509028 KB |
Output is correct |
45 |
Correct |
634 ms |
509124 KB |
Output is correct |
46 |
Correct |
699 ms |
509100 KB |
Output is correct |
47 |
Correct |
686 ms |
509200 KB |
Output is correct |
48 |
Correct |
718 ms |
509200 KB |
Output is correct |
49 |
Correct |
644 ms |
509188 KB |
Output is correct |
50 |
Correct |
690 ms |
509196 KB |
Output is correct |
51 |
Correct |
842 ms |
509204 KB |
Output is correct |
52 |
Correct |
740 ms |
509208 KB |
Output is correct |
53 |
Correct |
663 ms |
509324 KB |
Output is correct |
54 |
Correct |
658 ms |
509168 KB |
Output is correct |
55 |
Correct |
698 ms |
509128 KB |
Output is correct |