Codeforces768C-Jon Snow and his Favourite Number

原題鏈接

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 nkx (1 ≤ n ≤ 1050 ≤ k ≤ 1050 ≤ 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

純暴力,對所有數進行桶排序,d[e][i]表示i這個數的個數,然後對於每種數i算出他在奇數位置上的個數s個,d[e^1][i^x] += s,然後剩餘的數不變,d[e^1][i] += d[e][i] - s;

#include <bits/stdc++.h>
#define maxn 1000005
#define MOD 1000000007
using namespace std;
typedef long long ll;

int d[2][2505];
int main(){
//	freopen("in.txt", "r", stdin); 
	int n, k, x, a;
	scanf("%d%d%d", &n, &k, &x);
	for(int i = 0; i < n; i++){
		scanf("%d", &a);
		d[0][a]++;
	}
	int e = 0;
	while(k--){
		int cnt = 0;
		for(int i = 0; i <= 2500; i++){
			int s = 0;
			if(d[e][i]){
				if(cnt % 2 == 0){
					s += d[e][i] / 2;
					if(d[e][i]&1)
					 s++;
					d[e^1][i^x] += s;
				}
				else{
					s += d[e][i] / 2;
					d[e^1][i^x] += s;
				}
				d[e^1][i] += d[e][i] - s;
				cnt += d[e][i];
				d[e][i] = 0;
			}
		}
		e ^= 1;
	}
	for(int i = 2500; i >= 0; i--){
		if(d[e][i]){
			printf("%d", i);
			break;
		}
	}
	for(int i = 0; i <= 2500; i++){
		if(d[e][i]){
			printf(" %d", i);
			break;
		}
	}
	puts("");
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章