题干:

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.

Examples

Input

5 1 2
9 7 11 15 5

Output

13 7

Input

2 100000 569
605 986

Output

986 605

题目大意:

给你一组数,进行k组操作,每组操作中包含两个步骤,首先排序,然后对下标为奇数的数字进行异或操作。最后问你进行k组操作之后的数组中的最大值和最小值。

解题报告:

不是我就想知道这题打死也想不到会出现循环节的情况吧??!!?这也太、、可能这是异或运算的一种特性?反正我是第一次遇到,题目中会猜他存在循环节然后进行暴力的。。。然后,循环节是4不是2、所以其实代码中的(ci-3)是不需要进行判断的。(所以对于这种不知道循环节是几的这种题,保险起见建议那种精彩代码,直接Node结构体记录曾经算过的状态,并且用vector存下来!!技巧啊)

AC代码:

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int a[MAX];
int mx[MAX],mi[MAX];
int main()
{int n,k,x,ci;cin>>n>>k>>x;mx[0]=0,mi[0]=100005;for(int i = 1; i<=n; i++) scanf("%d",a+i),mx[0] = max(mx[0],a[i]),mi[0] = min(mi[0],a[i]);for(ci = 1; ci<=k; ci++) {sort(a+1,a+n+1);mx[ci] = -100005;//a[n];是错的 mi[ci] = 100005;//a[1];是错的 for(int i = 1; i<=n; i++) {if(i&1) a[i] = a[i] ^ x;mi[ci] = min(a[i],mi[ci]);mx[ci] = max(a[i],mx[ci]);}if(ci > 4) {if(mi[ci] == mi[ci-1] && mi[ci] == mi[ci-2] && mi[ci] == mi[ci-3] && mi[ci] == mi[ci-4])if(mx[ci] == mx[ci-1] && mx[ci] == mx[ci-2] && mx[ci] == mx[ci-3] && mx[ci] == mx[ci-4]) {printf("%d %d\n",mx[ci],mi[ci]);return 0 ;}}}
//  sort(a+1,a+n+1);printf("%d %d\n",mx[ci-1],mi[ci-1]);return 0 ;}

1WA代码:

需要i++,而不是i+=2直接做,因为你想啊,你这一轮操作的mx和mi,都是所有数字的,而不是选出来那些数的啊!!另外,不能直接每次都mx[ci] = a[n]这样,因为这一轮操作之后可能就没有这个最大值了,可能就被覆盖成别的值了。,。。所以说啊,写代码的时候一定要注意是否代码中存在小问题,这些问题叠加到一块就很致命了!!

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int a[MAX];
int mx[MAX],mi[MAX];
int main()
{int n,k,x,ci;cin>>n>>k>>x;mx[0]=0,mi[0]=100005;for(int i = 1; i<=n; i++) scanf("%d",a+i),mx[0] = max(mx[0],a[i]),mi[0] = min(mi[0],a[i]);for(ci = 1; ci<=k; ci++) {sort(a+1,a+n+1);mx[ci] = -100005;//a[n];是错的 mi[ci] = 100005;//a[1];是错的 for(int i = 1; i<=n; i+=2) {//这样写应该也是错的 a[i] = a[i] ^ x;mi[ci] = min(a[i],mi[ci]);mx[ci] = max(a[i],mx[ci]);}if(ci > 4) {if(mi[ci] == mi[ci-1] && mi[ci] == mi[ci-2] && mi[ci] == mi[ci-3] && mi[ci] == mi[ci-4])if(mx[ci] == mx[ci-1] && mx[ci] == mx[ci-2] && mx[ci] == mx[ci-3] && mx[ci] == mx[ci-4]) {printf("%d %d\n",mx[ci],mi[ci]);return 0 ;}}}sort(a+1,a+n+1);printf("%d %d\n",a[n],a[1]);return 0 ;}

另一个精彩的代码:

把每次变化的都存起来。然后得到一个新的状态的时候,先去跟之前存起来的那些状态比较,如果发现又相同的状态,那么就表示找到了循环节了。

之后求出来循环节的长度,然后取模,得到最后的答案。

(代码中可以直接return 0的,不需要goto,因为是codeforce的题。、。。)

#include <bits/stdc++.h>using namespace std;
const int MAXN=1e5+7;
int n,k,x;
int cnt;
struct node { //用来存储已经出现过的状态int num[MAXN];int MAX;int MIN;node() {MAX=0;MIN=1e9;}bool operator ==(const node &a)const {for(int i=0; i<n; ++i) {if(a.num[i]!=num[i])return 0;}return 1;}
} p;
vector<node>q;
int check() {for(int i=0; i<cnt; ++i) {if(q[i]==p)return i;}return -1;
}
int main() {int i;
xx:while(~scanf("%d%d%d",&n,&k,&x)) {q.clear();p.MAX=0;p.MIN=1e9;for(i=0; i<n; ++i) {scanf("%d",&p.num[i]);p.MAX=max(p.MAX,p.num[i]);p.MIN=min(p.MIN,p.num[i]);}sort(p.num,p.num+n);q.push_back(p);cnt=0;int pos;while(cnt<k) {cnt++;p.MAX=0;p.MIN=1e9;for(i=0; i<n; i+=2) {p.num[i]^=x;}for(i=0; i<n; ++i) {p.MAX=max(p.MAX,p.num[i]);p.MIN=min(p.MIN,p.num[i]);}sort(p.num,p.num+n);pos=check();if(pos!=-1) { //找到循环节了int t=cnt-pos;//循环节长度k=(k-pos)%t+pos;//取模之后的答案的下标printf("%d %d\n",q[k].MAX,q[k].MIN);goto xx;//找到了就直接重新读取下一组}q.push_back(p);}printf("%d %d\n",p.MAX,p.MIN);//表示没有找到循环节}return 0;
}

【CodeForces - 768C】Jon Snow and his Favourite Number(思维,技巧,套路,数学异或,循环节,trick)相关推荐

  1. codeforces 768 C. Jon Snow and his Favourite Number(思维+暴力)

    题目链接:http://codeforces.com/contest/768/problem/C 题意:给出n个数,k个操作,和一个x,每次操作先排序然后对奇数位数进行xor x操作,最后问k次操作后 ...

  2. C. Jon Snow and his Favourite Number DP + 注意数值大小

    http://codeforces.com/contest/768/problem/C 这题的数值大小只有1000,那么可以联想到,用数值做数组的下标,就是类似于计数排序那样子.. 这样就可以枚举k次 ...

  3. hdu1005 Number Sequence(寻找循环节)

    主题链接: huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力打表,找循环节,可是也不是那么eas ...

  4. Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或)

    Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或) 题意: 3种操作: 1 插入一个数 2 删除一个数 3 给出一个数 ...

  5. CodeForces 298A Snow Footprints

    1.http://codeforces.com/problemset/problem/298/A 2.题目大意:给定一个小球,可以往左往右移动,如果小球从第i个位置移到第i+1个位置,那么第i个位置将 ...

  6. Codeforces Round #491 (Div. 2) E - Bus Number + 反思

    E - Bus Number 最近感觉打CF各种车祸.....感觉要反思一下, 上次读错题,这次想当然地以为18!肯定暴了longlong 而没有去实践, 这个题我看到就感觉是枚举每个数字的个数,但是 ...

  7. Codeforces Round #608 (Div. 2) E. Common Number

    E. Common Number time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. 2018.12.05 codeforces 948C. Producing Snow(堆)

    传送门 维护一个堆. 每次先算出一个都不弹掉的总贡献. 然后把要弹掉的弹掉,并减去它们对应的贡献. 代码: #include<bits/stdc++.h> #define ri regis ...

  9. Last Theorem CodeForces - 1325F(dfs树找最大环+思维)

    It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, ...

最新文章

  1. 51nod百度之星2016练习赛
  2. 如何利用DTS数据同步功能,快速创建数据同步作业
  3. lol新服务器怎么发信息,《英雄联盟手游》国服新消息 最新玩法发布
  4. bootstrapV4.6.0之flex布局与float布局:对比
  5. 2018-2019-1 20165227 20165228 20165237 实验五 通讯协议设计
  6. qq语音按住ctrl就静音怎么解决_excel图形处理技巧:怎么制作出地摊经济的街景...
  7. UEFI win7系统的安装
  8. python下载网页方法_Python 下载网页的几种方法
  9. outlook qr码在哪里_聚合码微信支付宝申请开通,商家融合收款码实现一码支付,如何申请微信和支付宝合并收款码?聚合码支持信用卡、花呗、微信和支付宝收款...
  10. pygame编写井字棋游戏
  11. 随便说说,我回来啦~
  12. 家用计算机音效部件图示,唱吧新版自定义音效设置方法(附上最佳音效设置参数图)...
  13. IE浏览器极限提速完全攻略
  14. 读《明朝出了个张居正》有感
  15. 【数据结构】初识数据结构,十分钟带你玩转算法复杂度
  16. AI绘画工具软件网站合集:这些人工智能绘画生成器效果太赞了
  17. Codeforces 545 C Woodcutters(贪心/DP)
  18. Django框架学习——1—(虚拟环境搭建、MVC思想与Django的MVT区别、Django项目的创建、运行Django项目、项目结构介绍)
  19. Linux入门——ls的三个选项(l、h、a)及速配符(*、?、【】)的使用、clear、cd ~和隐藏文件
  20. 基于miu小波变换的人体步态数据检测和识别算法matlab仿真

热门文章

  1. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol52]42蒙哥马利乘法,哪里泄漏侧信道路吗?
  2. Java虚拟机(JVM)面试题大集合
  3. 混合代码块 Markdown Leedcde
  4. [Leedcode][JAVA][第22题括号生成][DFS][BFS][动态规划]
  5. python单元测试的应用_单元测试pythongui应用程序的推荐方法是什么?
  6. mysql修改字段默认值_MySQL增删改查操作
  7. eclipse 快捷键及插件
  8. python安装copy_python中copy和deepcopy 的区别
  9. 一建机电实务教材电子版_20年一建其实并不难,官方出版:复习题集(精修),速做速提90分...
  10. 设备I/O之OVERLAPPED