QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#472016 | #7937. Fast XORting | ucup-team052# | WA | 40ms | 7672kb | C++14 | 2.0kb | 2024-07-11 13:15:45 | 2024-07-11 13:15:46 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef long long ll;
template <typename _T>
inline void read(_T &f) {
f = 0; _T fu = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); }
while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }
f *= fu;
}
template <typename T>
void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x < 10) putchar(x + 48);
else print(x / 10), putchar(x % 10 + 48);
}
template <typename T>
void print(T x, char t) {
print(x); putchar(t);
}
const int N = 18;
int a[1 << N], b[1 << N], pos[1 << N], f[1 << N | 5];
int n;
void add(int x, int y) {
++x;
while (x <= n) {
f[x] += y;
x += (x & -x);
}
}
int query(int x) {
++x;
int ans = 0;
while (x) {
ans += f[x];
x &= (x - 1);
}
return ans;
}
ll cnt[N], all[N];
void solve(int l, int r, int dep) {
if (l == r) return;
int mid = (l + r) >> 1;
solve(l, mid, dep + 1); solve(mid + 1, r, dep + 1);
all[dep] += 1ll * (mid - l + 1) * (r - mid);
int p1 = l, p2 = mid + 1, p3 = l;
while (p1 <= mid || p2 <= r) {
if (p1 <= mid && (p2 > r || a[p1] < a[p2])) {
b[p3] = a[p1]; ++p1; ++p3;
} else {
b[p3] = a[p2]; ++p2; ++p3; cnt[dep] += p1 - l;
}
}
for (int i = l; i <= r; i++) a[i] = b[i];
}
int main() {
read(n);
for (int i = 0; i < n; i++) read(a[i]), pos[a[i]] = i;
ll ans = 0;
for (int i = n - 1; i >= 0; i--) {
ans += query(a[i]);
add(a[i], 1);
}
solve(0, n - 1, 0);
ll sum = 1;
for (int i = 0; i < N; i++) {
sum += min(cnt[i], all[i] - cnt[i]);
}
ans = min(ans, sum);
print(ans, '\n');
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5724kb
input:
8 0 1 3 2 5 4 7 6
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 5728kb
input:
8 2 0 1 3 4 5 6 7
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Wrong Answer
time: 40ms
memory: 7672kb
input:
262144 47482 131703 90418 122675 166494 247529 196154 16950 66501 50357 246808 25929 10418 50538 26955 151884 63776 58023 20073 26544 74785 44064 41836 148543 87920 54172 3270 131495 130960 112122 167229 215767 77499 195004 21391 11039 168999 256346 109690 180904 172679 157200 78594 201857 52784 147...
output:
17140202023
result:
wrong answer 1st numbers differ - expected: '17137565829', found: '17140202023'