QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#386865 | #7937. Fast XORting | carbon111# | RE | 1ms | 3616kb | C++20 | 1.6kb | 2024-04-11 20:50:14 | 2024-04-11 20:50:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FINISH cerr << "FINISH" << endl;
#define debug(x) cerr << #x << " == " << x << endl
#define el '\n'
#define fir first
#define sec second
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1000000007;
const int inf = 0x3f3f3f3f;
const int N = 400020;
int t[N], n, tot;
int lowbit(int x)
{
return x & (-x);
}
void add(int x, int val)
{
while (x <= n) {
t[x] += val;
x += lowbit(x);
}
}
int getsum(int x)
{
int rr = 0;
while (x >= 1) {
rr += t[x];
x -= lowbit(x);
}
return rr;
}
ll calc(vector<int>& a)
{
for (int i = 0; i <= n; i++)
t[i] = 0;
ll sum = 0;
for (int i = 1; i <= n; i++) {
add(a[i] + 1, 1);
sum += getsum(a[i]);
}
return n * (n - 1) / 2 - sum;
}
void getans(int now, vector<int>& a)
{
if (now == 0)
return;
vector<int> b = a;
for (int i = 1; i <= n; i++) {
b[i] ^= now;
}
ll sum1 = calc(a);
ll sum2 = calc(b);
/*debug(now);
for (int i = 1; i <= n; i++) {
cout << a[i] << " ";
}
cout << endl;
for (int i = 1; i <= n; i++) {
cout << b[i] << " ";
}
cout << endl;
cout << sum1 << " " << sum2 << endl;*/
if (sum1 > sum2 + 1) {
a = b;
}
getans(now / 2, a);
}
void solve()
{
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
ll ans = calc(a);
getans(n, a);
ans = min(ans, calc(a) + 1);
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3616kb
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: 3560kb
input:
8 2 0 1 3 4 5 6 7
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: -100
Runtime Error
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...