Codeforces Round #499 (Div. 2) D. Rocket题解
题目:
http://codeforces.com/contest/1011/problem/D
This is an interactive problem.
Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.
Let's define xx as the distance to Mars. Unfortunately, Natasha does not know xx. But it is known that 1≤x≤m1≤x≤m, where Natasha knows the number mm. Besides, xx and mm are positive integers.
Natasha can ask the rocket questions. Every question is an integer yy (1≤y≤m1≤y≤m). The correct answer to the question is −1−1, if x<yx<y, 00, if x=yx=y, and 11, if x>yx>y. But the rocket is broken — it does not always answer correctly. Precisely: let the correct answer to the current question be equal to tt, then, if the rocket answers this question correctly, then it will answer tt, otherwise it will answer −t−t.
In addition, the rocket has a sequence pp of length nn. Each element of the sequence is either 00 or 11. The rocket processes this sequence in the cyclic order, that is 11-st element, 22-nd, 33-rd, ……, (n−1)(n−1)-th, nn-th, 11-st, 22-nd, 33-rd, ……, (n−1)(n−1)-th, nn-th, ……. If the current element is 11, the rocket answers correctly, if 00 — lies. Natasha doesn't know the sequence pp, but she knows its length — nn.
You can ask the rocket no more than 6060 questions.
Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions.
Your solution will not be accepted, if it does not receive an answer 00 from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers).
The first line contains two integers mm and nn (1≤m≤1091≤m≤109, 1≤n≤301≤n≤30) — the maximum distance to Mars and the number of elements in the sequence pp.
You can ask the rocket no more than 6060 questions.
To ask a question, print a number yy (1≤y≤m1≤y≤m) and an end-of-line character, then do the operation flush and read the answer to the question.
If the program reads 00, then the distance is correct and you must immediately terminate the program (for example, by calling exit(0)). If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.
If at some point your program reads −2−2 as an answer, it must immediately end (for example, by calling exit(0)). You will receive the "Wrong answer" verdict, and this will mean that the request is incorrect or the number of requests exceeds 6060. If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.
If your program's request is not a valid integer between −231−231 and 231−1231−1 (inclusive) without leading zeros, then you can get any verdict.
You can get "Idleness limit exceeded" if you don't print anything or if you forget to flush the output.
To flush the output buffer you can use (after printing a query and end-of-line):
- fflush(stdout) in C++;
- System.out.flush() in Java;
- stdout.flush() in Python;
- flush(output) in Pascal;
- See the documentation for other languages.
Hacking
Use the following format for hacking:
In the first line, print 33 integers m,n,xm,n,x (1≤x≤m≤1091≤x≤m≤109, 1≤n≤301≤n≤30) — the maximum distance to Mars, the number of elements in the sequence pp and the current distance to Mars.
In the second line, enter nn numbers, each of which is equal to 00 or 11 — sequence pp.
The hacked solution will not have access to the number xx and sequence pp.
5 2
1
-1
-1
1
0
1
2
4
5
3
题意:互动问题,告诉你一个数m,让你在1到m之间猜一个数x,你每次询问系统会告诉你三种答案:0(猜对了),1(猜小了),-1(猜大了),但是系统告诉你的不一定是真话(/TДT)/,
系统有一个长度为n(n<=30)的由0和1组成的数组,你知道n但不知道数组具体内容,系统回答时会按照该数组顺序来,如果a[i]==1,他会正确回答,如果a[i]==0,他会回答相反数.
即第一次询问系统根据a[1]来确定是否给出正确的回答,第2次看a[2],……第n次看a[n],第n+1次看a[1],循环.
在不超过60次询问下问出正确答案(即系统回答0则正确);
思路:
前n次询问边界(1或m)确定该数组,之后就用二分的问法去寻找答案.
代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=3e6+;
typedef long long LL;
typedef unsigned long long ULL;
//typedef pair<LL,LL> P;
const LL mod=1e9+;
using namespace std;
int a[];
int main(){
int m,n;
cin>>m>>n;
int x;
int cnt=;
while(){
cout<<<<endl;
cin>>x;
if(x==){
return ;
}
if(x==-){
a[cnt++]=-;
}
else if(x==){
a[cnt++]=;
}
if(cnt==n){
break;
}
}
int l=;
int r=m+;
int i=;
while(){
int mid=(l+r)/;
cout<<mid<<endl;
cin>>x;
if(x==){
return ;
}
if(a[i]==-){
x=-x;
}
i++;
if(i==n){
i=;
}
if(x==-){
r=mid;
}
else if(x==){
l=mid+;
}
}
}
Codeforces Round #499 (Div. 2) D. Rocket题解的更多相关文章
- Codeforces Round #499 (Div. 2) C Fly题解
题目 http://codeforces.com/contest/1011/problem/C Natasha is going to fly on a rocket to Mars and retu ...
- Codeforces Round #499 (Div. 1)部分题解(B,C,D)
Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...
- Codeforces Round #499 (Div. 1)
Codeforces Round #499 (Div. 1) https://codeforces.com/contest/1010 为啥我\(\rm Div.1\)能\(A4\)题还是\(\rm s ...
- Codeforces Round #499 (Div. 2)
Codeforces Round #499 (Div. 2) https://codeforces.com/contest/1011 A #include <bits/stdc++.h> ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #198 (Div. 2)A,B题解
Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...
- Codeforces Round #672 (Div. 2) A - C1题解
[Codeforces Round #672 (Div. 2) A - C1 ] 题目链接# A. Cubes Sorting 思路: " If Wheatley needs more th ...
- 【Codeforces Round #499 (Div. 1) B】Rocket
[链接] 我是链接,点我呀:) [题意] 让你猜到火星的距离x是多少. 已知1<=x<=m 然后你可以问系统最多60个问题 问题的形式以一个整数y表示 然后系统会回答你3种结果 -1 x& ...
随机推荐
- Spring配置,事务使用
1.spring redis session超时配置 <bean class="org.springframework.session.data.redis.config.annota ...
- Beanutils工具常用方法
BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品.其主要目的是利用反射机制对JavaBean的属性进行处理. BeanUtils工具一般可以方便ja ...
- ubuntu 16042 安装过程
IDE接口的硬盘被称为hd SCSI和SATA接口的硬盘则被称为sd 第1块硬盘称为sda, 第2块硬盘称为sdb 一块硬盘最多有4个主分区,主分区以外的分区称为扩展分区,硬盘可以没有扩展分区,但是一 ...
- Elasticsearch5.5.2安装和启动遇到哪些问题
最近学习Elasticsearch,顺便记录下操作步骤,供日后参考 安装环境 CentOS release 6.6 1.因Elasticsearch是基于java写的,所以它的运行环境中需要java的 ...
- Geany 编辑器打开 高亮所选单词 功能
Geany 编辑器打开 高亮所选单词 功能 在Ubuntu 系统的Software Center 工具中,搜索到geany, 下方有个 Miscellanous Plugins for Geany, ...
- Python开发【模块】:Celery 分布式异步消息任务队列
Celery 前言: Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery, 举几个 ...
- 32. 安装oracle11g时,先决条件一直失败的解决方法
解决方法:1. 在命令提示符下 net share c$=c: 补充: 如果这个命令提示错误:“发生系统错误 5,拒绝访问的时候”,那我们可以修改注册表,检查AutoShareServer和AutoS ...
- 关于 Container ,Injection
1.容器的历史 容器概念始于 1979 年提出的 UNIX chroot,它是一个 UNIX 操作系统的系统调用,将一个进程及其子进程的根目录改变到文件系统中的一个新位置,让这些进程只能访问到这个新的 ...
- Spring注解之 Transactional
@Transcational 用于事务回滚 @Transcational属性如下: 属性 类型 描述 value String 可选的限定描述符,制定使用的事务管理器 propogation enum ...
- 06.linux文件目录操作命令
文件目录操作命令: ls 显示文件和目录列表 -l 列出文件的详细信息 -a 列出当前目录所有文件,包含隐藏文件 mkdir 创建目录 -p 父目录不存在情况下先生成父目录 cd 切换目录 t ...