AtCoder Regular Contest 078 E - Awkward Response
E - Awkward Response
Time limit : 2sec / Memory limit : 256MB
Score : 800 points
Problem Statement
This is an interactive task.
Snuke has a favorite positive integer, N. You can ask him the following type of question at most 64 times: "Is n your favorite integer?" Identify N.
Snuke is twisted, and when asked "Is n your favorite integer?", he answers "Yes" if one of the two conditions below is satisfied, and answers "No" otherwise:
- Both n≤N and str(n)≤str(N) hold.
- Both n>N and str(n)>str(N) hold.
Here, str(x) is the decimal representation of x (without leading zeros) as a string. For example, str(123)= 123
and str(2000) = 2000
. Strings are compared lexicographically. For example, 11111
< 123
and 123456789
< 9
.
Constraints
- 1≤N≤109
Input and Output
Write your question to Standard Output in the following format:
? n
Here, n must be an integer between 1 and 1018 (inclusive).
Then, the response to the question shall be given from Standard Input in the following format:
ans
Here, ans is either Y
or N
. Y
represents "Yes"; N
represents "No".
Finally, write your answer in the following format:
! n
Here, n=N must hold.
Judging
- After each output, you must flush Standard Output. Otherwise you may get
TLE
. - After you print the answer, the program must be terminated immediately. Otherwise, the behavior of the judge is undefined.
- When your output is invalid or incorrect, the behavior of the judge is undefined (it does not necessarily give
WA
).
Sample
Below is a sample communication for the case N=123:
Input | Output |
---|---|
? 1 |
|
Y |
|
? 32 |
|
N |
|
? 1010 |
|
N |
|
? 999 |
|
Y |
|
! 123 |
- Since 1≤123 and str(1)≤str(123), the first response is "Yes".
- Since 32≤123 but str(32)>str(123), the second response is "No".
- Since 1010>123 but str(1010)≤str(123), the third response is "No".
- Since 999≥123 and str(999)>str(123), the fourth response is "Yes".
- The program successfully identifies N=123 in four questions, and thus passes the case.
首先确定位数len,再确定前(len-1)位,再确定最后一位。
#include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<iostream> #include<queue> #include<map> #include<cmath> #include<set> #include<stack> #define ll long long #define pb push_back #define max(x,y) ((x)>(y)?(x):(y)) #define min(x,y) ((x)>(y)?(y):(x)) #define cls(name,x) memset(name,x,sizeof(name)) #define pos first #define index second #define mp make_pair using namespace std; const int inf=1e9+10; const ll llinf=1e16+10; const int maxn=2e5+10; const int maxm=1e2+10; const int mod=1e9+7; int n; char op[10]; int init() { int k=10; for(int i=1;i<=9;i++) { printf("? %lld\n",k); fflush(stdout); scanf("%s",op); if(op[0]=='N') return i; k=k*10; } k=9; for(int i=1;i<=9;i++) { printf("? %lld\n",k); fflush(stdout); scanf("%s",op); if(op[0]=='Y') return i; k=k*10+9; } return 10; } int main() { //freopen("in.txt","r",stdin); int len=init(); if(len==10) { printf("! %d\n",(int)1e9); return 0; } int ans=0; for(int i=1;i<len;i++) { int l=0,r=9,t=0; while(l<=r) { int mid=(l+r)/2; printf("? %d\n",ans*10+mid); fflush(stdout); scanf("%s",op); if(op[0]=='Y') { t=max(t,mid); l=mid+1; } else r=mid-1; } ans=ans*10+t; } for(int i=(len==1?1:0);i<=9;i++) { printf("? %lld\n",((ll)ans*10+i)*10); fflush(stdout); scanf("%s",op); if(op[0]=='Y') { ans=ans*10+i; break; } } printf("! %d\n",ans); return 0; }