#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;
}
} b1, b2;
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++) {
for(int i = 1; i <= r; i++) b1.add(p[i], 1);
int ans = 0;
for(int l = r; l >= 1; l--) {
ans += b1.qry(n) - b1.qry(p[l]);
ans -= b2.qry(p[l]);
cost[l][r] = ans;
b2.add(p[l], 1);
}
for(int l = r; l >= 1; l--) b2.add(p[l], -1);
for(int i = 1; i <= r; i++) b1.add(p[i], -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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 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 |
3 ms |
1260 KB |
Output is correct |
22 |
Correct |
3 ms |
1260 KB |
Output is correct |
23 |
Correct |
3 ms |
1260 KB |
Output is correct |
24 |
Correct |
3 ms |
1260 KB |
Output is correct |
25 |
Correct |
3 ms |
1260 KB |
Output is correct |
26 |
Correct |
3 ms |
1260 KB |
Output is correct |
27 |
Correct |
3 ms |
1260 KB |
Output is correct |
28 |
Correct |
3 ms |
1260 KB |
Output is correct |
29 |
Correct |
3 ms |
1260 KB |
Output is correct |
30 |
Correct |
3 ms |
1260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 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 |
3 ms |
1260 KB |
Output is correct |
22 |
Correct |
3 ms |
1260 KB |
Output is correct |
23 |
Correct |
3 ms |
1260 KB |
Output is correct |
24 |
Correct |
3 ms |
1260 KB |
Output is correct |
25 |
Correct |
3 ms |
1260 KB |
Output is correct |
26 |
Correct |
3 ms |
1260 KB |
Output is correct |
27 |
Correct |
3 ms |
1260 KB |
Output is correct |
28 |
Correct |
3 ms |
1260 KB |
Output is correct |
29 |
Correct |
3 ms |
1260 KB |
Output is correct |
30 |
Correct |
3 ms |
1260 KB |
Output is correct |
31 |
Correct |
34 ms |
4844 KB |
Output is correct |
32 |
Correct |
37 ms |
4844 KB |
Output is correct |
33 |
Correct |
34 ms |
4844 KB |
Output is correct |
34 |
Correct |
37 ms |
4844 KB |
Output is correct |
35 |
Correct |
34 ms |
4972 KB |
Output is correct |
36 |
Correct |
33 ms |
4844 KB |
Output is correct |
37 |
Correct |
34 ms |
4844 KB |
Output is correct |
38 |
Correct |
33 ms |
4844 KB |
Output is correct |
39 |
Correct |
33 ms |
4844 KB |
Output is correct |
40 |
Correct |
33 ms |
4844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 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 |
3 ms |
1260 KB |
Output is correct |
22 |
Correct |
3 ms |
1260 KB |
Output is correct |
23 |
Correct |
3 ms |
1260 KB |
Output is correct |
24 |
Correct |
3 ms |
1260 KB |
Output is correct |
25 |
Correct |
3 ms |
1260 KB |
Output is correct |
26 |
Correct |
3 ms |
1260 KB |
Output is correct |
27 |
Correct |
3 ms |
1260 KB |
Output is correct |
28 |
Correct |
3 ms |
1260 KB |
Output is correct |
29 |
Correct |
3 ms |
1260 KB |
Output is correct |
30 |
Correct |
3 ms |
1260 KB |
Output is correct |
31 |
Correct |
34 ms |
4844 KB |
Output is correct |
32 |
Correct |
37 ms |
4844 KB |
Output is correct |
33 |
Correct |
34 ms |
4844 KB |
Output is correct |
34 |
Correct |
37 ms |
4844 KB |
Output is correct |
35 |
Correct |
34 ms |
4972 KB |
Output is correct |
36 |
Correct |
33 ms |
4844 KB |
Output is correct |
37 |
Correct |
34 ms |
4844 KB |
Output is correct |
38 |
Correct |
33 ms |
4844 KB |
Output is correct |
39 |
Correct |
33 ms |
4844 KB |
Output is correct |
40 |
Correct |
33 ms |
4844 KB |
Output is correct |
41 |
Correct |
1480 ms |
67308 KB |
Output is correct |
42 |
Correct |
1475 ms |
67404 KB |
Output is correct |
43 |
Correct |
1478 ms |
67436 KB |
Output is correct |
44 |
Correct |
1495 ms |
67528 KB |
Output is correct |
45 |
Correct |
1530 ms |
67356 KB |
Output is correct |
46 |
Correct |
1520 ms |
67748 KB |
Output is correct |
47 |
Correct |
1459 ms |
67436 KB |
Output is correct |
48 |
Correct |
1469 ms |
67436 KB |
Output is correct |
49 |
Correct |
1480 ms |
67356 KB |
Output is correct |
50 |
Correct |
1495 ms |
67436 KB |
Output is correct |
51 |
Correct |
1456 ms |
67468 KB |
Output is correct |
52 |
Correct |
1464 ms |
67540 KB |
Output is correct |
53 |
Correct |
1469 ms |
67692 KB |
Output is correct |
54 |
Correct |
1462 ms |
67520 KB |
Output is correct |
55 |
Correct |
1455 ms |
67436 KB |
Output is correct |