Skip to content

Instantly share code, notes, and snippets.

@TrungNguyen1909
Last active December 8, 2018 03:39
Show Gist options
  • Save TrungNguyen1909/6990c06892825e863987646f24b054f8 to your computer and use it in GitHub Desktop.
Save TrungNguyen1909/6990c06892825e863987646f24b054f8 to your computer and use it in GitHub Desktop.
Communication/Interactive checker for CMS
#include <bits/stdc++.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
using namespace std;
void handler(int signum){
cout<<0;
cerr<<"Idleness limit Exceeded!";
exit(0);
}
int main(int argc, char* argv[]){
signal(SIGALRM,handler);
alarm(1);
auto fi = fdopen(open(argv[1],O_RDWR),"r");
auto fo = fdopen(open(argv[2],O_RDWR),"w");
setvbuf(fi, 0LL, 2, 0LL);
setvbuf(fo, 0LL, 2, 0LL);
int h,n;
cin>>h>>n;
fprintf(fo,"%d\n",h);
int cnt=log2(h)+2;
while(cnt--){
int q;
fscanf(fi,"%d",&q);
if(q<=0){
cout<<0;
cerr<<"Invalid query!";
exit(0);
}
if(q>n) {
fprintf(fo,"H\n");
}
else if(q<n){
fprintf(fo,"L\n");
}
else{
cout<<1;
cerr<<"okok! Correct number.";
exit(0);
}
}
cout<<0;
cerr<<"You guessed too much!";
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment