QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#128598 | #6626. Real Mountains | SanguineChameleon | 0 | 1ms | 19912kb | C++20 | 3.2kb | 2023-07-21 12:28:39 | 2023-07-21 12:28:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const long long inf = 1e18L + 20;
const int maxn = 5e5 + 20;
int a[maxn];
vector<int> groups[maxn];
pair<int, int> vals[maxn];
long long dp[maxn][2];
int bit1[maxn];
int bit2[maxn];
int zero;
void update(int bit[], int pos, int add) {
for (int i = pos; i < maxn; i += i & (-i)) {
bit[i] += add;
}
}
int get(int bit[], int pos) {
int res = 0;
for (int i = pos; i > 0; i -= i & (-i)) {
res += bit[i];
}
return res;
}
void calc(vector<int> &group, long long prv_dp[], long long cur_dp[]) {
if (a[group[0]] == 0) {
cur_dp[0] = 0;
cur_dp[1] = 0;
zero += group.size();
for (auto pos: group) {
update(bit1, pos, 1);
}
return;
}
int n = group.size();
sort(group.begin(), group.end());
cur_dp[0] = inf;
cur_dp[1] = inf;
vector<int> pos, neg;
for (int i = 0; i < n; i++){
if ((a[group[i]] > 0) ^ (group[i] & 1) ^ 1) {
pos.push_back(group[i]);
}
else {
neg.push_back(group[i]);
}
}
for (int iter = 0; iter < 2; iter++) {
int sz_pos0 = (int)pos.size();
int sz_neg0 = (int)neg.size();
int sz_pos1 = (n - 1) >> 1;
int sz_neg1 = n - 1 - sz_pos1;
int sz_pos2 = n >> 1;
int sz_neg2 = n - sz_pos2;
if (sz_pos0 < sz_pos1 || sz_neg0 < sz_neg1) {
pos.swap(neg);
continue;
}
if ((zero & 1) && (sz_pos0 < sz_pos2 || sz_neg0 < sz_neg2)) {
pos.swap(neg);
continue;
}
vector<int> order;
for (int i = 0; i < n - 1; i++) {
if (i & 1) {
order.push_back(pos[i >> 1]);
}
else {
order.push_back(neg[i >> 1]);
}
}
int cut = -1;
if (sz_pos0 > sz_pos1) {
order.push_back(pos.back());
cut = n - (n & 1);
}
else {
order.push_back(neg.back());
cut = n - ((n & 1) ^ 1);
}
long long cost = 0;
for (int i = 0; i < n; i++) {
if (i < cut) {
cost += get(bit1, order[i]);
}
else {
cost += zero - get(bit1, order[i]);
}
}
for (int i = n - 1; i >= 0; i--) {
cost += get(bit2, order[i]);
update(bit2, order[i], 1);
}
for (int i = 0; i < n; i++) {
update(bit2, order[i], -1);
}
int step = 2 - (zero & 1);
while (true) {
cur_dp[iter] = min(cur_dp[iter], cost + prv_dp[iter ^ (cut & 1)]);
if (cut < step) {
break;
}
for (int i = 1; i <= step; i++) {
cost += zero - get(bit1, order[cut - i]) * 2;
}
if (step == 2) {
cost += (order[cut - 2] < order[cut - 1]) ? 1 : -1;
}
cut -= step;
}
pos.swap(neg);
}
zero += n;
for (auto pos: group) {
update(bit1, pos, 1);
}
}
void just_do_it() {
int n;
cin >> n;
vals[0] = {-1, -1};
for (int i = 1; i <= n; i++) {
cin >> a[i];
vals[i] = {abs(a[i]), i};
}
sort(vals + 1, vals + n + 1);
int m = 0;
for (int i = 1; i <= n; i++) {
if (vals[i].first != vals[i - 1].first) {
m++;
}
groups[m].push_back(vals[i].second);
}
for (int i = 1; i <= m; i++) {
calc(groups[i], dp[i - 1], dp[i]);
}
if (dp[m][0] == inf) {
cout << -1;
}
else {
cout << dp[m][0];
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 19912kb
input:
3 29 9 9
output:
2
result:
wrong answer 1st numbers differ - expected: '0', found: '2'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Skipped
Dependency #2:
0%
Subtask #6:
score: 0
Skipped
Dependency #3:
0%
Subtask #7:
score: 0
Skipped
Dependency #1:
0%