Codeforces Round #399 (Div. 1 + Div. 2, combined)C. Jon Snow and his Favourite Number

来源:互联网 发布:程序员去汽车厂 编辑:程序博客网 时间:2024/05/18 12:34

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 29 7 11 15 5
output
13 7
input
2 100000 569605 986
output
986 605

因为ai的范围很小,所以可以通过记录每个数的个数来记录这个序列,能想到这样做基本就可以了

然后模拟k次,每次从0开始,一直到1024(因为最大是1000,异或之后可能出现的最大值是1024),根据每个数前面已经出现了几个可以判断这个数第一次出现在按从小到大的排序的序列里是否为第奇数个

例如  1 1 2 2 2 3 3 3

有两个1,并且前面没有任何数,所以这两个1里有一个要异或x,一个不变,三个2,,2前面有两个数,所以第一个2是位于奇数的位置,这三个2里有两个要异或x,一个不变,

三个3,而第一个3是位于偶数的位置,所以三个3里只有一个要异或x

#include<stdio.h>#include<string.h> #include<algorithm>using namespace std;#define MEM(a) memset(a,0,sizeof(a));#define res(i,be,n) for(i=be;i<n;i++)int main(){int n,x,k,a[1030],b[1030],t,i,j,re;scanf("%d %d %d",&n,&k,&x);MEM(a);MEM(b);res(i,0,n){scanf("%d",&j);a[j]++;}res(i,0,k){t=0;res(j,0,1025){b[j]=a[j];}MEM(a);res(j,0,1025){re=(b[j]+!(t&1))/2;a[j^x]+=re;a[j]+=b[j]-re;t+=b[j];}}int maxx=0,minn=1024;res(i,0,1025)if(a[i]){maxx=max(maxx,i);minn=min(minn,i);}printf("%d %d\n",maxx,minn);return 0;}




0 0
原创粉丝点击