#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
using namespace std;
using ll = long long;
const int N = 5000+5 , inf = 2e9 + 7;
const ll INF = 1e18 , mod = 1e9+7 , P = 6547;
int dp[N], pos[N], a[N], res[N][N];
int f[N];
int get(int x) {
int res = 0;
for(; x > 0; x&=x+1, x--) {
res += f[x];
}
return res;
}
void upd(int pos, int val = 1) {
for(; pos < N; pos |= pos+1) {
f[pos] += val;
}
}
int f1[N], db[N];
int get1(int x) {
int res = 0;
for(; x > 0; x&=x+1, x--) {
res += f1[x];
}
return res;
}
void upd1(int pos, int val = 1) {
for(; pos < N; pos |= pos+1) {
f1[pos] += val;
}
}
void solve(int tc) {
int n;
cin >> n;
for(int i = 1; i <= n; i ++) {
cin >> a[i];
pos[a[i]] = i;
}
for(int r = 1; r <= n; r ++) {
fill(f1, f1+n+1, 0);
for(int i = r; i >= 1; i --) {
db[i] = get1(pos[i]);
upd1(pos[i]);
}
upd(pos[r]);
for(int l = r; l >= 1; l --) {
if(l == r) {
res[l][r] = r-get(pos[r]);
}
else {
res[l][r] = res[l+1][r];
res[l][r] += r-get(pos[l]);
res[l][r] -= db[l];
}
}
}
for(int i = 1; i <= n; i ++) {
dp[i] = inf;
for(int j = i; j >= 1; j --) {
dp[i] = min(dp[i], res[j][i] + dp[j-1]);
}
}
cout << dp[n];
}
/*
*/
main() {
ios;
int tt = 1 , tc = 0;
// cin >> tt;
while(tt --) {
solve(++tc);
}
return 0;
}
Compilation message
Main.cpp:85:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
85 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 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 |
1 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 |
1 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 |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 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 |
1 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 |
1 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 |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
1220 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
2 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1224 KB |
Output is correct |
27 |
Correct |
1 ms |
1220 KB |
Output is correct |
28 |
Correct |
2 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 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 |
1 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 |
1 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 |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
1220 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
2 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1224 KB |
Output is correct |
27 |
Correct |
1 ms |
1220 KB |
Output is correct |
28 |
Correct |
2 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1220 KB |
Output is correct |
31 |
Correct |
12 ms |
4748 KB |
Output is correct |
32 |
Correct |
12 ms |
4820 KB |
Output is correct |
33 |
Correct |
12 ms |
4860 KB |
Output is correct |
34 |
Correct |
13 ms |
4820 KB |
Output is correct |
35 |
Correct |
13 ms |
4820 KB |
Output is correct |
36 |
Correct |
15 ms |
4808 KB |
Output is correct |
37 |
Correct |
13 ms |
4820 KB |
Output is correct |
38 |
Correct |
13 ms |
4808 KB |
Output is correct |
39 |
Correct |
16 ms |
4820 KB |
Output is correct |
40 |
Correct |
13 ms |
4880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 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 |
1 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 |
1 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 |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
1220 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
2 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1224 KB |
Output is correct |
27 |
Correct |
1 ms |
1220 KB |
Output is correct |
28 |
Correct |
2 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1220 KB |
Output is correct |
31 |
Correct |
12 ms |
4748 KB |
Output is correct |
32 |
Correct |
12 ms |
4820 KB |
Output is correct |
33 |
Correct |
12 ms |
4860 KB |
Output is correct |
34 |
Correct |
13 ms |
4820 KB |
Output is correct |
35 |
Correct |
13 ms |
4820 KB |
Output is correct |
36 |
Correct |
15 ms |
4808 KB |
Output is correct |
37 |
Correct |
13 ms |
4820 KB |
Output is correct |
38 |
Correct |
13 ms |
4808 KB |
Output is correct |
39 |
Correct |
16 ms |
4820 KB |
Output is correct |
40 |
Correct |
13 ms |
4880 KB |
Output is correct |
41 |
Correct |
609 ms |
67336 KB |
Output is correct |
42 |
Correct |
624 ms |
67432 KB |
Output is correct |
43 |
Correct |
593 ms |
67512 KB |
Output is correct |
44 |
Correct |
603 ms |
67308 KB |
Output is correct |
45 |
Correct |
616 ms |
67400 KB |
Output is correct |
46 |
Correct |
621 ms |
67432 KB |
Output is correct |
47 |
Correct |
607 ms |
67436 KB |
Output is correct |
48 |
Correct |
623 ms |
67468 KB |
Output is correct |
49 |
Correct |
618 ms |
67552 KB |
Output is correct |
50 |
Correct |
610 ms |
67516 KB |
Output is correct |
51 |
Correct |
622 ms |
67452 KB |
Output is correct |
52 |
Correct |
607 ms |
67508 KB |
Output is correct |
53 |
Correct |
630 ms |
67420 KB |
Output is correct |
54 |
Correct |
636 ms |
67496 KB |
Output is correct |
55 |
Correct |
630 ms |
67316 KB |
Output is correct |