#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 5005;
int n;
int a[SIZE], p[SIZE], add[SIZE];
int dp[SIZE];
int cnt[SIZE][SIZE], sum[SIZE][SIZE];
struct BIT {
BIT() {fill(bit, bit + n + 1, 0);}
int bit[SIZE];
void upd(int pos, int x) {
for (; pos <= n; pos += pos & -pos) bit[pos] += x;
}
int que(int pos) {
int re = 0;
for (; pos; pos -= pos & -pos) re += bit[pos];
return re;
}
int que(int l, int r) {
return que(r) - que(l - 1);
}
pair<int, int> sch(int s) {
int pos = 0, k = 0;
for (int i = __lg(n); i >= 0; i--) if (pos + (1 << i) <= n) {
int to = k + bit[pos + (1 << i)];
if (pos + (1 << i) <= s + to - 1) {
pos += 1 << i;
k = to;
}
}
return {pos, k};
}
} bit, bit2;
int Sum(int l, int r) {
return (l + r) * (r - l + 1) / 2;
}
void solve() {
cin >> n;
FOR (i, 1, n) cin >> a[i], p[a[i]] = i;
FOR (i, 1, n) {
bit = BIT();
FOR (j, i, n) {
cnt[i][j] = cnt[i][j - 1] + bit.que(p[j]);
bit.upd(p[j], 1);
}
}
FOR (i, 1, n) {
bit = bit2 = BIT();
FOR (j, i, n) {
int pos = p[j] + add[p[j]], k;
bit.upd(pos, 1);
bit2.upd(pos, pos);
tie(pos, k) = bit.sch(i - 1);
sum[i][j] = Sum(i, i + k - 1) - bit2.que(1, pos) + bit2.que(pos + 1, n) - Sum(i + k, j);
}
FOR (j, 1, p[i]) add[j]++;
}
FOR (i, 1, n) {
dp[i] = INT_MAX;
FOR (j, 0, i - 1) dp[i] = min(dp[i], dp[j] + sum[j + 1][i] + cnt[j + 1][i]);
}
cout << dp[n] << '\n';
}
int main() {
Waimai;
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
3 ms |
2144 KB |
Output is correct |
22 |
Correct |
3 ms |
2132 KB |
Output is correct |
23 |
Correct |
3 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2132 KB |
Output is correct |
25 |
Correct |
3 ms |
2132 KB |
Output is correct |
26 |
Correct |
3 ms |
2132 KB |
Output is correct |
27 |
Correct |
3 ms |
2132 KB |
Output is correct |
28 |
Correct |
3 ms |
2132 KB |
Output is correct |
29 |
Correct |
3 ms |
2132 KB |
Output is correct |
30 |
Correct |
3 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
3 ms |
2144 KB |
Output is correct |
22 |
Correct |
3 ms |
2132 KB |
Output is correct |
23 |
Correct |
3 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2132 KB |
Output is correct |
25 |
Correct |
3 ms |
2132 KB |
Output is correct |
26 |
Correct |
3 ms |
2132 KB |
Output is correct |
27 |
Correct |
3 ms |
2132 KB |
Output is correct |
28 |
Correct |
3 ms |
2132 KB |
Output is correct |
29 |
Correct |
3 ms |
2132 KB |
Output is correct |
30 |
Correct |
3 ms |
2132 KB |
Output is correct |
31 |
Correct |
37 ms |
9316 KB |
Output is correct |
32 |
Correct |
43 ms |
9244 KB |
Output is correct |
33 |
Correct |
43 ms |
9236 KB |
Output is correct |
34 |
Correct |
35 ms |
9240 KB |
Output is correct |
35 |
Correct |
36 ms |
9292 KB |
Output is correct |
36 |
Correct |
35 ms |
9292 KB |
Output is correct |
37 |
Correct |
35 ms |
9248 KB |
Output is correct |
38 |
Correct |
35 ms |
9292 KB |
Output is correct |
39 |
Correct |
35 ms |
9324 KB |
Output is correct |
40 |
Correct |
36 ms |
9444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
3 ms |
2144 KB |
Output is correct |
22 |
Correct |
3 ms |
2132 KB |
Output is correct |
23 |
Correct |
3 ms |
2132 KB |
Output is correct |
24 |
Correct |
3 ms |
2132 KB |
Output is correct |
25 |
Correct |
3 ms |
2132 KB |
Output is correct |
26 |
Correct |
3 ms |
2132 KB |
Output is correct |
27 |
Correct |
3 ms |
2132 KB |
Output is correct |
28 |
Correct |
3 ms |
2132 KB |
Output is correct |
29 |
Correct |
3 ms |
2132 KB |
Output is correct |
30 |
Correct |
3 ms |
2132 KB |
Output is correct |
31 |
Correct |
37 ms |
9316 KB |
Output is correct |
32 |
Correct |
43 ms |
9244 KB |
Output is correct |
33 |
Correct |
43 ms |
9236 KB |
Output is correct |
34 |
Correct |
35 ms |
9240 KB |
Output is correct |
35 |
Correct |
36 ms |
9292 KB |
Output is correct |
36 |
Correct |
35 ms |
9292 KB |
Output is correct |
37 |
Correct |
35 ms |
9248 KB |
Output is correct |
38 |
Correct |
35 ms |
9292 KB |
Output is correct |
39 |
Correct |
35 ms |
9324 KB |
Output is correct |
40 |
Correct |
36 ms |
9444 KB |
Output is correct |
41 |
Correct |
1653 ms |
134348 KB |
Output is correct |
42 |
Correct |
1692 ms |
134504 KB |
Output is correct |
43 |
Correct |
1686 ms |
134348 KB |
Output is correct |
44 |
Correct |
1705 ms |
134388 KB |
Output is correct |
45 |
Correct |
1695 ms |
134304 KB |
Output is correct |
46 |
Correct |
1665 ms |
134408 KB |
Output is correct |
47 |
Correct |
1645 ms |
134348 KB |
Output is correct |
48 |
Correct |
1652 ms |
134496 KB |
Output is correct |
49 |
Correct |
1644 ms |
134432 KB |
Output is correct |
50 |
Correct |
1675 ms |
134372 KB |
Output is correct |
51 |
Correct |
1651 ms |
134604 KB |
Output is correct |
52 |
Correct |
1696 ms |
134384 KB |
Output is correct |
53 |
Correct |
1698 ms |
134476 KB |
Output is correct |
54 |
Correct |
1679 ms |
134584 KB |
Output is correct |
55 |
Correct |
1639 ms |
134648 KB |
Output is correct |