This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 27.07.2023 20:51:29 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i-- > 0; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
// index from 0
template <class T = int>
struct Fenwick {
T *bit = nullptr;
int n;
Fenwick(int _n = 0) {
resize(_n);
}
void reset(void) {
fill(bit, bit + 1 + n, T(0));
}
void resize(int _n) {
if (bit != nullptr) delete[] bit;
n = _n;
bit = new T[n + 1];
reset();
}
void update(int i, T val) {
assert(0 <= i);
++i;
for (; i <= n; i += i & -i) bit[i] += val;
}
T get(int i) {
if (i < 0) return T(0);
++i;
i = min(i, n);
T res(0);
for (; i > 0; i -= i & -i)
res += bit[i];
return res;
}
int upper_bound(T val) {
int res = 0;
for (int i = __lg(n); i >= 0; --i) {
if ((res | MASK(i)) <= n && val >= bit[res | MASK(i)]) {
res |= MASK(i);
val -= bit[res];
}
}
return res;
}
int lower_bound(T val) {
int res = 0;
for (int i = __lg(n); i >= 0; --i) {
if ((res | MASK(i)) <= n && val > bit[res | MASK(i)]) {
res |= MASK(i);
val -= bit[res];
}
}
return res;
}
};
// end of template
const int MAX = 5e5 + 5;
const long long INF = 1e18;
int N, H[MAX], P[MAX];
void init(void) {
cin >> N;
REP(i, N) cin >> H[i];
}
void process(void) {
for (int i = 0; i < N; i += 2) H[i] = -H[i];
vector <int> order(N);
iota(ALL(order), 0);
sort(ALL(order), [&] (int a, int b) { return make_pair(abs(H[a]), -a) > make_pair(abs(H[b]), -b); });
Fenwick <int> bit_pref(N), bit_suff(N), cnt_zero(N);
REP(i, N) cnt_zero.update(i, 1);
long long dp[2] = {0, INF};
REP(l, N) {
if (!H[order[l]]) break;
int r = l;
while (r + 1 < N && abs(H[order[r + 1]]) == abs(H[order[l]])) ++r;
vector <tuple <int, long long, long long>> pos[2];
FORE(i, l, r) {
cnt_zero.update(order[i], -1);
int pref_zero = cnt_zero.get(order[i]);
int suff_zero = N - r - 1 - pref_zero;
if (H[order[i]] < 0) pos[0].emplace_back(order[i], pref_zero, suff_zero);
else pos[1].emplace_back(order[i], pref_zero, suff_zero);
}
REP(i, 2) {
REP(j, (int) pos[i].size() - 1) get<1>(pos[i][j + 1]) += get<1>(pos[i][j]);
REPD(j, (int) pos[i].size() - 1) get<2>(pos[i][j]) += get<2>(pos[i][j + 1]);
}
int sz = pos[0].size() + pos[1].size();
long long nxt[2] = {INF, INF};
REP(p, 2) {
int pl = !p;
int pr = !((N - l + p) & 1);
vector <int> pref(sz, 0), suff(sz, 0);
REP(i, sz) {
int v = pl ^ (i & 1);
if (i / 2 < pos[v].size()) pref[i] = get<0>(pos[v][i / 2]);
v = pr ^ (i & 1);
if (i / 2 < pos[v].size()) suff[sz - i - 1] = get<0>(pos[v][(int) pos[v].size() - i / 2 - 1]);
}
long long inv = 0;
REPD(i, sz) {
inv += bit_suff.get(suff[i] - 1);
bit_suff.update(suff[i], 1);
}
REP(i, sz + 1) {
int pos_pref[2], pos_suff[2];
pos_pref[pl] = (i + 1) / 2;
pos_pref[!pl] = i / 2;
pos_suff[pr] = (int) pos[pr].size() - (sz - i + 1) / 2;
pos_suff[!pr] = (int) pos[!pr].size() - (sz - i) / 2;
if (pos_pref[0] == pos_suff[0] && pos_pref[1] == pos_suff[1]) {
long long cur = inv;
REP(i, 2) {
if (pos_pref[i]) cur += get<1>(pos[i][pos_pref[i] - 1]);
if (pos_suff[i] < pos[i].size()) cur += get<2>(pos[i][pos_suff[i]]);
}
minimize(nxt[p ^ (i & 1)], dp[p] + cur);
}
if (i < sz) {
inv -= bit_suff.get(suff[i] - 1);
inv -= bit_pref.get(N - suff[i] - 2);
bit_suff.update(suff[i], -1);
inv += bit_suff.get(pref[i] - 1);
inv += bit_pref.get(N - pref[i] - 2);
bit_pref.update(N - pref[i] - 1, +1);
}
}
for (int i: pref) bit_pref.update(N - i - 1, -1);
}
dp[0] = nxt[0], dp[1] = nxt[1];
l = r;
}
long long res = min(dp[0], dp[1]);
cout << (res == INF ? -1 : res) << '\n';
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
file("cco23p3");
init();
process();
cerr << "Time elapsed: " << TIME << " s.\n";
return (0^0);
}
Compilation message (stderr)
Main.cpp: In function 'void process()':
Main.cpp:155:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
155 | if (i / 2 < pos[v].size()) pref[i] = get<0>(pos[v][i / 2]);
| ~~~~~~^~~~~~~~~~~~~~~
Main.cpp:157:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
157 | if (i / 2 < pos[v].size()) suff[sz - i - 1] = get<0>(pos[v][(int) pos[v].size() - i / 2 - 1]);
| ~~~~~~^~~~~~~~~~~~~~~
Main.cpp:177:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
177 | if (pos_suff[i] < pos[i].size()) cur += get<2>(pos[i][pos_suff[i]]);
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:203:2: note: in expansion of macro 'file'
203 | file("cco23p3");
| ^~~~
Main.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:203:2: note: in expansion of macro 'file'
203 | file("cco23p3");
| ^~~~
# | 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... |