QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#371326 | #7937. Fast XORting | ishmeal# | WA | 94ms | 4740kb | C++23 | 1.1kb | 2024-03-30 05:11:21 | 2024-03-30 05:11:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
bool x = 0;
pair<int,int> solve(vector<int>::iterator it, int shift, int size) {
int zero = 0, one = 0;
for (int i = 0, seen1 = 0, seen0 = 0; i < size; i++) {
if ((*(it+i)) & (1 << shift)) one += i - seen1++;
else zero += i - seen0++;
}
return {zero, one};
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
vector<int> a(n); for (int &x : a) cin >> x;
long long ans = 0;
for (int shift = log2(n)-1, size = n; shift >= 0; shift--, size /= 2) {
pair<long long,long long> p {0,0};
for (int i = 0; i < n; i += size) {
auto t = solve(a.begin()+i, shift, size);
p.first += t.first, p.second += t.second;
}
ans += min(p.first, p.second);
if (p.second < p.first) {
x = 1;
for (int i = 0; i < n; i += size) {
stable_sort(a.begin()+i, a.begin()+i+size, [&](int a, int b) {
return (a & (1 << shift)) > (b & (1 << shift));
});
}
} else {
for (int i = 0; i < n; i += size) {
stable_sort(a.begin()+i, a.begin()+i+size, [&](int a, int b) {
return (a & (1 << shift)) < (b & (1 << shift));
});
}
}
}
cout << ans+x << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3708kb
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: 3716kb
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: 94ms
memory: 4740kb
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:
-1842773
result:
wrong answer 1st numbers differ - expected: '17137565829', found: '-1842773'