QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#442040 | #7937. Fast XORting | HKOI0# | WA | 26ms | 11608kb | C++14 | 1001b | 2024-06-15 04:39:50 | 2024-06-15 04:39:50 |
Judging History
answer
#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
#define int long long
using namespace std;
int a[1 << 18], ai[1 << 18];
struct BIT{
int bit[1 << 19];
void add(int p, int v) { ++p; for (int i = p; i < (1 << 19); i += i & -i) bit[i] += v; }
int qry(int p) { ++p; int ret = 0; for (int i = p; i; i -= i & -i) ret += bit[i]; return ret; }
} bit;
void solve(){
int n; cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i]; ai[a[i]] = i;
}
int inv = 0;
for (int i = n - 1; i >= 0; i--) {
inv += bit.qry(a[i]);
bit.add(a[i], 1);
}
int ans = 1;
for (int b = 0; (1 << b) < n; b++) {
int cnt = 0;
for (int j = 0; j < n; j += 1 << (b + 1)) {
for (int k = 0; k < (1 << b); k++) {
cnt += ai[j + k] < ai[j + (1 << b) + k];
}
}
ans += min(cnt, n / 2 - cnt);
}
cout << min(inv, ans) << '\n';
}
int32_t main(){
#ifndef LOCAL
cin.tie(0)->sync_with_stdio(false);
#endif
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9768kb
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: 9760kb
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: 26ms
memory: 11608kb
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:
1176939
result:
wrong answer 1st numbers differ - expected: '17137565829', found: '1176939'