QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#394552 | #6627. Line Town | RedreamMer | 0 | 0ms | 14208kb | C++23 | 3.1kb | 2024-04-20 16:03:14 | 2024-04-20 16:03:14 |
Judging History
answer
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <cstdio>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
#define PB push_back
#define int long long
#define ll long long
#define vi vector<int>
#define siz(a) ((int) ((a).size()))
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
void print(vi n) { rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1]; }
const int N = 5e5, inf = 1e18;
int a, s[N + 5], w1[N + 5], w2[N + 5], L[N + 5], R[N + 5], q[N + 5], top;
vi p[N + 5][2];
void add(int *w, int n, int m) {
w[a + 1] += m;
for(; n <= a; n += n & -n) w[n] += m;
}
int ask(int *w, int n) {
int res = 0;
for(; n; n -= n & -n) res += w[n];
return res;
}
int up(int *w, int n) {
return w[a + 1] - ask(w, n);
}
int low(int *w, int n) {
return ask(w, n - 1);
}
signed main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> a;
rep(i, 1, a) cin >> s[i], q[++top] = abs(s[i]);
sort(q + 1, q + top + 1);
top = unique(q + 1, q + top + 1) - q - 1;
rep(i, 1, a) if(s[i] != 0) {
int x = lower_bound(q + 1, q + top + 1, abs(s[i])) - q;
// cout << ((abs(s[i]) != s[i]) == (i & 1)) << endl;
p[x][(abs(s[i]) != s[i]) == (i & 1)].PB(i);
}
rep(i, 1, a) add(w1, i, 1);
int f[2] = {inf, 0}, siz = a;
per(i, top, 1) {
if(q[i] == 0) break;
sort(p[i][0].begin(), p[i][0].end());
sort(p[i][1].begin(), p[i][1].end());
int g[2] = {inf, inf}, b = siz(p[i][0]) + siz(p[i][1]);
for(int x : p[i][0]) add(w1, x, -1);
for(int x : p[i][1]) add(w1, x, -1);
rep(j, 1, b) L[j] = R[j] = inf;
rep(x, 0, 1) {
int y = x ^ (siz & 1);
vi::iterator j[2] = {p[i][0].begin(), p[i][1].begin()};
for(int o = x, siz = 1; ; ++siz, ++j[o], o ^= 1) {
if(j[o] == p[i][o].end()) break;
L[siz] = L[siz - 1] + up(w2, *j[o]) + low(w1, *j[o]);
add(w2, *j[o], 1);
}
for(; j[0] != p[i][0].end(); add(w2, *j[0], 1), ++j[0]);
for(; j[1] != p[i][1].end(); add(w2, *j[1], 1), ++j[1]);
for(int o = y, siz = 1; ; ++siz, o ^= 1) {
// cout << *j[o] << ' ' << j[o] - p[i][o].end() << endl;
if(j[o] == p[i][o].begin()) break;
--j[o];
add(w2, *j[o], -1);
R[siz] = R[siz - 1] + up(w2, *j[o]) + up(w1, *j[o]);
}
for(; j[0] != p[i][0].begin(); add(w2, *(--j[0]), -1));
for(; j[1] != p[i][1].begin(); add(w2, *(--j[1]), -1));
// if(q[i] == 2) cout << L[0] << ' ' << R[2] << ' ' << f[x] << ' ' << x << ' ' << y << endl;
rep(j, 0, b) g[x ^ (j & 1)] = min(g[x ^ (j & 1)], L[j] + R[b - j] + f[x]);
// return 0;
}
swap(f, g);
siz -= b;
// cout << f[0] << ' ' << f[1] << endl;
}
int ans = min(f[0], f[1]);
ans > a * (a - 1) / 2 ? cout << -1 : cout << ans;
return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 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: 0ms
memory: 12212kb
input:
10 1 1 1 1 1 -1 -1 -1 1 -1
output:
8
result:
wrong answer 1st numbers differ - expected: '-1', found: '8'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Wrong Answer
Test #60:
score: 0
Wrong Answer
time: 0ms
memory: 14208kb
input:
10 3 10 5 -9 7 2 -6 1 8 0
output:
13
result:
wrong answer 1st numbers differ - expected: '-1', found: '13'
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%