QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#552322 | #8939. Permutation | ucup-team3555 | TL | 0ms | 0kb | C++14 | 1022b | 2024-09-07 21:59:43 | 2024-09-07 21:59:43 |
Judging History
answer
# include <bits/stdc++.h>
const int N=100010,INF=0x3f3f3f3f;
inline int read(void){
int res,f=1;
char c;
while((c=getchar())<'0'||c>'9')
if(c=='-')f=-1;
res=c-48;
while((c=getchar())>='0'&&c<='9')
res=res*10+c-48;
return res*f;
}
int n;
inline int query(int l,int r){
printf("? %d %d\n",l,r);
fflush(stdout);
return read();
}
inline int solve(void){
n=read();
if(n==1) return 1;
int l=1,r=n,pos=query(1,n);
while(r-l+1>3){
int len=(int)(r-l+1)*0.618;
int mid=l+len-1;
if(pos<=mid){
if(query(l,mid)==pos) r=mid;
else pos=query(mid+1,r),l=mid+1;
}else{
mid=r-len;
if(query(mid+1,r)==pos) l=mid+1;
else pos=query(l,mid),r=mid;
}
}
if(l+1==r) return (pos==l)?r:l;
if(pos<=l+1){
if(query(l,l+1)==pos) return (pos==l)?l+1:l;
else return r;
}else{
if(query(r-1,r)==pos) return (pos==r)?r-1:r;
else return l;
}
return 0;
}
int main(void){
int T=read();
while(T--) printf("%d\n",solve());
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 5 3 2 5
output:
? 1 5 ? 1 3 ? 4 5