CF835E The penguin's game(异或性质+二分查找)
来源:互联网 发布:你睡过几个男人 知乎 编辑:程序博客网 时间:2024/05/18 02:34
题目大意:有n个数,其中有且只有两个数为y,其他的都为x,让你找到这两个为y的位置pos1,pos2。你可以问20个问题,每个问题为你给出一个集合,系统回馈给你他们的异或和。首先,每个集合中要不只有1个y,要不有0或2个y。我们通过问一个问题就可以知道这个集合中y的个数是奇数还是偶数。分以下四种情况:
1、集合大小为偶数,y有偶数个,则x有偶数个,那么返回值为0
2、集合大小为偶数,y有奇数个,则x有奇数个,那么返回值为x^y
3、集合大小为奇数,y有偶数个,则x有奇数个,那么返回值为x
4、集合大小为奇数,y有奇数个,则x有偶数个,那么返回值为y
因为x!=y,x!=0,y!=0,所以这四种返回值一定是不同的数。我们根据返回值就可以判断y的个数。
我们把每个数当做二进制,则最多有
#include <cstdio>#include <cstring>#include <iostream>#include <vector>#include <algorithm>using namespace std;int n,x,y,differ=0,differbit=-1;vector<int> a;vector<int> b;int ask(){ if(a.empty()) return 0; printf("? %d",a.size()); for(int i=0;i<a.size();++i) printf(" %d",a[i]); puts("");fflush(stdout); int res; scanf("%d",&res); return res;}int solve(){//b这个子集中只有一个特殊值,用二分找到它。 int l=0,r=b.size()-1; while(l<r){ int mid=(l+r)>>1;a.clear(); for(int i=l;i<=mid;++i) a.push_back(b[i]); int res=ask(); if(res==y||res==(x^y)) r=mid; else l=mid+1; }return b[l];}int main(){ scanf("%d%d%d",&n,&x,&y); for(int i=0;(1<<i)<=n;++i){ a.clear(); for(int j=1;j<=n;++j) if(j&(1<<i)) a.push_back(j); int res=ask(); if(res==y||res==(x^y)) differ|=(1<<i),differbit=i; } a.clear(); for(int i=1;i<=n;++i){ if(i&(1<<differbit)) a.push_back(i); else b.push_back(i); } if(a.size()<b.size()) swap(a,b); int pos1=solve(); int pos2=pos1^differ; if(pos1>pos2) swap(pos1,pos2); printf("! %d %d\n",pos1,pos2); return 0;}
阅读全文
0 0
- CF835E The penguin's game(异或性质+二分查找)
- Codeforces 835E:The penguin's game
- Codeforces Round #427 (Div. 2) E. The penguin's game(交互+思维+二分)
- Codeforces Round #427 (Div. 2) E. The penguin's game
- Codeforces Round #427 (Div. 2) E. The penguin's game
- [Codeforces 835E. The penguin's game]构造+二进制
- COdeforces 835E The penguin's game (二进制)
- Codeforces 835E E. The penguin's game 交互题 多猪试毒
- Codeforces 835E E. The penguin's game 交互题 多猪试毒 (写的挺好的)
- CodeForces-835E The penguin's game(思维+二进制乱搞+交互题)
- HDU4768 Flyer 二分|异或的性质|暴力
- (数学或二分)Codeforces Round #426 C. The Meaningless Game
- hdoj 5433 MZL's xor ( 异或性质)
- pku 1634 Who's the boss? stl排序+二分查找
- hdu 4004The Frog's Games 二分查找!!!!!!!
- hdu 4004 The Frog's Games 二分查找
- HDU 4004(The Frog's Games) 二分查找求答案
- hdoj The Frog's Games (二分查找)
- Tempter of the Bone
- LeetCode
- 防止内存泄漏--android-weak-handler
- SEO优化怎么设置描述语呢?
- HDU6063---RXD and math(2017多校联赛题目(???))
- CF835E The penguin's game(异或性质+二分查找)
- 什么是RxSwift?
- HDU-Binary Tree Traversals
- 全局变量
- Git基本操作_Anroid群英传之神兵利器笔记(一)
- JS循环辅助语句
- AngularJS入门-(11)表单
- Mybatis一对多和多对一的映射
- java代码实现外网IP的获取