C. Jon Snow and his Favourite Number
time limit per test:4 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
Jon Snow now has to fight with White Walkers. He has n rangers, each of which has his own strength. Also Jon Snow has his favourite number x. Each ranger can fight with a white walker only if the strength of the white walker equals his strength. He however thinks that his rangers are weak and need to improve. Jon now thinks that if he takes the bitwise XOR of strengths of some of rangers with his favourite number x, he might get soldiers of high strength. So, he decided to do the following operation k times:
1. Arrange all the rangers in a straight line in the order of increasing strengths.
2. Take the bitwise XOR (is written as ) of the strength of each alternate ranger with x and update it’s strength.
Suppose, Jon has 5 rangers with strengths [9, 7, 11, 15, 5] and he performs the operation 1 time with x = 2. He first arranges them in the order of their strengths, [5, 7, 9, 11, 15]. Then he does the following: 1. The strength of first ranger is updated to , i.e. 7.
2. The strength of second ranger remains the same, i.e. 7.
3. The strength of third ranger is updated to , i.e. 11.
4. The strength of fourth ranger remains the same, i.e. 11.
5. The strength of fifth ranger is updated to , i.e. 13.
The new strengths of the 5 rangers are [7, 7, 11, 11, 13]
Now, Jon wants to know the maximum and minimum strength of the rangers after performing the above operations k times. He wants your help for this task. Can you help him?
Input
First line consists of three integers n, k, x (1 ≤ n ≤ 105, 0 ≤ k ≤ 105, 0 ≤ x ≤ 103) — number of rangers Jon has, the number of times Jon will carry out the operation and Jon’s favourite number respectively.
Second line consists of n integers representing the strengths of the rangers a1, a2, …, an (0 ≤ ai ≤ 103).
Output
Output two integers, the maximum and the minimum strength of the rangers after performing the operation k times.
Examples
Input
5 1 2
9 7 11 15 5
Output
13 7
Input
2 100000 569
605 986
Output
986 605
求:n个数与k异或k次(每次异或后从小到大排序),求最后一次运算结束后的最小值和最大值
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
typedef long long ll;
const int maxn = 1e5 + 7;
int a[maxn], maxx[maxn], minn[maxn];
int main() {
int n, k, x;
cin >> n >> k >> x;
rep(i, 1, n) cin >> a[i];
sort(a + 1, a + 1 + n);
maxx[1] = a[n];
minn[1] = a[1];
rep(i, 1, k) {
sort(a + 1, a + 1 + n);
maxx[i] = a[n];
minn[i] = a[1];
if (i >= 3) {
if (maxx[i] == maxx[i - 1] && minn[i] == minn[i - 1] &&
maxx[i] == maxx[i - 2] && minn[i] == minn[i - 2] &&
maxx[i] == maxx[i - 3] && minn[i] == minn[i - 3]) {
cout << a[n] << " " << a[1] << endl;
return 0;
}
}
for (int j = 1; j <= n; j += 2) a[j] ^= x;
}
sort(a + 1, a + 1 + n);
cout << a[n] << " " << a[1] << endl;
return 0;
}