#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MAXN = 5000;
int mns[MAXN+5][MAXN+5];
int dp[MAXN+5];
int a[MAXN+5];
int pos[MAXN+5];
struct BIT{
int n;
int bit[MAXN+5];
void add(int x){
while(x <= n){
bit[x]++;
x += x & -x;
}
}
int get(int x){
int res = 0;
while(x){
res += bit[x];
x -= x & -x;
}
return res;
}
};
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n;
cin >> n;
BIT bitpre;
bitpre.n = n;
for(int i=1; i<=n; i++){
cin >> a[i];
pos[a[i]] = i;
}
for(int l=1; l<=n; l++){
BIT bit;
bit.n = n;
for(int i=1; i<=n; i++) bit.bit[i] = 0;
for(int r=l; r<=n; r++){
mns[l][r] = mns[l][r-1];
mns[l][r] += bitpre.get(n) - bitpre.get(pos[r]);
mns[l][r] += bit.get(pos[r] - 1);
bit.add(pos[r]);
}
bitpre.add(pos[l]);
}
for(int i=1; i<=n; i++){
dp[i] = n*n;
for(int j=i; j>=1; j--){
dp[i] = min(dp[i], dp[j-1] + mns[j][i]);
}
}
cout << dp[n] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
356 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 |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
356 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 |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
356 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 |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
1232 KB |
Output is correct |
22 |
Correct |
2 ms |
1232 KB |
Output is correct |
23 |
Correct |
2 ms |
1232 KB |
Output is correct |
24 |
Correct |
2 ms |
1232 KB |
Output is correct |
25 |
Correct |
2 ms |
1124 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1128 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
2 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
356 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 |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
1232 KB |
Output is correct |
22 |
Correct |
2 ms |
1232 KB |
Output is correct |
23 |
Correct |
2 ms |
1232 KB |
Output is correct |
24 |
Correct |
2 ms |
1232 KB |
Output is correct |
25 |
Correct |
2 ms |
1124 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1128 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
2 ms |
1228 KB |
Output is correct |
31 |
Correct |
13 ms |
4812 KB |
Output is correct |
32 |
Correct |
14 ms |
4856 KB |
Output is correct |
33 |
Correct |
13 ms |
4836 KB |
Output is correct |
34 |
Correct |
13 ms |
4856 KB |
Output is correct |
35 |
Correct |
13 ms |
4812 KB |
Output is correct |
36 |
Correct |
14 ms |
4764 KB |
Output is correct |
37 |
Correct |
13 ms |
4812 KB |
Output is correct |
38 |
Correct |
14 ms |
4772 KB |
Output is correct |
39 |
Correct |
13 ms |
4752 KB |
Output is correct |
40 |
Correct |
13 ms |
4764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
356 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 |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
1232 KB |
Output is correct |
22 |
Correct |
2 ms |
1232 KB |
Output is correct |
23 |
Correct |
2 ms |
1232 KB |
Output is correct |
24 |
Correct |
2 ms |
1232 KB |
Output is correct |
25 |
Correct |
2 ms |
1124 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1128 KB |
Output is correct |
29 |
Correct |
2 ms |
1228 KB |
Output is correct |
30 |
Correct |
2 ms |
1228 KB |
Output is correct |
31 |
Correct |
13 ms |
4812 KB |
Output is correct |
32 |
Correct |
14 ms |
4856 KB |
Output is correct |
33 |
Correct |
13 ms |
4836 KB |
Output is correct |
34 |
Correct |
13 ms |
4856 KB |
Output is correct |
35 |
Correct |
13 ms |
4812 KB |
Output is correct |
36 |
Correct |
14 ms |
4764 KB |
Output is correct |
37 |
Correct |
13 ms |
4812 KB |
Output is correct |
38 |
Correct |
14 ms |
4772 KB |
Output is correct |
39 |
Correct |
13 ms |
4752 KB |
Output is correct |
40 |
Correct |
13 ms |
4764 KB |
Output is correct |
41 |
Correct |
688 ms |
67336 KB |
Output is correct |
42 |
Correct |
677 ms |
67448 KB |
Output is correct |
43 |
Correct |
672 ms |
67392 KB |
Output is correct |
44 |
Correct |
676 ms |
67396 KB |
Output is correct |
45 |
Correct |
686 ms |
67452 KB |
Output is correct |
46 |
Correct |
679 ms |
67420 KB |
Output is correct |
47 |
Correct |
664 ms |
67392 KB |
Output is correct |
48 |
Correct |
666 ms |
67396 KB |
Output is correct |
49 |
Correct |
676 ms |
67300 KB |
Output is correct |
50 |
Correct |
659 ms |
67324 KB |
Output is correct |
51 |
Correct |
677 ms |
67532 KB |
Output is correct |
52 |
Correct |
691 ms |
67400 KB |
Output is correct |
53 |
Correct |
691 ms |
67420 KB |
Output is correct |
54 |
Correct |
664 ms |
67320 KB |
Output is correct |
55 |
Correct |
668 ms |
67324 KB |
Output is correct |