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

题干:

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;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441005.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C语言第六次作业指针,c语言第六次作业解析

《c语言第六次作业解析》由会员分享&#xff0c;可在线阅读&#xff0c;更多相关《c语言第六次作业解析(36页珍藏版)》请在人人文库网上搜索。1、c 语言第六次作业解析第六次作业&#xff1a;指针(以下题目如无特殊声明&#xff0c; 请使用指针技术实现 , 尽量不要使用数组作为…

【HDU - 1069】Monkey and Banana (最长下降子序列 + 贪心,最长上升子序列类问题)

题干&#xff1a; A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able …

c 语言定义2维字符串数组赋值,二维数组赋值字符串 c 语言 二维字符串数组赋值问题...

C语言中二维字符数组应该怎样赋值&#xff1f;c语言二维数组如何定义字符串&#xff1f;&#xff1f;&#xff1f;&#xff1f;急。。。二维字符数组的定义格式为&#xff1a;char 数组名[第一维大小][第二维大小]; 例如&#xff1a;char c[3][10]; //定义了一个3行10列的二维字…

【牛客 - 297D】little w and Exchange(上下界贪心)

题干&#xff1a; 旅行到K国的小w发现K国有着很多物美价廉的商品&#xff0c;他想要买一些商品。 结果一掏钱包&#xff0c;包里只剩下n张K国的纸币了&#xff0c;说起来也奇怪&#xff0c;K国纸币并不像其他国家一样都是1元&#xff0c;5元&#xff0c;10元…而是各种奇怪的…

c语言程序设计 doc,《C语言程序设计》.doc

《C语言程序设计》.doc《C语言程序设计》实验 编实验一 C程序的运行环境和运行C程序的方法2实验二 数据类型、运算符和表达式9实验三 最简单的C程序设计14实验四 逻辑结构程序设计20实验五 循环结构程序设计26实验六 数组31实验七 函数39实验八 编译预处理命令45实验九 指针50…

【牛客 - 315F】美丽的项链(线性dp,递推,我为人人)

题干&#xff1a; 妞妞参加了Nowcoder Girl女生编程挑战赛, 但是很遗憾, 她没能得到她最喜欢的黑天鹅水晶项链。 于是妞妞决定自己来制作一条美丽的项链。一条美丽的项链需要满足以下条件: 1、需要使用n种特定的水晶宝珠 2、第i种水晶宝珠的数量不能少于li颗, 也不能多于…

撞球编程c语言,急!C语言编程题——撞球

满意答案#include #include #include int main(){double length,wide,x0,y0,x1,y1;int i;char towards[1500];while(1){memset(towards,0,sizeof(towards));if(scanf("%lf %lf",&wide,&length)EOF)break;scanf("%lf %lf",&x0,&y0);scanf(&…

【牛客 - 315C】排列(思维,贪心,同优则立证明法)

题干&#xff1a; 妞妞得到一个(1~n)的排列p1, p2, p3,...,pn, 听村里的老人牛牛说如果让这个排列变为: 对于所有的1 < i < n, 都满足pi ≠ i, 就可以获得Google Girl Hackathon的入场券。 妞妞仅允许的操作是: 交换排列中两个相邻的元素, 并且妞妞允许做这个操作任意…

镇江 linux技术支持,东云镇江服务器

弹性云服务器 ECS弹性云服务器(Elastic Cloud Server)是一种可随时自助获取、可弹性伸缩的云服务器&#xff0c;帮助用户打造可靠、安全、灵活、高效的应用环境&#xff0c;确保服务持久稳定运行&#xff0c;提升运维效率三年低至5折&#xff0c;多种配置可选了解详情什么是弹性…

*【牛客 - 315D】打车(贪心,同优则立证明法)

题干&#xff1a; 妞妞参加完Google Girl Hackathon之后,打车回到了牛家庄。 妞妞需要支付给出租车司机车费s元。妞妞身上一共有n个硬币&#xff0c;第i个硬币价值为p[i]元。 妞妞想选择尽量多的硬币&#xff0c;使其总价值足以支付s元车费(即大于等于s)。 但是如果从妞妞…

c语言中只能逐个引用6,C语言前面六个练习.doc

C语言前面六个练习第一章 C语言基础知识4&#xff0e;一个函数的函数体可以没有变量定义和执行部分&#xff0c;函数可以是空函数2&#xff0e;一个函数由两部分组成&#xff0c;它们是 函数体 和 函数的说明部分。3&#xff0e;函数体的范围是 大括号以内 。(0级)4&#xff0e…

【牛客 - 289K】这是一个沙雕题III(贪心,思维枚举,技巧trick,计算上下界)

题干&#xff1a; 因为现在的新生太强了&#xff0c;都学会了“dp”&#xff0c;所以就有了这样一个“dp”题&#xff0c;双11时Gugugu有(x&#xff0c;x1,x2....y-1,y)元的抵用券无数张&#xff0c;但是Gugugu有强迫症所以他希望他使用抵扣券正好能够抵扣k元&#xff0c;这…

c 跟r语言运行速度,1. R语言运行效率分析(5)

方法5&#xff1a; 采用 which 语句1: 自定义函数# digital was translated into englishnameMonth_name_whichMonth_nameMonth_name[(which(Month_name1))]Month_name[(which(Month_name2))]Month_name[(which(Month_name3))]Month_name[(which(Month_name4))]Month_name[(whi…

【牛客 - 297C】little w and Segment Coverage(差分数组,区间差分,思维,卡线段树)☆

题干&#xff1a; 小w有m条线段&#xff0c;编号为1到m。 用这些线段覆盖数轴上的n个点&#xff0c;编号为1到n。 第i条线段覆盖数轴上的区间是L[i]&#xff0c;R[i]。 覆盖的区间可能会有重叠&#xff0c;而且不保证m条线段一定能覆盖所有n个点。 现在小w不小心丢失了一…

链表c语言stl,C++STL之List容器

1.再谈链表List链表的概念再度出现了&#xff0c;作为线性表的一员&#xff0c;C的STL提供了快速进行构建的方法&#xff0c;为此&#xff0c;在前文的基础上通过STL进行直接使用&#xff0c;这对于程序设计中快速构建原型是相当有必要的&#xff0c;这里的STL链表是单链表的形…

*【牛客 - 318B】签到题(单调栈,水题)

题干&#xff1a; 众所周知&#xff0c;IG是英雄联盟S8世界总决赛冠军&#xff0c;夺冠之夜&#xff0c;数亿人为之欢呼&#xff01; 赛后某百分百胜率退役ADC选手的某表情包意外走红&#xff0c;某苟会长看到此表情包也想模仿。 于是有n个友爱的萌新决定每人都送会长一根长…

c 语言车牌识别系统课题设计,车牌识别系统的设计--课程设计报告.doc

车牌识别系统的设计--课程设计报告目录一、摘要:3二、设计目的和意义:32.1、设计目的&#xff1a;32.2、设计意义&#xff1a;3三、设计原理:3四、详细设计步骤:34.1、提出总体设计方案:44.2、各模块的实现:5五、设计结果及分析20六、总结:22七、体会23八、参考文献:23一、摘要…

*【HDU - 2586】How far away ? (LCA模板题,倍增)

题干&#xff1a; There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int th…

android volley 上传图片 和参数,Android使用Volley上传文件

一个项目中用到的使用Volley上传头像文件的例子/*** Created by wangshihui on 2015/11/30.* 上传文件* url&#xff1a;.....method&#xff1a;post参数&#xff1a;file接口给的参数&#xff1a;file 就是表单的key&#xff0c;传给mFilePartName;这是个测试类&#xff0c;…

【HDU - 4056】Draw a Mess (并查集 or 线段树)

题干&#xff1a; Its graduated season, every students should leave something on the wall, so....they draw a lot of geometry shape with different color. When teacher come to see what happened, without getting angry, he was surprised by the talented achiev…