This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 5e5 + 5;
const int oo = 1e18, mod = 1e9 + 7;
struct Fenwick {
int n;
int tree[N];
void init(int _n) {
n = _n;
for (int i = 1; i <= n; i++) tree[i] = 0;
}
void add(int x, int y) {
while (x <= n) {
tree[x] += y;
x += x & -x;
}
}
int sum(int x) {
int ret = 0;
while (x) {
ret += tree[x];
x -= x & -x;
}
return ret;
}
int sum(int l, int r) {
if (l > r) return 0;
return sum(r) - sum(l - 1);
}
} tree, treeL, treeR;
int n;
int arr[N];
int DP[2][2];
int ld[N], rd[N];
int inversion = 0;
bool impossible(int start0, int start1, int len0, int len1, int cnt0, int cnt1) {
int c0 = 0, c1 = 0;
c0 += (len0 % 2 && start0 == 0) ? (len0 + 1) / 2 : len0 / 2;
c1 += (len0 % 2 && start0 == 1) ? (len0 + 1) / 2 : len0 / 2;
c0 += (len1 % 2 && start1 == 0) ? (len1 + 1) / 2 : len1 / 2;
c1 += (len1 % 2 && start1 == 1) ? (len1 + 1) / 2 : len1 / 2;
if (c0 == cnt0 && c1 == cnt1) return false;
return true;
}
void addSetLeft(int x) {
inversion += ld[x];
inversion += treeL.sum(x + 1, n);
inversion += treeR.sum(1, x - 1);
treeL.add(x, 1);
}
void addSetRight(int x) {
inversion += rd[x];
inversion += treeR.sum(1, x - 1);
inversion += treeL.sum(x + 1, n);
treeR.add(x, 1);
}
void delSetLeft(int x) {
treeL.add(x, -1);
inversion -= treeR.sum(1, x - 1);
inversion -= treeL.sum(x + 1, n);
inversion -= ld[x];
}
void delSetRight(int x) {
treeR.add(x, -1);
inversion -= treeL.sum(x + 1, n);
inversion -= treeR.sum(1, x - 1);
inversion -= rd[x];
}
void process() {
cin >> n;
foru(i, 1, n) {
cin >> arr[i];
if (i % 2 == 0) arr[i] = -arr[i];
}
vector<pair<int, int> > vec;
for (int i = 1; i <= n; i++) vec.push_back(make_pair(abs(arr[i]), i));
sort(vec.rbegin(), vec.rend());
tree.init(n);
treeL.init(n);
treeR.init(n);
for (int i = 1; i <= n; i++) tree.add(i, 1);
for (int a = 0; a < 4; a++) DP[(a >> 1) & 1][a & 1] = oo;
DP[0][0] = 0;
int usedCnt = 0;
for (int s = 0; s < n; s++) {
if (vec[s].first == 0) break;
int e = s;
while (e + 1 < n && vec[s].first == vec[e + 1].first) e++;
reverse(vec.begin() + s, vec.begin() + e + 1);
vector<int> pos[2];
vector<int> rpos[2];
for (int i = s; i <= e; i++) tree.add(vec[i].second, -1);
for (int i = s; i <= e; i++) {
int x = vec[i].second;
ld[x] = tree.sum(1, x), rd[x] = tree.sum(x, n);
if (arr[x] < 0) pos[0].push_back(x);
else pos[1].push_back(x);
}
rpos[0] = pos[0];
rpos[1] = pos[1];
reverse(rpos[0].begin(), rpos[0].end());
reverse(rpos[1].begin(), rpos[1].end());
int L = tree.sum(1, n);
int K = (int)pos[0].size() + (int)pos[1].size();
for (int js = 0; js < 2; js++) {
int je = (n % 2) ^ ((usedCnt - js) & 1);
for (int ta = 0; ta < 2; ta++) {
int tb = (K + ta) % 2;
if (impossible(js, je, ta, K - ta, (int)pos[0].size(), (int)pos[1].size())) continue;
int l = ta, r = K - ta;
inversion = 0;
for (int i = 1; i <= l; i++) {
if (i % 2 == 1) addSetLeft(pos[js][i / 2]);
else addSetLeft(pos[!js][(i - 1) / 2]);
}
for (int i = 1; i <= r; i++) {
if (i % 2 == 1) addSetRight(rpos[je][i / 2]);
else addSetRight(rpos[!je][(i - 1) / 2]);
}
DP[1][(js + ta) & 1] = min(DP[1][(js + ta) & 1], DP[0][js] + inversion);
while (r >= 2) {
for (int c = 0; c < 2; c++) {
if (r % 2 == 1) delSetRight(rpos[je][r / 2]);
else delSetRight(rpos[!je][(r - 1) / 2]);
r--, ++l;
if (l % 2 == 1) addSetLeft(pos[js][l / 2]);
else addSetLeft(pos[!js][(l - 1) / 2]);
}
DP[1][(js + ta) & 1] = min(DP[1][(js + ta) & 1], DP[0][js] + inversion);
}
while (l) {
if (l % 2 == 1) delSetLeft(pos[js][l / 2]);
else delSetLeft(pos[!js][(l - 1) / 2]);
l--;
}
while (r) {
if (r % 2 == 1) delSetRight(rpos[je][r / 2]);
else delSetRight(rpos[!je][(r - 1) / 2]);
r--;
}
}
}
s = e;
for (int a = 0; a < 2; a++) DP[0][a] = DP[1][a], DP[1][a] = oo;
usedCnt += K;
}
int ans = min(DP[0][0], DP[0][1]);
printf("%lld", ans == oo ? -1 : ans);
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void process()':
Main.cpp:145:21: warning: unused variable 'tb' [-Wunused-variable]
145 | int tb = (K + ta) % 2;
| ^~
Main.cpp:139:13: warning: unused variable 'L' [-Wunused-variable]
139 | int L = tree.sum(1, n);
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |