#include<bits/stdc++.h>
using namespace std;
const int MX = 2004;
int n;
int a[MX];
int cost[MX][MX];
int st[MX*4];
void upd(int l, int r, int id, int val, int v) {
if(l==r) {
st[v] = val;
return;
}
int mid = (l+r)/2;
if(id<=mid) upd(l, mid, id, val, v*2);
else upd(mid+1, r, id, val, v*2+1);
st[v] = st[v*2]+st[v*2+1];
}
int query(int l, int r, int L, int R, int v) {
if(l>=L && r<=R) return st[v];
if(l>R || r<L) return 0;
int mid = (l+r)/2;
return query(l, mid, L, R, v*2) + query(mid+1, r, L, R, v*2+1);
}
int dp[MX];
int solve(int i) {
if(i>n) return 0;
int &ret = dp[i];
if(ret!=-1) return ret;
ret = INT_MAX;
for(int j=i; j<=n; ++j) {
ret = min(ret, solve(j+1)+cost[i][j]);
}
return ret;
}
int alt[MX];
void PlayGround() {
cin>>n;
for(int i=1; i<=n; ++i) {
cin>>a[i];
alt[a[i]] = i;
}
int inv[n+1] = {};
for(int i=1; i<=n; ++i) {
for(int j=alt[i]-1; j>0; --j) if(a[j]>i) {
++inv[i];
}
}
for(int j=1; j<=n; ++j) {
for(int i=1; i<=n; ++i) {
upd(1, n, i, 0, 1);
}
for(int i=j; i>0; --i) {
cost[i][j] = inv[i] - query(1, n, 1, alt[i], 1) + query(1, n, alt[i], n, 1);
if(i!=j) cost[i][j] += cost[i+1][j];
upd(1, n, alt[i], 1, 1);
}
}
memset(dp, -1, sizeof dp);
cout<<solve(1)<<'\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
1236 KB |
Output is correct |
22 |
Correct |
6 ms |
1236 KB |
Output is correct |
23 |
Correct |
5 ms |
1236 KB |
Output is correct |
24 |
Correct |
6 ms |
1228 KB |
Output is correct |
25 |
Correct |
5 ms |
1236 KB |
Output is correct |
26 |
Correct |
6 ms |
1228 KB |
Output is correct |
27 |
Correct |
6 ms |
1124 KB |
Output is correct |
28 |
Correct |
5 ms |
1236 KB |
Output is correct |
29 |
Correct |
5 ms |
1224 KB |
Output is correct |
30 |
Correct |
5 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
1236 KB |
Output is correct |
22 |
Correct |
6 ms |
1236 KB |
Output is correct |
23 |
Correct |
5 ms |
1236 KB |
Output is correct |
24 |
Correct |
6 ms |
1228 KB |
Output is correct |
25 |
Correct |
5 ms |
1236 KB |
Output is correct |
26 |
Correct |
6 ms |
1228 KB |
Output is correct |
27 |
Correct |
6 ms |
1124 KB |
Output is correct |
28 |
Correct |
5 ms |
1236 KB |
Output is correct |
29 |
Correct |
5 ms |
1224 KB |
Output is correct |
30 |
Correct |
5 ms |
1236 KB |
Output is correct |
31 |
Correct |
112 ms |
4744 KB |
Output is correct |
32 |
Correct |
110 ms |
4740 KB |
Output is correct |
33 |
Correct |
109 ms |
4828 KB |
Output is correct |
34 |
Correct |
109 ms |
4736 KB |
Output is correct |
35 |
Correct |
113 ms |
4756 KB |
Output is correct |
36 |
Correct |
113 ms |
4740 KB |
Output is correct |
37 |
Correct |
109 ms |
4748 KB |
Output is correct |
38 |
Correct |
112 ms |
4788 KB |
Output is correct |
39 |
Correct |
109 ms |
4828 KB |
Output is correct |
40 |
Correct |
116 ms |
4840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
1236 KB |
Output is correct |
22 |
Correct |
6 ms |
1236 KB |
Output is correct |
23 |
Correct |
5 ms |
1236 KB |
Output is correct |
24 |
Correct |
6 ms |
1228 KB |
Output is correct |
25 |
Correct |
5 ms |
1236 KB |
Output is correct |
26 |
Correct |
6 ms |
1228 KB |
Output is correct |
27 |
Correct |
6 ms |
1124 KB |
Output is correct |
28 |
Correct |
5 ms |
1236 KB |
Output is correct |
29 |
Correct |
5 ms |
1224 KB |
Output is correct |
30 |
Correct |
5 ms |
1236 KB |
Output is correct |
31 |
Correct |
112 ms |
4744 KB |
Output is correct |
32 |
Correct |
110 ms |
4740 KB |
Output is correct |
33 |
Correct |
109 ms |
4828 KB |
Output is correct |
34 |
Correct |
109 ms |
4736 KB |
Output is correct |
35 |
Correct |
113 ms |
4756 KB |
Output is correct |
36 |
Correct |
113 ms |
4740 KB |
Output is correct |
37 |
Correct |
109 ms |
4748 KB |
Output is correct |
38 |
Correct |
112 ms |
4788 KB |
Output is correct |
39 |
Correct |
109 ms |
4828 KB |
Output is correct |
40 |
Correct |
116 ms |
4840 KB |
Output is correct |
41 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 11 |
42 |
Halted |
0 ms |
0 KB |
- |