#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 500'005;
const ll MOD = 1'000'000'007;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int a[5005][5005];
int bit[5005];
int h[N];
int loc[N];
void update(int x){
while(x < 5005){
++bit[x];
x += x & -x;
}
}
int get(int x){
int s = 0;
while(x){
s += bit[x];
x -= x & -x;
}
return s;
}
int dp[5005];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i = 1; i <= n; ++i){
cin>>h[i];
loc[h[i]] = i;
}
for(int i = 1; i <= n; ++i){
for(int j = 0; j < 5005; ++j)
bit[j] = 0;
for(int j = i; j <= n; ++j){
a[i][j] = a[i][j - 1] + j - i - get(loc[j]);
update(loc[j]);
}
}
for(int i = 0; i <= n; ++i){
dp[i] = a[1][n];
for(int j = 0; j < i; ++j)
dp[i] = min(dp[i], dp[j] - a[j + 1][i] + (i - j) * (i - j - 1) / 2 - a[j + 1][i]);
}
cout<<dp[n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 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 |
512 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 |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 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 |
512 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 |
1304 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 |
4 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 |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 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 |
512 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 |
1304 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 |
4 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 |
17 ms |
4844 KB |
Output is correct |
32 |
Correct |
16 ms |
4844 KB |
Output is correct |
33 |
Correct |
17 ms |
4844 KB |
Output is correct |
34 |
Correct |
17 ms |
4844 KB |
Output is correct |
35 |
Correct |
17 ms |
4844 KB |
Output is correct |
36 |
Correct |
18 ms |
4844 KB |
Output is correct |
37 |
Correct |
17 ms |
4844 KB |
Output is correct |
38 |
Correct |
16 ms |
4844 KB |
Output is correct |
39 |
Correct |
17 ms |
4844 KB |
Output is correct |
40 |
Correct |
17 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 |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 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 |
512 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 |
1304 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 |
4 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 |
17 ms |
4844 KB |
Output is correct |
32 |
Correct |
16 ms |
4844 KB |
Output is correct |
33 |
Correct |
17 ms |
4844 KB |
Output is correct |
34 |
Correct |
17 ms |
4844 KB |
Output is correct |
35 |
Correct |
17 ms |
4844 KB |
Output is correct |
36 |
Correct |
18 ms |
4844 KB |
Output is correct |
37 |
Correct |
17 ms |
4844 KB |
Output is correct |
38 |
Correct |
16 ms |
4844 KB |
Output is correct |
39 |
Correct |
17 ms |
4844 KB |
Output is correct |
40 |
Correct |
17 ms |
4844 KB |
Output is correct |
41 |
Correct |
564 ms |
67448 KB |
Output is correct |
42 |
Correct |
542 ms |
67436 KB |
Output is correct |
43 |
Correct |
542 ms |
67436 KB |
Output is correct |
44 |
Correct |
545 ms |
67436 KB |
Output is correct |
45 |
Correct |
545 ms |
67564 KB |
Output is correct |
46 |
Correct |
544 ms |
67436 KB |
Output is correct |
47 |
Correct |
557 ms |
67564 KB |
Output is correct |
48 |
Correct |
559 ms |
67604 KB |
Output is correct |
49 |
Correct |
551 ms |
67476 KB |
Output is correct |
50 |
Correct |
545 ms |
67572 KB |
Output is correct |
51 |
Correct |
539 ms |
67652 KB |
Output is correct |
52 |
Correct |
541 ms |
67564 KB |
Output is correct |
53 |
Correct |
577 ms |
67692 KB |
Output is correct |
54 |
Correct |
540 ms |
67564 KB |
Output is correct |
55 |
Correct |
547 ms |
67424 KB |
Output is correct |