#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1e18;
struct BIT{
int n;
vector<ll> sm;
BIT(int _n){
n = _n;
sm.resize(n);
}
void add(int in, int x){
in++;
while(in <= n) sm[in-1]+=x, in+=in&-in;
}
ll sum(int in){
in++;
ll s = 0;
while(in >= 1) s+=sm[in-1], in-=in&-in;
return s;
}
ll sum(int l, int r){
return sum(r)-(l == 0? 0:sum(l-1));
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
vector<vector<ll>> dp(n, vector<ll>(n, inf));
vector<vector<ll>> inv(n, vector<ll>(n, 0));
vector<int> v(n);
for(int &x: v) cin >> x, x--;
vector<int> pos(n);
for(int i = 0; i < n; i++) pos[v[i]] = i;
for(int i = 0; i < n; i++) for(int j = i+1; j < n; j++) if(pos[i] < pos[j]) inv[i][j]++;
for(int i = 0; i < n; i++) for(int j = 0; j+i < n; j++){
int l = j, r = j+i;
if(l == r) continue;
inv[l][r]+=inv[l][r-1]+inv[l+1][r]-inv[l+1][r-1];
}
vector<ll> mn(n, inf);
for(int i = n-1; i >= 0; i--){
vector<ll> sum(n, 0);
for(int j = 0; j < n; j++) if(v[j] > i) sum[j]++;
for(int j = 1; j < n; j++) sum[j]+=sum[j-1];
ll c = 0;
for(int j = i; j >= 0; j--){
c+=sum[pos[j]];
dp[i][j] = inv[j][i]+c;
if(i != n-1) dp[i][j]+=mn[i];
if(j != 0) mn[j-1] = min(mn[j-1], dp[i][j]);
}
}
ll ans = inf;
for(int i = 0; i < n; i++) ans = min(ans, dp[i][0]);
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
844 KB |
Output is correct |
22 |
Correct |
1 ms |
844 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
1 ms |
844 KB |
Output is correct |
25 |
Correct |
1 ms |
844 KB |
Output is correct |
26 |
Correct |
1 ms |
844 KB |
Output is correct |
27 |
Correct |
1 ms |
844 KB |
Output is correct |
28 |
Correct |
1 ms |
844 KB |
Output is correct |
29 |
Correct |
1 ms |
956 KB |
Output is correct |
30 |
Correct |
1 ms |
844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
844 KB |
Output is correct |
22 |
Correct |
1 ms |
844 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
1 ms |
844 KB |
Output is correct |
25 |
Correct |
1 ms |
844 KB |
Output is correct |
26 |
Correct |
1 ms |
844 KB |
Output is correct |
27 |
Correct |
1 ms |
844 KB |
Output is correct |
28 |
Correct |
1 ms |
844 KB |
Output is correct |
29 |
Correct |
1 ms |
956 KB |
Output is correct |
30 |
Correct |
1 ms |
844 KB |
Output is correct |
31 |
Correct |
11 ms |
10372 KB |
Output is correct |
32 |
Correct |
12 ms |
10368 KB |
Output is correct |
33 |
Correct |
13 ms |
10372 KB |
Output is correct |
34 |
Correct |
14 ms |
10316 KB |
Output is correct |
35 |
Correct |
13 ms |
10292 KB |
Output is correct |
36 |
Correct |
15 ms |
10316 KB |
Output is correct |
37 |
Correct |
12 ms |
10416 KB |
Output is correct |
38 |
Correct |
12 ms |
10316 KB |
Output is correct |
39 |
Correct |
12 ms |
10416 KB |
Output is correct |
40 |
Correct |
12 ms |
10416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
844 KB |
Output is correct |
22 |
Correct |
1 ms |
844 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
1 ms |
844 KB |
Output is correct |
25 |
Correct |
1 ms |
844 KB |
Output is correct |
26 |
Correct |
1 ms |
844 KB |
Output is correct |
27 |
Correct |
1 ms |
844 KB |
Output is correct |
28 |
Correct |
1 ms |
844 KB |
Output is correct |
29 |
Correct |
1 ms |
956 KB |
Output is correct |
30 |
Correct |
1 ms |
844 KB |
Output is correct |
31 |
Correct |
11 ms |
10372 KB |
Output is correct |
32 |
Correct |
12 ms |
10368 KB |
Output is correct |
33 |
Correct |
13 ms |
10372 KB |
Output is correct |
34 |
Correct |
14 ms |
10316 KB |
Output is correct |
35 |
Correct |
13 ms |
10292 KB |
Output is correct |
36 |
Correct |
15 ms |
10316 KB |
Output is correct |
37 |
Correct |
12 ms |
10416 KB |
Output is correct |
38 |
Correct |
12 ms |
10316 KB |
Output is correct |
39 |
Correct |
12 ms |
10416 KB |
Output is correct |
40 |
Correct |
12 ms |
10416 KB |
Output is correct |
41 |
Correct |
651 ms |
391812 KB |
Output is correct |
42 |
Correct |
599 ms |
391976 KB |
Output is correct |
43 |
Correct |
609 ms |
391976 KB |
Output is correct |
44 |
Correct |
589 ms |
391996 KB |
Output is correct |
45 |
Correct |
618 ms |
391992 KB |
Output is correct |
46 |
Correct |
620 ms |
392260 KB |
Output is correct |
47 |
Correct |
676 ms |
392272 KB |
Output is correct |
48 |
Correct |
634 ms |
392168 KB |
Output is correct |
49 |
Correct |
617 ms |
392132 KB |
Output is correct |
50 |
Correct |
636 ms |
392260 KB |
Output is correct |
51 |
Correct |
655 ms |
392236 KB |
Output is correct |
52 |
Correct |
591 ms |
392316 KB |
Output is correct |
53 |
Correct |
604 ms |
392188 KB |
Output is correct |
54 |
Correct |
634 ms |
392328 KB |
Output is correct |
55 |
Correct |
630 ms |
392132 KB |
Output is correct |