First of all, what is an interactive problem?
read it from here
The blog says all about it, still i am showing it roughly and practically.
In interactive problem we have to grab input data from the problem on our demand. we have to make queries and if our query stand valid then the problem (interactor) give us some input and based on these inputs of some limited number of query we have to answer a valid result of this problem.
Lets see a problem,
Problem statement:
Okay, Our hero Homer has an array which is a permutation, but hidden. We have to find any local minimum of the array in at most 100 queries. Wants to know more about local minimum click here
In each query we can get a value of any index we asks for. For finding local minimum we will run a binary search.
For every printf() function or cout << we have to flush
in C++, we can use cout.flush() or fflush(stdout)
Here is the official Editorial of this problem.
Code:
/// Bismillahir Rahmanir Rahim
/* Mohammad Morsalin
Dept of ICE, NSTU
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
int arr[maxn];
int n;
///solution
void take(int w){
if(1<=w && w<=n){
printf("? %d\n",w);
fflush(stdout);
scanf("%d",&arr[w]);
}
}
void solution(){
//cin>>n;
scanf("%d",&n);
int ans;
arr[0]=n+1;
arr[n+1]=n+1;
int left=1, right=n;
while(left<right){
int mid=(left+right)/2;
take(mid);
take(mid+1);
if(arr[mid+1]>arr[mid]){
right=mid;
}
else left=mid+1;
}
printf("! %d\n",left);
fflush(stdout);
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen ("input.txt","r",stdin);
freopen ("output.txt","w",stdout);
#endif
int t;
t=1;
//cin>>t;
while(t--){
solution();
}
return 0;
}
///Alhamdulillah
No comments:
Post a Comment
If you have any doubts, let me know through comments