codeforces 768C - Jon Snow and his Favourite Number

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 ≤ 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.

這一題照着模擬就會發現會出現循環,然後模一下就出來了。

AC代碼:

 

#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int n, k, x, flage;
int a[100010];
vector<int> d[100010];
int main() {
	int i, j, cnt=0;
	scanf("%d%d%d", &n, &k, &x);
	for (i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	for (i = 0; i < n; i++) {
		d[0].push_back(a[i]);
	}
	if (k == 0) {
		printf("%d %d", a[n - 1], a[0]);
		return 0;
	}
	while (1){
		cnt++;
		for (i = 0; i < n; i++) {
			if (i & 1) {
				d[cnt].push_back(d[cnt - 1][i]);
			}
			else {
				d[cnt].push_back(d[cnt-1][i]^x);
			}
		}
		sort(d[cnt].begin(), d[cnt].end());
		if (k == cnt) {
			printf("%d %d", d[cnt][n - 1], d[cnt][0]);
			return 0;
		}
		for (i = 0; i < cnt; i++) {
			flage = 1;
			for (j = 0; j < n; j++) {
				if (d[cnt][j] != d[i][j]) {
					flage = 0; 
					break;
				}
			}
			if (flage) {
				int temp = (k - (cnt - 1)) % (cnt - i);
				if (temp == 0) {
					printf("%d %d", d[cnt-1][n - 1], d[cnt-1][0]);
					return 0;
				}
				else {
					temp = i + temp - 1;
					printf("%d %d", d[temp][n - 1], d[temp][0]);
					return 0;
				}
				
				return 0;
			}
		}

	}
	return 0;
}
 

 

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