#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 5005;
int a[N], p[N], dp[N], cost[N][N];
struct BIT {
int bit[N];
BIT() {
memset(bit, 0, sizeof bit);
}
void add(int pos, int val) {
for(; pos < N; pos += pos & -pos)
bit[pos] += val;
}
int qry(int pos) {
int res = 0;
for(; pos; pos -= pos & -pos)
res += bit[pos];
return res;
}
} b;
signed main()
{
IO_OP;
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
p[a[i]] = i;
}
for(int r = 1; r <= n; r++) {
vi psum(n + 1);
for(int i = 1; i <= r; i++) psum[p[i]]++;
for(int i = 1; i <= n; i++) psum[i] += psum[i - 1];
int ans = 0;
for(int l = r; l >= 1; l--) {
ans += psum[n] - psum[p[l]];
ans -= b.qry(p[l]);
cost[l][r] = ans;
b.add(p[l], 1);
}
for(int l = r; l >= 1; l--) b.add(p[l], -1);
}
dp[0] = 0;
for(int i = 1; i <= n; i++) {
dp[i] = INF;
for(int j = 1; j <= i; j++) {
dp[i] = min(dp[i], dp[j - 1] + cost[j][i]);
}
}
cout << dp[n] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
1260 KB |
Output is correct |
22 |
Correct |
2 ms |
1260 KB |
Output is correct |
23 |
Correct |
2 ms |
1260 KB |
Output is correct |
24 |
Correct |
3 ms |
1280 KB |
Output is correct |
25 |
Correct |
2 ms |
1260 KB |
Output is correct |
26 |
Correct |
3 ms |
1260 KB |
Output is correct |
27 |
Correct |
2 ms |
1280 KB |
Output is correct |
28 |
Correct |
6 ms |
1400 KB |
Output is correct |
29 |
Correct |
2 ms |
1260 KB |
Output is correct |
30 |
Correct |
2 ms |
1260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
1260 KB |
Output is correct |
22 |
Correct |
2 ms |
1260 KB |
Output is correct |
23 |
Correct |
2 ms |
1260 KB |
Output is correct |
24 |
Correct |
3 ms |
1280 KB |
Output is correct |
25 |
Correct |
2 ms |
1260 KB |
Output is correct |
26 |
Correct |
3 ms |
1260 KB |
Output is correct |
27 |
Correct |
2 ms |
1280 KB |
Output is correct |
28 |
Correct |
6 ms |
1400 KB |
Output is correct |
29 |
Correct |
2 ms |
1260 KB |
Output is correct |
30 |
Correct |
2 ms |
1260 KB |
Output is correct |
31 |
Correct |
22 ms |
4844 KB |
Output is correct |
32 |
Correct |
22 ms |
4844 KB |
Output is correct |
33 |
Correct |
24 ms |
4972 KB |
Output is correct |
34 |
Correct |
22 ms |
4844 KB |
Output is correct |
35 |
Correct |
22 ms |
4908 KB |
Output is correct |
36 |
Correct |
24 ms |
4844 KB |
Output is correct |
37 |
Correct |
22 ms |
4844 KB |
Output is correct |
38 |
Correct |
22 ms |
4844 KB |
Output is correct |
39 |
Correct |
22 ms |
4844 KB |
Output is correct |
40 |
Correct |
28 ms |
4844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
1260 KB |
Output is correct |
22 |
Correct |
2 ms |
1260 KB |
Output is correct |
23 |
Correct |
2 ms |
1260 KB |
Output is correct |
24 |
Correct |
3 ms |
1280 KB |
Output is correct |
25 |
Correct |
2 ms |
1260 KB |
Output is correct |
26 |
Correct |
3 ms |
1260 KB |
Output is correct |
27 |
Correct |
2 ms |
1280 KB |
Output is correct |
28 |
Correct |
6 ms |
1400 KB |
Output is correct |
29 |
Correct |
2 ms |
1260 KB |
Output is correct |
30 |
Correct |
2 ms |
1260 KB |
Output is correct |
31 |
Correct |
22 ms |
4844 KB |
Output is correct |
32 |
Correct |
22 ms |
4844 KB |
Output is correct |
33 |
Correct |
24 ms |
4972 KB |
Output is correct |
34 |
Correct |
22 ms |
4844 KB |
Output is correct |
35 |
Correct |
22 ms |
4908 KB |
Output is correct |
36 |
Correct |
24 ms |
4844 KB |
Output is correct |
37 |
Correct |
22 ms |
4844 KB |
Output is correct |
38 |
Correct |
22 ms |
4844 KB |
Output is correct |
39 |
Correct |
22 ms |
4844 KB |
Output is correct |
40 |
Correct |
28 ms |
4844 KB |
Output is correct |
41 |
Correct |
971 ms |
67424 KB |
Output is correct |
42 |
Correct |
955 ms |
67308 KB |
Output is correct |
43 |
Correct |
948 ms |
67492 KB |
Output is correct |
44 |
Correct |
962 ms |
67576 KB |
Output is correct |
45 |
Correct |
934 ms |
67436 KB |
Output is correct |
46 |
Correct |
966 ms |
67436 KB |
Output is correct |
47 |
Correct |
942 ms |
67820 KB |
Output is correct |
48 |
Correct |
947 ms |
67564 KB |
Output is correct |
49 |
Correct |
957 ms |
67840 KB |
Output is correct |
50 |
Correct |
945 ms |
67612 KB |
Output is correct |
51 |
Correct |
939 ms |
67592 KB |
Output is correct |
52 |
Correct |
945 ms |
67564 KB |
Output is correct |
53 |
Correct |
949 ms |
67584 KB |
Output is correct |
54 |
Correct |
954 ms |
67592 KB |
Output is correct |
55 |
Correct |
946 ms |
67564 KB |
Output is correct |