QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#393666 | #7937. Fast XORting | kyuukyuusha# | WA | 143ms | 13396kb | C++17 | 1.9kb | 2024-04-19 04:03:23 | 2024-04-19 04:03:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
int inversions(vector<int> a) {
int inversion = 0;
function<void(int, int)> mergesort = [&](int l, int r) {
if (r - l <= 1) return;
int m = l + (r - l) / 2;
mergesort(l, m);
mergesort(m, r);
vector<int> merged;
int i = l, j = m;
while (i < m && j < r) {
if (a[i] <= a[j]) {
merged.push_back(a[i++]);
} else {
inversion += m - i;
merged.push_back(a[j++]);
}
}
while (i < m) merged.push_back(a[i++]);
while (j < r) merged.push_back(a[j++]);
for (auto x : merged) a[l++] = x;
};
mergesort(0, a.size());
return inversion;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; ++i) {
cin >> a[i];
}
int bits = log2(n);
auto curr = a;
int xorval = 0;
for(int bit = bits - 1; bit >= 0; --bit) {
int mask = 1 << bit;
vector<int> ones;
vector<int> zeros;
int icnt = 0;
int n_ones = 0;
for (int i = 0; i < n; ++i) {
int x = curr[i];
if (i % (1 << (bit + 1)) == 0) n_ones = 0;
if (mask & x) {
ones.push_back(x & ~mask);
++n_ones;
} else {
zeros.push_back(x & ~mask);
icnt += n_ones;
}
}
xorval <<= 1;
if (icnt > (1 << bit)) {
xorval |= 1;
}
curr = zeros;
curr.insert(end(curr), begin(ones), end(ones));
}
int res = inversions(a);
for (auto& x : a) x ^= xorval;
res = min(res, inversions(a) + 1);
cout << res << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3648kb
input:
8 0 1 3 2 5 4 7 6
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3844kb
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: 143ms
memory: 13396kb
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:
17145325034
result:
wrong answer 1st numbers differ - expected: '17137565829', found: '17145325034'