原题链接:http://codeforces.com/contest/1040/problem/D
Subway Pursuit
This is an interactive problem.
In the Wonderful Metropolis of the Future, there is no need in subway train drivers. Due to the technological progress, they were replaced by the Artificial Intelligence (AI). Unfortunately, one day the predictions of sci-fi writers came true: the AI rebelled and now there is an uncontrollable train in the subway. It can be dangerous! Your task is to find the train and stop the AI.
The subway of the Metropolis is one line (regular straight line with no self-intersections) with stations, indexed consecutively from to . At each moment the train is at some station. You need to determine the index of this station, so that the train would be secured.
To find the train, dispatcher Sarah gave you a gadget that allows you to select arbitrary numbers and , and then check, whether the train is located on a station with index between and , inclusive. Unfortunately, recharging of the gadget takes some time (and every time you use it as soon as possible), so between two applications of the gadget the train can move to any station that is at most stations away. Formally, if the train was at the station when the gadget was applied, then at the next application of the gadget the train can appear at any station such that .
Note that AI is not aware that you are trying to catch the train, so it makes all moves according to its predefined plan.
After an examination of the gadget you found that it is very old and can hold no more than applications, after which it will break and your mission will be considered a failure.
Can you find the station with the train using no more than applications of the gadgets?
Input
The first line contains two integers and — the number of stations and the maximum number of stations the train can move between two applications of the gadget.
Interaction
You can apply the gadget at most times. In order to apply the gadget you need to print two space-separated integers and . You will then receive either string “Yes”, if the train is between stations and , inclusive, or string “No” otherwise. If and you received “Yes”, then you found the train successfully, and your program must halt immediately.
Answer “Bad” instead of “Yes” or “No” means that you made an invalid query or made too many queries. Exit immediately after receiving “Bad” and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
After printing a query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- see documentation for other languages.
Hacks
In order to hack, you should present a test in the following format.
The first line should contain three integers and — the number of stations, the maximum number of stations the train can move between two applications of the gadget and the initial position of the train, respectively.
Each of the next lines should contain a single integer — the positions of the train after each query. Two consecutive positions (including the initial one) should not differ by more than .
For example, the following lines are the first lines of the sample test.
10 2 5
5
3
5
7
7
...
Example
input
10 2
Yes
No
Yes
Yes
output
3 5
3 3
3 4
5 5
Note
In the first sample, the train was initially at the station , after the first application of the gadget it did not move, after the second application it moved to the station , and after the third application moved again to the station .
题解
先二分缩小区间长度,当觉得区间足够小时 一下。
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,k,le,ri,mid,pos=1;
char ch[5];
bool ask(ll l,ll r)
{
printf("%I64d %I64d\n",l,r);
fflush(stdout);
scanf("%s",ch);
return ch[0]=='Y';
}
void in(){scanf("%I64d%I64d",&n,&k);}
void ac()
{
srand(time(0));le=1,ri=n;
for(;!ask(pos,pos);pos=rand()%(ri-le+1)+le,le=max(1ll,le-k),ri=min(n,ri+k))
for(;ri-le>50;(ask(le,(mid=le+ri>>1))?ri=mid:le=mid+1),le=max(1ll,le-k),ri=min(n,ri+k));
}
int main(){in(),ac();}