Codeforces Round #523 (Div. 2) F. Katya and Segments Sets (交互题+思维)
https://codeforces.com/contest/1061/problem/F
题意
假设存在一颗完全k叉树(n<=1e5),允许你进行最多(n*60)次询问,然后输出这棵树的根,每次询问,a,b,c三个点,会返回b是否在a,c之间的路径上
思路
- 粗略看了下询问次数,可以知道,你最多可以询问60对不同的点,每对点遍历n个点,可以知道n个点在不在这两个点的中间
- 一开始的思路是,随机找两个点,遍历所有点,然后记录在他们中间的点,在里面再找两个点,继续上述操作,知道剩下一个点,但是假设某一步挑出的两个点的路径不经过根节点,hack
- 假设树根在第0层,根据完全k叉树的性质,前k-1层点数为\(2^k-1\),第k层点数为\(2^k\),所以大约就是1:1,那么每次问出一个点是否为叶子的概率是1/2,所以可以先找出两个叶子,然后再询问出两个叶子的路径,中间的点就是根节点
- 叶子结点怎么找:将叶子节点作为中间的点,随便找一个另外一个节点,那么叶子结点不在这个节点和其他节点的路径上,询问次数n
规律
- 完全k叉树,叶子:非叶子 约等于 1:1
- 生成随机数后加一个质数再取模,会使数据更加离散,
- 树的叶子十分重要
- 交互题一定要优化到最简询问次数,不然很容易被hack
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
char s[10];
int n,k,d,a,b,vi[2000],i,cnt;
vector<int>p,P;
int pw(int bs,int x){
int ans=1;
while(x){
if(x&1)ans*=bs;bs*=bs;x>>=1;
}
return ans;
}
void op(int a,int b,int x){
vector<int>tp;
memset(vi,0,sizeof(vi));
int cnt=0;
for(int i=1;i<=n;i++){
if(a==i||b==i)continue;
printf("? %d %d %d\n",a,i,b);
fflush(stdout);
scanf("%s",s);
if(s[0]=='Y'){vi[i]=1;cnt++;}
if(x==0&&cnt==2*d-3)break;
if(x&&cnt>d-2)break;
}
for(int i=1;i<=n;i++)if(vi[i])tp.pb(i);
if(x==0)P=tp;
else p=tp;
}
int main(){
scanf("%d %d",&n,&k);
for(d=1;;d++)if((pw(k,d)-1)/(k-1)==n)break;
for(;;){
a=(rand()+4399)%n+1;b=(rand()+2755)%n+1;
if(a==b){b++;b=b%n+1;}
op(a,b,0);
if(P.size()==2*d-3)break;
}
for(i=0;i<P.size();i++){
op(P[i],a,1);
if(p.size()==d-2){
printf("! %d\n",P[i]);
fflush(stdout);
return 0;
}
}
}
Codeforces Round #523 (Div. 2) F. Katya and Segments Sets (交互题+思维)的更多相关文章
- Codeforces Round #524 (Div. 2) F. Katya and Segments Sets(主席树)
https://codeforces.com/contest/1080/problem/F 题意 有k个区间,区间的种类有n种,有m个询问(n,m<=1e5,k<=3e5),每次询问a,b ...
- Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题
F. Restore a Number Vasya decided to pass a very large integer n to Kate. First, he wrote that num ...
- Codeforces Round #496 (Div. 3) E1. Median on Segments (Permutations Edition) (中位数,思维)
题意:给你一个数组,求有多少子数组的中位数等于\(m\).(若元素个数为偶数,取中间靠左的为中位数). 题解:由中位数的定义我们知道:若数组中\(<m\)的数有\(x\)个,\(>m\)的 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #523 (Div. 2)
Codeforces Round #523 (Div. 2) 题目一览表 来源 考察知识点 完成时间 A Coins cf 贪心(签到题) 2018.11.23 B Views Matter cf 思 ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
随机推荐
- Delphi:MSBuild编译dproj工程
Delphi之命令行编译工程,传统是用dcc32来编译的,它需要设置一大堆参数. 自Delphi 2007以后,支持MSBuild编译,它直接编译.dproj工程文件,所有编译需要的东西,都已在其中设 ...
- PHP百杂
PHP实时生成并下载超大数据量的EXCEL文件 PHP错误和异常 PHP异常处理机制 我所理解的php缓冲机制及嵌套级别 $nick = 'test'; //简化输出 echo $nick?:''
- java 基础之--反射详解
java 反射绝大部分都位于 java.lang.reflect package 中:常用的类就是: 1.class类:代表一个类 2.field类:代表类的成员变量 3.method:代表类的方法 ...
- webpack(一) 安装使用 之css使用注意
在webpackDemo 文件夹中新建 style.css,index.html style.css 中将背景色设为红色. body{ background-color: red; } he'llWo ...
- Non-negative Integers without Consecutive Ones
n位二进制,求不包含连续1的二进制(n位)数字个数. http://www.geeksforgeeks.org/count-number-binary-strings-without-consecut ...
- ABP框架使用Mysql数据库
参考文档:https://github.com/ABPFrameWorkGroup/AbpDocument2Chinese/blob/master/Markdown/Abp/9.4ABP%E5%9F% ...
- Next generation sequencing (NGS)二代测序数据预处理与分析
二代测序原理: 1.DNA待测文库构建. 超声波把DNA打断成小片段,一般200--500bp,两端加上不同的接头2.Flowcell.一个flowcell,8个channel,很多接头3.桥式PCR ...
- iOS 用其他应用程序打开文件功能
先摘抄一段我抄别人用的. <key>CFBundleDocumentTypes</key> <array> <dict> ...
- Numpy 学习 array np.where lexsort 切片 按行按列求平均mean
array 的创建可以通过list给 array print出来像一个表格,可以按行按列来观察. 原来是一个list相当于一行 np.where用于寻找一个condition下的坐标,返回的是一个2个 ...
- APICloud开发
2018-06-16 今天在看房角石APPIOS版本闪退的问题,后来定位到了 elements.find("video").attr("preload", &q ...