#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];
namespace sub2 {
int pref[N];
void solve() {
int answer = MAX;
{ // the number of 1 is odd
deque<int> odd, even;
for (int i = 1; i <= n; ++i) {
auto& vt = (i ^ (h[i] == 1)) & 1 ? odd : even;
vt.push_back(i);
}
vector<int> sequence;
for (int i = 1; i < n; ++i) {
auto& ret = pref[i];
ret = pref[i - 1];
auto& vt = i & 1 ? odd : even;
if (!vt.size()) { ret = MAX; continue; }
sequence.push_back(vt.front());
ret += max(0ll, i - vt.front());
vt.pop_front();
}
if (pref[n - 1] < MAX) {
answer = min(answer, pref[n - 1]);
int total = 0;
{ // i == n
int i = n;
auto& ret = pref[i];
ret = pref[i - 1];
auto& vt = (i ^ 1) & 1 ? odd : even;
if (!vt.size()) ret = MAX;
sequence.push_back(vt.front());
ret += max(0ll, i - vt.front());
total += max(0ll, i - vt.front());
vt.pop_front();
}
for (int i = n - 2; i >= 1; i -= 2) {
swap(sequence[i - 1], sequence[i]);
total += max(0ll, i - sequence[i - 1]);
total += max(0ll, i + 1 - sequence[i]);
answer = min(answer, pref[i - 1] + total);
}
}
}
{ // the number of 1 is even
deque<int> odd, even;
for (int i = 1; i <= n; ++i) {
auto& vt = (i ^ (h[i] == 1)) & 1 ? odd : even;
vt.push_back(i);
}
vector<int> sequence;
for (int i = 1; i <= n; ++i) {
auto& ret = pref[i];
ret = pref[i - 1];
auto& vt = i & 1 ? odd : even;
if (!vt.size()) { ret = MAX; continue; }
sequence.push_back(vt.front());
ret += max(0ll, i - vt.front());
vt.pop_front();
}
if (pref[n] < MAX) {
answer = min(answer, pref[n]);
int total = 0;
for (int i = n - 1; i >= 1; i -= 2) {
swap(sequence[i - 1], sequence[i]);
total += max(0ll, i - sequence[i - 1]);
total += max(0ll, i + 1 - sequence[i]);
answer = min(answer, pref[i - 1] + total);
}
}
}
cout << (answer == MAX ? -1 : answer) << "\n";
}
}
namespace sub3 {
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;
}
void solve() {
int cntZero = 0;
for (int i = 1; i <= n; ++i) {
if (!h[i]) cntZero += 1;
}
int answer = MAX;
for (int negNum = 0; negNum <= n - cntZero; ++negNum) {
auto cal = [&]() {
deque<int> odd, even;
for (int i = 1; i <= n; ++i) {
if (h[i]) {
auto& vt = ((i ^ (h[i] == 1)) & 1) ? odd : even;
vt.push_back(i);
}
}
memset(bit, 0, sizeof bit);
int ret = 0;
for (int i = 1; i <= negNum; ++i) {
auto& vt = i & 1 ? odd : even;
if (!vt.size()) return MAX;
ret += vt.front() - 1 - que(vt.front() - 1);
upd(vt.front(), 1);
vt.pop_front();
}
for (int i = 1; i <= n; ++i) {
if (h[i]) continue;
ret += i - 1 - que(i - 1);
upd(i, 1);
}
for (int i = negNum + cntZero + 1; i <= n; ++i) {
auto& vt = (i ^ 1) & 1 ? odd : even;
if (!vt.size()) return MAX;
ret += vt.front() - 1 - que(vt.front() - 1);
upd(vt.front(), 1);
vt.pop_front();
}
return ret;
};
answer = min(answer, cal());
}
cout << (answer == MAX ? -1 : answer) << "\n";
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> h[i];
if (all_of(h + 1, h + n + 1, [](const auto& a) { return abs(a) == 1; })) {
sub2::solve();
return 0;
}
if (all_of(h + 1, h + n + 1, [](const auto& a) { return abs(a) <= 1; })) {
sub3::solve();
return 0;
}
assert(false);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2556 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
2 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2556 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
2 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
45 ms |
21800 KB |
Output is correct |
16 |
Correct |
50 ms |
21428 KB |
Output is correct |
17 |
Correct |
42 ms |
21432 KB |
Output is correct |
18 |
Correct |
46 ms |
21620 KB |
Output is correct |
19 |
Correct |
52 ms |
21624 KB |
Output is correct |
20 |
Correct |
48 ms |
21632 KB |
Output is correct |
21 |
Correct |
44 ms |
21624 KB |
Output is correct |
22 |
Correct |
44 ms |
21624 KB |
Output is correct |
23 |
Correct |
44 ms |
21484 KB |
Output is correct |
24 |
Correct |
45 ms |
21624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2556 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
2 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
4 ms |
4176 KB |
Output is correct |
15 |
Correct |
4 ms |
4176 KB |
Output is correct |
16 |
Correct |
4 ms |
4176 KB |
Output is correct |
17 |
Correct |
110 ms |
4408 KB |
Output is correct |
18 |
Correct |
94 ms |
4176 KB |
Output is correct |
19 |
Correct |
107 ms |
6736 KB |
Output is correct |
20 |
Correct |
94 ms |
6620 KB |
Output is correct |
21 |
Correct |
140 ms |
6736 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
86 ms |
6796 KB |
Output is correct |
24 |
Correct |
103 ms |
6736 KB |
Output is correct |
25 |
Correct |
95 ms |
6736 KB |
Output is correct |
26 |
Correct |
100 ms |
6736 KB |
Output is correct |
27 |
Correct |
136 ms |
6736 KB |
Output is correct |
28 |
Correct |
134 ms |
4432 KB |
Output is correct |
29 |
Correct |
113 ms |
6804 KB |
Output is correct |
30 |
Correct |
2 ms |
6736 KB |
Output is correct |
31 |
Correct |
2 ms |
6788 KB |
Output is correct |
32 |
Correct |
2 ms |
6736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2556 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
2 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
45 ms |
21800 KB |
Output is correct |
16 |
Correct |
50 ms |
21428 KB |
Output is correct |
17 |
Correct |
42 ms |
21432 KB |
Output is correct |
18 |
Correct |
46 ms |
21620 KB |
Output is correct |
19 |
Correct |
52 ms |
21624 KB |
Output is correct |
20 |
Correct |
48 ms |
21632 KB |
Output is correct |
21 |
Correct |
44 ms |
21624 KB |
Output is correct |
22 |
Correct |
44 ms |
21624 KB |
Output is correct |
23 |
Correct |
44 ms |
21484 KB |
Output is correct |
24 |
Correct |
45 ms |
21624 KB |
Output is correct |
25 |
Correct |
4 ms |
4176 KB |
Output is correct |
26 |
Correct |
4 ms |
4176 KB |
Output is correct |
27 |
Correct |
4 ms |
4176 KB |
Output is correct |
28 |
Correct |
110 ms |
4408 KB |
Output is correct |
29 |
Correct |
94 ms |
4176 KB |
Output is correct |
30 |
Correct |
107 ms |
6736 KB |
Output is correct |
31 |
Correct |
94 ms |
6620 KB |
Output is correct |
32 |
Correct |
140 ms |
6736 KB |
Output is correct |
33 |
Correct |
1 ms |
2384 KB |
Output is correct |
34 |
Correct |
86 ms |
6796 KB |
Output is correct |
35 |
Correct |
103 ms |
6736 KB |
Output is correct |
36 |
Correct |
95 ms |
6736 KB |
Output is correct |
37 |
Correct |
100 ms |
6736 KB |
Output is correct |
38 |
Correct |
136 ms |
6736 KB |
Output is correct |
39 |
Correct |
134 ms |
4432 KB |
Output is correct |
40 |
Correct |
113 ms |
6804 KB |
Output is correct |
41 |
Correct |
2 ms |
6736 KB |
Output is correct |
42 |
Correct |
2 ms |
6788 KB |
Output is correct |
43 |
Correct |
2 ms |
6736 KB |
Output is correct |
44 |
Execution timed out |
2053 ms |
13328 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
4688 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
4688 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2556 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
2 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
4 ms |
4176 KB |
Output is correct |
15 |
Correct |
4 ms |
4176 KB |
Output is correct |
16 |
Correct |
4 ms |
4176 KB |
Output is correct |
17 |
Correct |
110 ms |
4408 KB |
Output is correct |
18 |
Correct |
94 ms |
4176 KB |
Output is correct |
19 |
Correct |
107 ms |
6736 KB |
Output is correct |
20 |
Correct |
94 ms |
6620 KB |
Output is correct |
21 |
Correct |
140 ms |
6736 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
86 ms |
6796 KB |
Output is correct |
24 |
Correct |
103 ms |
6736 KB |
Output is correct |
25 |
Correct |
95 ms |
6736 KB |
Output is correct |
26 |
Correct |
100 ms |
6736 KB |
Output is correct |
27 |
Correct |
136 ms |
6736 KB |
Output is correct |
28 |
Correct |
134 ms |
4432 KB |
Output is correct |
29 |
Correct |
113 ms |
6804 KB |
Output is correct |
30 |
Correct |
2 ms |
6736 KB |
Output is correct |
31 |
Correct |
2 ms |
6788 KB |
Output is correct |
32 |
Correct |
2 ms |
6736 KB |
Output is correct |
33 |
Runtime error |
6 ms |
4688 KB |
Execution killed with signal 6 |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2556 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
2 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
2 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
45 ms |
21800 KB |
Output is correct |
16 |
Correct |
50 ms |
21428 KB |
Output is correct |
17 |
Correct |
42 ms |
21432 KB |
Output is correct |
18 |
Correct |
46 ms |
21620 KB |
Output is correct |
19 |
Correct |
52 ms |
21624 KB |
Output is correct |
20 |
Correct |
48 ms |
21632 KB |
Output is correct |
21 |
Correct |
44 ms |
21624 KB |
Output is correct |
22 |
Correct |
44 ms |
21624 KB |
Output is correct |
23 |
Correct |
44 ms |
21484 KB |
Output is correct |
24 |
Correct |
45 ms |
21624 KB |
Output is correct |
25 |
Correct |
4 ms |
4176 KB |
Output is correct |
26 |
Correct |
4 ms |
4176 KB |
Output is correct |
27 |
Correct |
4 ms |
4176 KB |
Output is correct |
28 |
Correct |
110 ms |
4408 KB |
Output is correct |
29 |
Correct |
94 ms |
4176 KB |
Output is correct |
30 |
Correct |
107 ms |
6736 KB |
Output is correct |
31 |
Correct |
94 ms |
6620 KB |
Output is correct |
32 |
Correct |
140 ms |
6736 KB |
Output is correct |
33 |
Correct |
1 ms |
2384 KB |
Output is correct |
34 |
Correct |
86 ms |
6796 KB |
Output is correct |
35 |
Correct |
103 ms |
6736 KB |
Output is correct |
36 |
Correct |
95 ms |
6736 KB |
Output is correct |
37 |
Correct |
100 ms |
6736 KB |
Output is correct |
38 |
Correct |
136 ms |
6736 KB |
Output is correct |
39 |
Correct |
134 ms |
4432 KB |
Output is correct |
40 |
Correct |
113 ms |
6804 KB |
Output is correct |
41 |
Correct |
2 ms |
6736 KB |
Output is correct |
42 |
Correct |
2 ms |
6788 KB |
Output is correct |
43 |
Correct |
2 ms |
6736 KB |
Output is correct |
44 |
Execution timed out |
2053 ms |
13328 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |