mercredi 27 janvier 2021

Idleness limit exceeded

I recently took part in a coding contest and I found idleness limit exceeded on test 1 in case of my submission while it was working perfectly fine on my offline ide. The link to the question is:

Alice and Bob are at yet another guessing game. Bob chooses a number k between 1 and n (1≤n≤10^9). Now, Alice makes a guess g:

if g<k, Bob responds with lesser and changes k to k−⌊k−g2⌋
if g>k, Bob responds with greater and changes k to k+⌊g−k2⌋
if g=k, Bob responds with guessed and Alice wins the game.

Alice is allowed to make not more than 16 guesses.

Input
A single integer n (1≤n≤109) such that the number k chosen by Bob is 1≤k≤n.

Output
Print your guess g (1≤g≤n) in a single line.

Interaction
To every g that you print, the judge may give one of the three responses- greater, lesser, guessed. As soon as you receive guessed, you have to terminate your program else you may receive any verdict. You are allowed to make not more than 16 guesses and must terminate your program irrespective of the response after the 16th guess.

and my submission is:


    #include<iostream>
    #include<cmath>
    using namespace std;
    
    int main()
    {
        long long int n;
        cin>>n;
        int g;
        long long int k=rand()%(n);
        int i=16;
        while(i--)
        {
            cin>>g;
            cout<<endl;
            if(g==k)
            {
                cout<<"guessed"<<endl;
                exit(0);
            }
            if(g<k)
            {
                cout<<"lesser"<<endl;
                k=k-floor((k-g)/2);
    
            }
            if(k<g)
            {
                cout<<"greater"<<endl;
                k=k+floor((g-k)/2);
    
            }
    
    
        }
    }

I searched on codeforces blog and found that in c we need to use fflush but I am using c++11 where it is automatically done when I use endl. Please help. thanks in advance!

Aucun commentaire:

Enregistrer un commentaire