QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#393664 | #7937. Fast XORting | kyuukyuusha# | WA | 118ms | 13420kb | C++17 | 2.3kb | 2024-04-19 03:37:50 | 2024-04-19 03:37:52 |
Judging History
answer
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#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;
for (auto x : curr) {
if (mask & x) {
ones.push_back(x & ~mask);
} else {
zeros.push_back(x & ~mask);
icnt += ones.size();
}
}
xorval <<= 1;
if (icnt > n) {
xorval |= 1;
curr = ones;
curr.insert(end(curr), begin(zeros), end(zeros));
} else {
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: 0ms
memory: 3624kb
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: 3792kb
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: 118ms
memory: 13420kb
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'