#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
class fenw{
vector <ll> bit;
ll vel;
public:
fenw(ll a){
vel = a;
bit.resize(a+1);
}
void upd(ll x, ll val){
while (x <= vel){
bit[x] += val;
x += x&-x;
}
}
ll sum(ll x){
ll s = 0;
while (x > 0){
s += bit[x];
x -= x&-x;
}
return s;
}
};
int main()
{
ll n;
cin >> n;
vector <ll> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
fenw bit1(n);
vector <ll> poz(n+1);
for (int i = 0; i < n; i++)
poz[a[i]] = i + 1;
vector <ll> dp(n+1, 1000000000000000000);
dp[0] = 0;
for (int i = 0; i < n; i++){
fenw inv(n);
ll brin = 0;
ll dodatne_poz = 0;
ll trenutne_poz = 0;
for (int j = i + 1; j <= n; j++){
trenutne_poz += j;
dodatne_poz += poz[j] + bit1.sum(n) - bit1.sum(poz[j]);
//cout << trenutne_poz << " " << dodatne_poz << endl;
brin += inv.sum(n) - inv.sum(poz[j]);
//cout << brin << endl;
inv.upd(poz[j], 1);
ll tren = dp[i] + dodatne_poz - trenutne_poz;
ll manji = min(brin, ((j-i)*(j-i-1))/2 - brin);
tren += manji;
dp[j] = min(dp[j], tren);
}
bit1.upd(poz[i+1], 1);
}
cout << dp[n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
272 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 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 |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
272 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 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 |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
288 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
292 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
272 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 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 |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
288 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
292 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
2 ms |
292 KB |
Output is correct |
24 |
Correct |
1 ms |
288 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
292 KB |
Output is correct |
27 |
Correct |
2 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
272 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 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 |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
288 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
292 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
2 ms |
292 KB |
Output is correct |
24 |
Correct |
1 ms |
288 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
292 KB |
Output is correct |
27 |
Correct |
2 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
12 ms |
204 KB |
Output is correct |
32 |
Correct |
13 ms |
204 KB |
Output is correct |
33 |
Correct |
23 ms |
328 KB |
Output is correct |
34 |
Correct |
11 ms |
204 KB |
Output is correct |
35 |
Correct |
10 ms |
324 KB |
Output is correct |
36 |
Correct |
14 ms |
320 KB |
Output is correct |
37 |
Correct |
10 ms |
324 KB |
Output is correct |
38 |
Correct |
12 ms |
320 KB |
Output is correct |
39 |
Correct |
10 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
272 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 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 |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
288 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
292 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
2 ms |
292 KB |
Output is correct |
24 |
Correct |
1 ms |
288 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
292 KB |
Output is correct |
27 |
Correct |
2 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
12 ms |
204 KB |
Output is correct |
32 |
Correct |
13 ms |
204 KB |
Output is correct |
33 |
Correct |
23 ms |
328 KB |
Output is correct |
34 |
Correct |
11 ms |
204 KB |
Output is correct |
35 |
Correct |
10 ms |
324 KB |
Output is correct |
36 |
Correct |
14 ms |
320 KB |
Output is correct |
37 |
Correct |
10 ms |
324 KB |
Output is correct |
38 |
Correct |
12 ms |
320 KB |
Output is correct |
39 |
Correct |
10 ms |
204 KB |
Output is correct |
40 |
Correct |
10 ms |
204 KB |
Output is correct |
41 |
Correct |
607 ms |
488 KB |
Output is correct |
42 |
Correct |
606 ms |
492 KB |
Output is correct |
43 |
Correct |
615 ms |
492 KB |
Output is correct |
44 |
Correct |
620 ms |
492 KB |
Output is correct |
45 |
Correct |
665 ms |
488 KB |
Output is correct |
46 |
Correct |
601 ms |
484 KB |
Output is correct |
47 |
Correct |
569 ms |
488 KB |
Output is correct |
48 |
Correct |
575 ms |
492 KB |
Output is correct |
49 |
Correct |
566 ms |
488 KB |
Output is correct |
50 |
Correct |
592 ms |
484 KB |
Output is correct |
51 |
Correct |
603 ms |
488 KB |
Output is correct |
52 |
Correct |
582 ms |
484 KB |
Output is correct |
53 |
Correct |
576 ms |
484 KB |
Output is correct |
54 |
Correct |
616 ms |
484 KB |
Output is correct |
55 |
Correct |
568 ms |
488 KB |
Output is correct |