#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 500'000 + 10,
MAX = 1'000'000'000'000'000;
int n;
int h[N];
struct BIT {
int bit[N];
inline void upd(int i, int x) {
for (; i <= n; i += i & -i) bit[i] += x;
}
inline int que(int i) {
int ret = 0;
for (; i; i -= i & -i) ret += bit[i];
return ret;
}
} T, Z;
int pref[N];
vector<int> position;
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> h[i];
int cntZero = 0, totalZero = 0;
for (int i = 1; i <= n; ++i) {
if (h[i]) continue;
position.push_back(i);
cntZero += 1;
totalZero += i - 1 - Z.que(i - 1);
Z.upd(i, 1);
}
const int sz = n - cntZero;
int answer = MAX;
{ // the number of 1 is odd
deque<int> odd, even;
for (int i = 1; i <= n; ++i) {
if (!h[i]) continue;
auto& vt = (i ^ (h[i] == 1)) & 1 ? odd : even;
vt.push_back(i);
}
vector<int> sequence;
for (int i = 1; i < sz; ++i) {
auto& ret = pref[i];
ret = pref[i - 1];
auto& vt = i & 1 ? odd : even;
if (!vt.size()) { ret = MAX; continue; }
int pos = vt.front();
vt.pop_front();
ret += pos - 1 - T.que(pos - 1);
ret -= position.end() - upper_bound(position.begin(), position.end(), pos);
T.upd(pos, 1);
sequence.push_back(pos);
}
int total = totalZero;
{ //calculate total
auto& vt = (n ^ 1) & 1 ? odd : even;
if (!vt.size()) total = MAX;
else {
int pos = vt.front();
vt.pop_front();
total += pos - 1 - T.que(pos - 1) - Z.que(pos - 1);
sequence.push_back(pos);
}
}
if ((int)sequence.size() == sz) {
answer = min(answer, pref[sz - 1] + total);
for (int i = sz - 2; i >= 1; i -= 2) {
if (!(cntZero & 1)) swap(sequence[i - 1], sequence[i]);
T.upd(sequence[i - 1], -1);
T.upd(sequence[i], -1);
total += sequence[i - 1] - 1 - T.que(sequence[i - 1] - 1) - Z.que(sequence[i - 1] - 1);
T.upd(sequence[i - 1], 1);
total += sequence[i] - 1 - T.que(sequence[i] - 1) - Z.que(sequence[i] - 1);
T.upd(sequence[i], 1);
T.upd(sequence[i - 1], -1);
T.upd(sequence[i], -1);
answer = min(answer, pref[i - 1] + total);
}
}
}
{ // the number of 1 is even
memset(T.bit, 0, sizeof T.bit);
deque<int> odd, even;
for (int i = 1; i <= n; ++i) {
if (!h[i]) continue;
auto& vt = (i ^ (h[i] == 1)) & 1 ? odd : even;
vt.push_back(i);
}
vector<int> sequence;
for (int i = 1; i <= sz; ++i) {
auto& ret = pref[i];
ret = pref[i - 1];
auto& vt = i & 1 ? odd : even;
if (!vt.size()) { ret = MAX; continue; }
int pos = vt.front();
vt.pop_front();
ret += pos - 1 - T.que(pos - 1);
ret -= position.end() - upper_bound(position.begin(), position.end(), pos);
T.upd(pos, 1);
sequence.push_back(pos);
}
if ((int)sequence.size() == sz) {
int total = totalZero;
answer = min(answer, pref[sz] + total);
for (int i = sz - 1; i >= 1; i -= 2) {
if (!(cntZero & 1)) swap(sequence[i - 1], sequence[i]);
T.upd(sequence[i - 1], -1);
T.upd(sequence[i], -1);
total += sequence[i - 1] - 1 - T.que(sequence[i - 1] - 1) - Z.que(sequence[i - 1] - 1);
T.upd(sequence[i - 1], 1);
total += sequence[i] - 1 - T.que(sequence[i] - 1) - Z.que(sequence[i] - 1);
T.upd(sequence[i], 1);
T.upd(sequence[i - 1], -1);
T.upd(sequence[i], -1);
answer = min(answer, pref[i - 1] + total);
}
}
}
cout << (answer == MAX ? -1 : answer) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
1 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
5200 KB |
Output is correct |
5 |
Correct |
2 ms |
6480 KB |
Output is correct |
6 |
Correct |
2 ms |
5200 KB |
Output is correct |
7 |
Correct |
2 ms |
6480 KB |
Output is correct |
8 |
Correct |
2 ms |
6640 KB |
Output is correct |
9 |
Correct |
3 ms |
6480 KB |
Output is correct |
10 |
Correct |
2 ms |
5200 KB |
Output is correct |
11 |
Correct |
3 ms |
5200 KB |
Output is correct |
12 |
Correct |
2 ms |
5200 KB |
Output is correct |
13 |
Correct |
2 ms |
5084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
1 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
5200 KB |
Output is correct |
5 |
Correct |
2 ms |
6480 KB |
Output is correct |
6 |
Correct |
2 ms |
5200 KB |
Output is correct |
7 |
Correct |
2 ms |
6480 KB |
Output is correct |
8 |
Correct |
2 ms |
6640 KB |
Output is correct |
9 |
Correct |
3 ms |
6480 KB |
Output is correct |
10 |
Correct |
2 ms |
5200 KB |
Output is correct |
11 |
Correct |
3 ms |
5200 KB |
Output is correct |
12 |
Correct |
2 ms |
5200 KB |
Output is correct |
13 |
Correct |
2 ms |
5084 KB |
Output is correct |
14 |
Correct |
2 ms |
6652 KB |
Output is correct |
15 |
Correct |
103 ms |
25276 KB |
Output is correct |
16 |
Correct |
112 ms |
26036 KB |
Output is correct |
17 |
Correct |
100 ms |
25780 KB |
Output is correct |
18 |
Correct |
73 ms |
27196 KB |
Output is correct |
19 |
Correct |
99 ms |
25720 KB |
Output is correct |
20 |
Correct |
153 ms |
27444 KB |
Output is correct |
21 |
Correct |
150 ms |
27388 KB |
Output is correct |
22 |
Correct |
137 ms |
27324 KB |
Output is correct |
23 |
Correct |
160 ms |
27436 KB |
Output is correct |
24 |
Correct |
86 ms |
27320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
1 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
5200 KB |
Output is correct |
5 |
Correct |
2 ms |
6480 KB |
Output is correct |
6 |
Correct |
2 ms |
5200 KB |
Output is correct |
7 |
Correct |
2 ms |
6480 KB |
Output is correct |
8 |
Correct |
2 ms |
6640 KB |
Output is correct |
9 |
Correct |
3 ms |
6480 KB |
Output is correct |
10 |
Correct |
2 ms |
5200 KB |
Output is correct |
11 |
Correct |
3 ms |
5200 KB |
Output is correct |
12 |
Correct |
2 ms |
5200 KB |
Output is correct |
13 |
Correct |
2 ms |
5084 KB |
Output is correct |
14 |
Correct |
2 ms |
6992 KB |
Output is correct |
15 |
Correct |
1 ms |
6480 KB |
Output is correct |
16 |
Correct |
2 ms |
8528 KB |
Output is correct |
17 |
Correct |
2 ms |
6992 KB |
Output is correct |
18 |
Correct |
2 ms |
5200 KB |
Output is correct |
19 |
Correct |
2 ms |
6480 KB |
Output is correct |
20 |
Correct |
2 ms |
6704 KB |
Output is correct |
21 |
Correct |
2 ms |
6480 KB |
Output is correct |
22 |
Correct |
2 ms |
6480 KB |
Output is correct |
23 |
Correct |
2 ms |
5200 KB |
Output is correct |
24 |
Correct |
1 ms |
6624 KB |
Output is correct |
25 |
Correct |
1 ms |
6480 KB |
Output is correct |
26 |
Correct |
2 ms |
6648 KB |
Output is correct |
27 |
Correct |
2 ms |
5200 KB |
Output is correct |
28 |
Correct |
2 ms |
5368 KB |
Output is correct |
29 |
Correct |
2 ms |
5200 KB |
Output is correct |
30 |
Correct |
1 ms |
8528 KB |
Output is correct |
31 |
Correct |
2 ms |
8528 KB |
Output is correct |
32 |
Correct |
2 ms |
6984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
1 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
5200 KB |
Output is correct |
5 |
Correct |
2 ms |
6480 KB |
Output is correct |
6 |
Correct |
2 ms |
5200 KB |
Output is correct |
7 |
Correct |
2 ms |
6480 KB |
Output is correct |
8 |
Correct |
2 ms |
6640 KB |
Output is correct |
9 |
Correct |
3 ms |
6480 KB |
Output is correct |
10 |
Correct |
2 ms |
5200 KB |
Output is correct |
11 |
Correct |
3 ms |
5200 KB |
Output is correct |
12 |
Correct |
2 ms |
5200 KB |
Output is correct |
13 |
Correct |
2 ms |
5084 KB |
Output is correct |
14 |
Correct |
2 ms |
6652 KB |
Output is correct |
15 |
Correct |
103 ms |
25276 KB |
Output is correct |
16 |
Correct |
112 ms |
26036 KB |
Output is correct |
17 |
Correct |
100 ms |
25780 KB |
Output is correct |
18 |
Correct |
73 ms |
27196 KB |
Output is correct |
19 |
Correct |
99 ms |
25720 KB |
Output is correct |
20 |
Correct |
153 ms |
27444 KB |
Output is correct |
21 |
Correct |
150 ms |
27388 KB |
Output is correct |
22 |
Correct |
137 ms |
27324 KB |
Output is correct |
23 |
Correct |
160 ms |
27436 KB |
Output is correct |
24 |
Correct |
86 ms |
27320 KB |
Output is correct |
25 |
Correct |
2 ms |
6992 KB |
Output is correct |
26 |
Correct |
1 ms |
6480 KB |
Output is correct |
27 |
Correct |
2 ms |
8528 KB |
Output is correct |
28 |
Correct |
2 ms |
6992 KB |
Output is correct |
29 |
Correct |
2 ms |
5200 KB |
Output is correct |
30 |
Correct |
2 ms |
6480 KB |
Output is correct |
31 |
Correct |
2 ms |
6704 KB |
Output is correct |
32 |
Correct |
2 ms |
6480 KB |
Output is correct |
33 |
Correct |
2 ms |
6480 KB |
Output is correct |
34 |
Correct |
2 ms |
5200 KB |
Output is correct |
35 |
Correct |
1 ms |
6624 KB |
Output is correct |
36 |
Correct |
1 ms |
6480 KB |
Output is correct |
37 |
Correct |
2 ms |
6648 KB |
Output is correct |
38 |
Correct |
2 ms |
5200 KB |
Output is correct |
39 |
Correct |
2 ms |
5368 KB |
Output is correct |
40 |
Correct |
2 ms |
5200 KB |
Output is correct |
41 |
Correct |
1 ms |
8528 KB |
Output is correct |
42 |
Correct |
2 ms |
8528 KB |
Output is correct |
43 |
Correct |
2 ms |
6984 KB |
Output is correct |
44 |
Correct |
116 ms |
23288 KB |
Output is correct |
45 |
Correct |
102 ms |
24944 KB |
Output is correct |
46 |
Correct |
93 ms |
23872 KB |
Output is correct |
47 |
Correct |
79 ms |
23740 KB |
Output is correct |
48 |
Correct |
127 ms |
27396 KB |
Output is correct |
49 |
Correct |
145 ms |
27324 KB |
Output is correct |
50 |
Correct |
79 ms |
22844 KB |
Output is correct |
51 |
Correct |
112 ms |
22720 KB |
Output is correct |
52 |
Correct |
118 ms |
22720 KB |
Output is correct |
53 |
Correct |
61 ms |
22596 KB |
Output is correct |
54 |
Correct |
140 ms |
28696 KB |
Output is correct |
55 |
Correct |
115 ms |
26128 KB |
Output is correct |
56 |
Correct |
131 ms |
27700 KB |
Output is correct |
57 |
Correct |
37 ms |
18888 KB |
Output is correct |
58 |
Correct |
38 ms |
18812 KB |
Output is correct |
59 |
Correct |
42 ms |
18872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6480 KB |
Output is correct |
2 |
Incorrect |
2 ms |
6648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6480 KB |
Output is correct |
2 |
Incorrect |
2 ms |
6648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
1 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
5200 KB |
Output is correct |
5 |
Correct |
2 ms |
6480 KB |
Output is correct |
6 |
Correct |
2 ms |
5200 KB |
Output is correct |
7 |
Correct |
2 ms |
6480 KB |
Output is correct |
8 |
Correct |
2 ms |
6640 KB |
Output is correct |
9 |
Correct |
3 ms |
6480 KB |
Output is correct |
10 |
Correct |
2 ms |
5200 KB |
Output is correct |
11 |
Correct |
3 ms |
5200 KB |
Output is correct |
12 |
Correct |
2 ms |
5200 KB |
Output is correct |
13 |
Correct |
2 ms |
5084 KB |
Output is correct |
14 |
Correct |
2 ms |
6992 KB |
Output is correct |
15 |
Correct |
1 ms |
6480 KB |
Output is correct |
16 |
Correct |
2 ms |
8528 KB |
Output is correct |
17 |
Correct |
2 ms |
6992 KB |
Output is correct |
18 |
Correct |
2 ms |
5200 KB |
Output is correct |
19 |
Correct |
2 ms |
6480 KB |
Output is correct |
20 |
Correct |
2 ms |
6704 KB |
Output is correct |
21 |
Correct |
2 ms |
6480 KB |
Output is correct |
22 |
Correct |
2 ms |
6480 KB |
Output is correct |
23 |
Correct |
2 ms |
5200 KB |
Output is correct |
24 |
Correct |
1 ms |
6624 KB |
Output is correct |
25 |
Correct |
1 ms |
6480 KB |
Output is correct |
26 |
Correct |
2 ms |
6648 KB |
Output is correct |
27 |
Correct |
2 ms |
5200 KB |
Output is correct |
28 |
Correct |
2 ms |
5368 KB |
Output is correct |
29 |
Correct |
2 ms |
5200 KB |
Output is correct |
30 |
Correct |
1 ms |
8528 KB |
Output is correct |
31 |
Correct |
2 ms |
8528 KB |
Output is correct |
32 |
Correct |
2 ms |
6984 KB |
Output is correct |
33 |
Correct |
2 ms |
6480 KB |
Output is correct |
34 |
Incorrect |
2 ms |
6648 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
2 ms |
4944 KB |
Output is correct |
3 |
Correct |
1 ms |
6480 KB |
Output is correct |
4 |
Correct |
2 ms |
5200 KB |
Output is correct |
5 |
Correct |
2 ms |
6480 KB |
Output is correct |
6 |
Correct |
2 ms |
5200 KB |
Output is correct |
7 |
Correct |
2 ms |
6480 KB |
Output is correct |
8 |
Correct |
2 ms |
6640 KB |
Output is correct |
9 |
Correct |
3 ms |
6480 KB |
Output is correct |
10 |
Correct |
2 ms |
5200 KB |
Output is correct |
11 |
Correct |
3 ms |
5200 KB |
Output is correct |
12 |
Correct |
2 ms |
5200 KB |
Output is correct |
13 |
Correct |
2 ms |
5084 KB |
Output is correct |
14 |
Correct |
2 ms |
6652 KB |
Output is correct |
15 |
Correct |
103 ms |
25276 KB |
Output is correct |
16 |
Correct |
112 ms |
26036 KB |
Output is correct |
17 |
Correct |
100 ms |
25780 KB |
Output is correct |
18 |
Correct |
73 ms |
27196 KB |
Output is correct |
19 |
Correct |
99 ms |
25720 KB |
Output is correct |
20 |
Correct |
153 ms |
27444 KB |
Output is correct |
21 |
Correct |
150 ms |
27388 KB |
Output is correct |
22 |
Correct |
137 ms |
27324 KB |
Output is correct |
23 |
Correct |
160 ms |
27436 KB |
Output is correct |
24 |
Correct |
86 ms |
27320 KB |
Output is correct |
25 |
Correct |
2 ms |
6992 KB |
Output is correct |
26 |
Correct |
1 ms |
6480 KB |
Output is correct |
27 |
Correct |
2 ms |
8528 KB |
Output is correct |
28 |
Correct |
2 ms |
6992 KB |
Output is correct |
29 |
Correct |
2 ms |
5200 KB |
Output is correct |
30 |
Correct |
2 ms |
6480 KB |
Output is correct |
31 |
Correct |
2 ms |
6704 KB |
Output is correct |
32 |
Correct |
2 ms |
6480 KB |
Output is correct |
33 |
Correct |
2 ms |
6480 KB |
Output is correct |
34 |
Correct |
2 ms |
5200 KB |
Output is correct |
35 |
Correct |
1 ms |
6624 KB |
Output is correct |
36 |
Correct |
1 ms |
6480 KB |
Output is correct |
37 |
Correct |
2 ms |
6648 KB |
Output is correct |
38 |
Correct |
2 ms |
5200 KB |
Output is correct |
39 |
Correct |
2 ms |
5368 KB |
Output is correct |
40 |
Correct |
2 ms |
5200 KB |
Output is correct |
41 |
Correct |
1 ms |
8528 KB |
Output is correct |
42 |
Correct |
2 ms |
8528 KB |
Output is correct |
43 |
Correct |
2 ms |
6984 KB |
Output is correct |
44 |
Correct |
116 ms |
23288 KB |
Output is correct |
45 |
Correct |
102 ms |
24944 KB |
Output is correct |
46 |
Correct |
93 ms |
23872 KB |
Output is correct |
47 |
Correct |
79 ms |
23740 KB |
Output is correct |
48 |
Correct |
127 ms |
27396 KB |
Output is correct |
49 |
Correct |
145 ms |
27324 KB |
Output is correct |
50 |
Correct |
79 ms |
22844 KB |
Output is correct |
51 |
Correct |
112 ms |
22720 KB |
Output is correct |
52 |
Correct |
118 ms |
22720 KB |
Output is correct |
53 |
Correct |
61 ms |
22596 KB |
Output is correct |
54 |
Correct |
140 ms |
28696 KB |
Output is correct |
55 |
Correct |
115 ms |
26128 KB |
Output is correct |
56 |
Correct |
131 ms |
27700 KB |
Output is correct |
57 |
Correct |
37 ms |
18888 KB |
Output is correct |
58 |
Correct |
38 ms |
18812 KB |
Output is correct |
59 |
Correct |
42 ms |
18872 KB |
Output is correct |
60 |
Correct |
2 ms |
6480 KB |
Output is correct |
61 |
Incorrect |
2 ms |
6648 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |