QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#378110 | #7937. Fast XORting | tylerm390# | WA | 41ms | 6200kb | C++20 | 1.6kb | 2024-04-06 04:03:15 | 2024-04-06 04:03:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ll = long long;
using pii = pair<int, int>;
using vvi = vector<vi>;
#define rep(i, a, b) for(int i = a; i < (b); i++)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
struct FT {
vector<ll> s;
FT(int n): s(n) {}
void update(int pos, ll dif) {
for (; pos < sz(s); pos |= pos+1) s[pos] += dif;
}
ll query(int pos) {
ll res = 0;
for (; pos > 0; pos &= pos-1) res += s[pos-1];
return res;
}
};
ll count_invs(const vector<int>& arr) {
FT tree(sz(arr));
ll res = 0;
for (int i = sz(arr)-1; i >= 0; i--) {
res += tree.query(arr[i]);
tree.update(arr[i], 1);
}
return res;
}
void solve() {
int n;
cin >> n;
vi arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
ll res = count_invs(arr);
ll res2 = 1;
for (int bit = __lg(n); bit > 0; bit--) {
vector<int> num1s(n>>bit);
vector<int> tot(n>>bit);
ll tot_pairs = 0, invs = 0;
for (int i = 0; i < n; i++) {
int idx = arr[i]>>bit;
int val = (arr[i]>>(bit-1))&1;
tot_pairs += tot[idx];
tot[idx]++;
if (val == 0) invs += num1s[idx];
num1s[idx] += val;
}
res2 += min(invs, tot_pairs-invs);
}
cout << min(res, res2) << "\n";
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3592kb
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: 3816kb
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: 41ms
memory: 6200kb
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'