#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define ALL(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<ll>
#define SZ(x) (int)x.size()
#define CASE int t;cin>>t;for(int ca=1;ca<=t;ca++)
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int MAX = 5007, MOD = 1e9 + 7;
int a[MAX], ans[MAX], dp[MAX][MAX], id[MAX];
int bit[MAX][5];
void upd(int x, int v, int ty){
while(x < MAX){
bit[x][ty] += v;
x += x & -x;
}
}
int qry(int x, int ty){
int res = 0;
while(x){
res += bit[x][ty];
x -= x & -x;
}
return res;
}
void solve(){
int n;
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i];
ans[i] = 2e9;
id[a[i]] = i;
}
for(int i = 1;i <= n;i++){
int now = 0;
for(int j = n;j >= 1;j--){
if(i <= a[j]){
int pos = i + n - a[j];
now += qry(pos, 0);
upd(pos, 1, 0);
upd(j, 1, 1);
}
}
for(int j = n;j >= i;j--){
dp[i][j] = now;
upd(id[j], -1, 1);
now += j - i - qry(id[j], 1);
now -= qry(id[j], 1);
now -= qry(id[j], 2);
upd(id[j], 1, 2);
}
for(int j = n;j >= 1;j--){
if(i <= a[j]){
int pos = i + n - a[j];
upd(pos, -1, 0);
}
}
for(int j = n;j >= i;j--)
upd(id[j], -1, 2);
}
for(int i = 0;i < n;i++){
for(int j = i + 1;j <= n;j++){
ans[j] = min(ans[j], ans[i] + dp[i + 1][j]);
}
}
cout << ans[n] << '\n';
}
int main(){
IOS
// CASE
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
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 |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
324 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 |
332 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 |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
324 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 |
332 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 |
4 ms |
1136 KB |
Output is correct |
22 |
Correct |
4 ms |
1236 KB |
Output is correct |
23 |
Correct |
4 ms |
1228 KB |
Output is correct |
24 |
Correct |
5 ms |
1236 KB |
Output is correct |
25 |
Correct |
5 ms |
1264 KB |
Output is correct |
26 |
Correct |
4 ms |
1236 KB |
Output is correct |
27 |
Correct |
4 ms |
1236 KB |
Output is correct |
28 |
Correct |
5 ms |
1236 KB |
Output is correct |
29 |
Correct |
3 ms |
1236 KB |
Output is correct |
30 |
Correct |
4 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
324 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 |
332 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 |
4 ms |
1136 KB |
Output is correct |
22 |
Correct |
4 ms |
1236 KB |
Output is correct |
23 |
Correct |
4 ms |
1228 KB |
Output is correct |
24 |
Correct |
5 ms |
1236 KB |
Output is correct |
25 |
Correct |
5 ms |
1264 KB |
Output is correct |
26 |
Correct |
4 ms |
1236 KB |
Output is correct |
27 |
Correct |
4 ms |
1236 KB |
Output is correct |
28 |
Correct |
5 ms |
1236 KB |
Output is correct |
29 |
Correct |
3 ms |
1236 KB |
Output is correct |
30 |
Correct |
4 ms |
1236 KB |
Output is correct |
31 |
Correct |
46 ms |
4688 KB |
Output is correct |
32 |
Correct |
46 ms |
4684 KB |
Output is correct |
33 |
Correct |
51 ms |
4812 KB |
Output is correct |
34 |
Correct |
46 ms |
4772 KB |
Output is correct |
35 |
Correct |
54 ms |
4816 KB |
Output is correct |
36 |
Correct |
45 ms |
4752 KB |
Output is correct |
37 |
Correct |
46 ms |
4804 KB |
Output is correct |
38 |
Correct |
46 ms |
4736 KB |
Output is correct |
39 |
Correct |
47 ms |
4796 KB |
Output is correct |
40 |
Correct |
50 ms |
4812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
324 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 |
332 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 |
4 ms |
1136 KB |
Output is correct |
22 |
Correct |
4 ms |
1236 KB |
Output is correct |
23 |
Correct |
4 ms |
1228 KB |
Output is correct |
24 |
Correct |
5 ms |
1236 KB |
Output is correct |
25 |
Correct |
5 ms |
1264 KB |
Output is correct |
26 |
Correct |
4 ms |
1236 KB |
Output is correct |
27 |
Correct |
4 ms |
1236 KB |
Output is correct |
28 |
Correct |
5 ms |
1236 KB |
Output is correct |
29 |
Correct |
3 ms |
1236 KB |
Output is correct |
30 |
Correct |
4 ms |
1236 KB |
Output is correct |
31 |
Correct |
46 ms |
4688 KB |
Output is correct |
32 |
Correct |
46 ms |
4684 KB |
Output is correct |
33 |
Correct |
51 ms |
4812 KB |
Output is correct |
34 |
Correct |
46 ms |
4772 KB |
Output is correct |
35 |
Correct |
54 ms |
4816 KB |
Output is correct |
36 |
Correct |
45 ms |
4752 KB |
Output is correct |
37 |
Correct |
46 ms |
4804 KB |
Output is correct |
38 |
Correct |
46 ms |
4736 KB |
Output is correct |
39 |
Correct |
47 ms |
4796 KB |
Output is correct |
40 |
Correct |
50 ms |
4812 KB |
Output is correct |
41 |
Correct |
1678 ms |
67300 KB |
Output is correct |
42 |
Correct |
1675 ms |
67340 KB |
Output is correct |
43 |
Correct |
1632 ms |
67504 KB |
Output is correct |
44 |
Correct |
1684 ms |
67396 KB |
Output is correct |
45 |
Correct |
1693 ms |
67324 KB |
Output is correct |
46 |
Correct |
1644 ms |
67508 KB |
Output is correct |
47 |
Correct |
1761 ms |
67312 KB |
Output is correct |
48 |
Correct |
1720 ms |
67296 KB |
Output is correct |
49 |
Correct |
1647 ms |
67368 KB |
Output is correct |
50 |
Correct |
1678 ms |
67320 KB |
Output is correct |
51 |
Correct |
1718 ms |
67512 KB |
Output is correct |
52 |
Correct |
1666 ms |
67312 KB |
Output is correct |
53 |
Correct |
1627 ms |
67372 KB |
Output is correct |
54 |
Correct |
1664 ms |
67496 KB |
Output is correct |
55 |
Correct |
1741 ms |
67328 KB |
Output is correct |