Блог пользователя Wonsei

Автор Wonsei, история, 4 года назад, По-английски
#include <iostream>
#include <cstring>

using namespace std;

int main() {
	int ll = 1, rr = 1000000;
	int ans;
	while (ll <= rr) {
		int mid = (ll + rr) / 2;
		cout << mid;
		fflush(stdout);
		string s;
		cin >> s;
		if (s == ">=") {
			ans = mid;
			ll = mid + 1;
		}
		else {
			rr = mid - 1;
		}
	}
	cout << "! " << ans;
	fflush(stdout);
	return 0;
}

This is an easy interactive question that guesses the number 1 to 1000000 by binary search. However, I am new to the concept of interactive problems, and I am getting an idleness TLE on this problem. Can someone tell me what is causing the Idleness on my code? Thanks in advance.

Problem statement : https://codeforces.com/gym/101021/problem/1

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Wonsei (previous revision, new revision, compare).

»
4 года назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

Replace fflush(stdout); with cout<<endl; instead.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Thanks, you resolved the issue :) But do you know why fflush(stdout) and cout.flush() doesn't work?

    • »
      »
      »
      4 года назад, # ^ |
      Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

      I think the main issue is that you forgot to output a line break. The grader can't tell that it's at the end of the number you printed until it sees some whitespace.