QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#416771#7937. Fast XORtingpkien01WA 77ms5688kbC++231.3kb2024-05-22 07:01:162024-05-22 07:01:16

Judging History

你现在查看的是最新测评结果

  • [2024-05-22 07:01:16]
  • 评测
  • 测评结果:WA
  • 用时:77ms
  • 内存:5688kb
  • [2024-05-22 07:01:16]
  • 提交

answer

/*input
8
7 2 4 5 3 1 6 0
*/

#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

int n, lgn;
array<int, (1 << 18) + 1> arr;

long long countInversions(int l, int r) {
	if (l >= r) return 0ll;
	int mid = (l + r) >> 1;
	long long res = countInversions(l, mid) + countInversions(mid + 1, r);
	vector<int> merged; merged.reserve(r - l + 1);
	for (int i = l, j = mid + 1; i <= mid || j <= r;) {
		if (i > mid) {
			merged.push_back(arr[j++]);
		} else if (j > r) {
			merged.push_back(arr[i++]);
		} else if (arr[i] <= arr[j]) {
			merged.push_back(arr[i++]);
		} else {
			res += mid + 1 - i;
			merged.push_back(arr[j++]);
		}
	}
	move(merged.begin(), merged.end(), arr.begin() + l);
	return res;
}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) cin >> arr[i];
	lgn = round(log2(n));
	vector<array<int, 2>> cnt(lgn, array<int, 2>{});
	vector<array<long long, 2>> inv(lgn, array<long long, 2>{});

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < lgn; j++) {
			int jbit = arr[i] >> j & 1;
			cnt[j][jbit]++;
			inv[j][jbit] += cnt[j][jbit ^ 1];
		}
	}
	int x = 0;
	for (int j = 0; j < lgn; j++) {
		x |= (inv[j][0] > inv[j][1]) << j;
	}

	for (int i = 0; i < n; i++) arr[i] ^= x;
	cout << countInversions(0, n - 1) + (x != 0) << endl;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3508kb

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: 3588kb

input:

8
2 0 1 3 4 5 6 7

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 77ms
memory: 5688kb

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:

17137565829

result:

ok 1 number(s): "17137565829"

Test #4:

score: -100
Wrong Answer
time: 73ms
memory: 5568kb

input:

262144
14207 154745 21702 203084 49988 1853 104664 62971 102664 211126 35057 170586 75653 27474 189094 4988 207149 255640 140879 82517 224575 43198 49395 235775 251474 41544 258165 5501 13321 109993 9272 92321 55714 89738 14682 88517 145206 198849 196461 178924 145814 171363 135465 20289 15464 17330...

output:

17139892665

result:

wrong answer 1st numbers differ - expected: '17139892655', found: '17139892665'