4 seconds
256 megabytes
standard input
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:
- Arrange all the rangers in a straight line in the order of increasing strengths.
- 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:
- The strength of first ranger is updated to ,
i.e. 7. - The strength of second ranger remains the same, i.e. 7.
- The strength of third ranger is updated to ,
i.e. 11. - The strength of fourth ranger remains the same, i.e. 11.
- 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?
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 two integers, the maximum and the minimum strength of the rangers after performing the operation k times.
5 1 2
9 7 11 15 5
13 7
2 100000 569
605 986
986 605
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
using namespace std;
int cnt[2][2005];
int n,k,m; int main()
{
int x;
while(~scanf("%d%d%d",&n,&m,&k))
{
memset(cnt,0,sizeof(cnt)); for(int i=0; i<n; i++)
{
scanf("%d",&x);
cnt[0][x]++;
}
for(int i=0; i<m; i++)
{
bool flag=0;
for(int j=0; j<2000; j++)
{
if(cnt[0][j]>0||cnt[1][j]>0)
{
if(cnt[0][j]%2==0)
{
int s=j^k;
if(s<=j)
cnt[0][s]+=cnt[0][j]/2;
else
cnt[1][s]+=cnt[0][j]/2;
cnt[0][j]/=2;
cnt[0][j]+=cnt[1][j];
cnt[1][j]=0;
}
else
{
int s=j^k;
if(flag==0)
{
if(s<=j)
cnt[0][s]+=cnt[0][j]/2+1;
else
cnt[1][s]+=cnt[0][j]/2+1;
cnt[0][j]/=2;
cnt[0][j]+=cnt[1][j];
cnt[1][j]=0;
flag=1;
}
else
{
if(s<=j)
cnt[0][s]+=cnt[0][j]/2;
else
cnt[1][s]+=cnt[0][j]/2;
cnt[0][j]/=2;
cnt[0][j]++;
cnt[0][j]+=cnt[1][j];
cnt[1][j]=0;
flag=0;
} }
}
}
}
int mn,mx;
for(int i=0;i<=2000;i++)
{
if(cnt[0][i]>0)
{
mn=i;
break;
}
}
for(int i=2000;i>=0;i--)
{
if(cnt[0][i]>0)
{
mx=i;
break;
}
}
printf("%d %d\n",mx,mn);
}
return 0;
}