alexjercan/bugnet huggingface dataset:
Problem p00186 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
int q1, b, c1, c2, q2;
int main() {
  while (scanf("%d", &q1)) {
    if (q1 == 0)
      break;
    scanf("%d%d%d%d", &b, &c1, &c2, &q2);
    int jidori = b / c1;
    jidori = (jidori < q2 ? jidori : q2);
    b -= jidori * c1;
    int normal = b / c2;
    b -= normal * c2;
    while (jidori + normal < q1) {
      if (jidori <= 0)
        break;
      jidori--;
      b += c1;
      int temp = b / c2;
      normal += temp;
      b -= c2 * temp;
    }
    if (jidori <= 0)
      puts("NA");
    else
      printf("%d %d\\n", jidori, normal);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
int q1, b, c1, c2, q2;
int main() {
  while (scanf("%d", &q1)) {
    if (q1 == 0)
      break;
    scanf("%d%d%d%d", &b, &c1, &c2, &q2);
    int jidori = b / c1;
    jidori = (jidori < q2 ? jidori : q2);
    b -= jidori * c1;
    int normal = b / c2;
    b -= normal * c2;
    while (jidori + normal < q1) {
      jidori--;
      b += c1;
      int temp = b / c2;
      normal += temp;
      b -= c2 * temp;
    }
    if (jidori <= 0)
      puts("NA");
    else
      printf("%d %d\\n", jidori, normal);
  }
}
Problem p00186 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e10, EPS = 1e-9;

int main() {
  int q1, b, c1, c2, q2;
  while (cin >> q1, q1) {
    cin >> b >> c1 >> c2 >> q2;
    for (int aizu = q2; aizu > 0; aizu--) {
      if (double(aizu) * c1 > b)
        continue;
      int hutuu = (b - aizu * c1) / c2;
      if (aizu + hutuu < q1)
        continue;

      cout << aizu << " " << hutuu << endl;
      goto END;
    }
    cout << "NA" << endl;

  END:;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  long long q1, b, c1, c2, q2, d, n;
  while (cin >> q1, q1) {
    cin >> b >> c1 >> c2 >> q2;
    if (c2 * (q1 - 1) + c1 > b)
      cout << "NA" << endl;
    else {
      while (c1 == c2)
        ;
      d = min(q2, min((b - q1 * c2) / (c1 - c2), b / c1)),
      n = (b - c1 * d) / c2;
      cout << d << " " << n << endl;
    }
  }
  return 0;
}
Problem p00186 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

typedef long long int lli;

int main() {
  lli m, n, p, q, s, t, u, v;

  while (cin >> n && n) {
    cin >> s >> t >> u >> v;

    while (v > 0) {
      if ((s - t * v) >= 0 && (s - t * v) / u + v >= n) {
        break;
      }
      v--;
    }

    if (v == 0) {
      cout << "NA" << endl;
    } else {
      cout << v << " " << ((s - t * v) / u) << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int q1, b, c1, c2, q2, s, t, cost;

  while (1) {
    cin >> q1;
    if (q1 == 0) {
      break;
    }
    cin >> b >> c1 >> c2 >> q2;

    s = b / c1;
    if (s > q2) {
      s = q2;
    }
    t = q1 - s;
    if (t < 0) {
      t = 0;
    }
    cost = c1 * s + c2 * t;
    if (cost > b) {
      while (s > 0 && cost > b) {
        cost -= c1;
        s--;
      }
    }
    if (s <= 0) {
      cout << "NA" << endl;
      continue;
    }
    while (cost + c2 <= b) {
      t++;
      cost += c2;
    }
    if (s + t < q1) {
      while (s + t < q1) {
        cost -= c1;
        s--;
        while (cost + c2 <= b) {
          cost += c2;
          t++;
        }
      }
    }
    cout << s << " " << t << endl;
  }

  return 0;
}
Problem p00186 (C++) — buggy submission, verdict: Runtime Error

// 2014/06/05 Tazoe

#include <iostream>
using namespace std;

int main() {
  while (true) {
    int q1;
    cin >> q1;

    if (q1 == 0)
      break;

    int b, c1, c2, q2;
    cin >> b >> c1 >> c2 >> q2;

    if ((q1 - 1) * c2 > b - c1) {
      cout << "NA" << endl;
      continue;
    }

    int Q1, Q2;

    Q1 = b / c1;
    if (Q1 > q2) {
      Q1 = q2;
    }

    Q2 = (b - c1 * Q1) / c2;

    while (Q1 + Q2 < q1) {
      Q1--;
      Q2 = (b - c1 * Q1) / c2;
    }

    cout << Q1 << ' ' << Q2 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// 2011/08/22 Tazoe

#include <iostream>
using namespace std;

int main() {
  while (true) {
    int q1;
    cin >> q1;
    if (q1 == 0)
      break;

    int b, c1, c2, q2;
    cin >> b >> c1 >> c2 >> q2;

    int A = (b - c2 * q1) / (c1 - c2);
    if (A <= 0) {
      cout << "NA" << endl;
      continue;
    }
    if (A > q2)
      A = q2;

    int B = (b - c1 * A) / c2;
    if (B < 0) {
      A = b / c1;
      B = (b - c1 * A) / c2;
    }

    cout << A << ' ' << B << endl;
  }

  return 0;
}
Problem p00186 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;

int main() {

  while (true) {

    long long int b, c1, c2, q1, q2;

    cin >> q1;

    if (q1 == 0) {
      break;
    }

    cin >> b >> c1 >> c2 >> q2;

    long long int ans1, ans2;

    if (c1 <= c2) {
      ans1 = min(b / c1, q2);
      ans2 = (b - c1 * ans1) / c2;
    } else if (q1 * c1 > b) {
      ans1 = min((b - q1 * c2) / (c1 - c2), q2);
      ans2 = (b - c1 * ans1) / c2;
    } else {
      ans1 = min(b / c1, q2);
      ans2 = (b - c1 * ans1) / c2;
    }

    if (ans1 < 1 || ans2 < 0 || ans1 + ans2 < q1) {
      cout << "NA" << endl;
    } else {
      cout << ans1 << " " << ans2 << endl;
    }

    // cout << ans1 << " " << ans2 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;

int main() {

  while (true) {

    int b, c1, c2, q1, q2;

    cin >> q1;

    if (q1 == 0) {
      break;
    }

    cin >> b >> c1 >> c2 >> q2;

    int ans1, ans2;

    if (c1 <= c2) {
      while (true) {
      }
      ans1 = min(b / c1, q2);
      ans2 = (b - c1 * ans1) / c2;
    } else if (q1 * c1 > b) {
      ans1 = min((b - q1 * c2) / (c1 - c2), q2);
      ans2 = (b - c1 * ans1) / c2;
    } else {
      ans1 = min(b / c1, q2);
      ans2 = (b - c1 * ans1) / c2;
    }

    if (ans1 < 1 || ans2 < 0) {
      cout << "NA" << endl;
    } else {
      cout << ans1 << " " << ans2 << endl;
    }

    // cout << ans1 << " " << ans2 << endl;
  }

  return 0;
}
Problem p00186 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>

using namespace std;

void failed() { cout << "NA" << endl; }

int main() {

  int Q1, B, C1, C2, Q2;

  while (cin >> Q1 >> B >> C1 >> C2 >> Q2) {
    if (Q1 == 0)
      break;

    if (B / C1 == 0) {
      failed();
      continue;
    }

    int AizuCh = min(B / C1, Q2);
    int NCh = 0;
    for (;;) {

      if (AizuCh <= 0) {
        failed();
        break;
      }

      NCh = (B - AizuCh * C1) / C2;

      /*
      if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
        NCh = (B - AizuCh*C1)/C2;
      }
      */
      if (AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl;
        break;
      }
      /*
      else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
        NCh ++, AizuCh --;
      }
      */
      else
        AizuCh--;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>

using namespace std;

void failed() { cout << "NA" << endl; }

int main() {

  int Q1, B, C1, C2, Q2;

  while (cin >> Q1 >> B >> C1 >> C2 >> Q2) {
    if (Q1 == 0)
      break;

    if (B / C1 == 0) {
      failed();
      continue;
    }

    int AizuCh = min(B / C1, Q2);
    int NCh = 0;
    for (;;) {

      if (AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl;
        break;
      } else if (B - (NCh + 1) * C2 - AizuCh * C1 >= 0) {
        NCh += (B - NCh * C2 - AizuCh * C1) / C2;
      } else if (B - (NCh + 1) * C2 - (AizuCh - 1) * C1 >= 0) {
        NCh++, AizuCh--;
      }

      if (AizuCh <= 0) {
        failed();
        break;
      }
    }
  }

  return 0;
}
Problem p00186 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  long long int q1, b, c1, c2, q2, now;

  while (cin >> q1 && q1) {

    cin >> b >> c1 >> c2 >> q2;

    /*ツ全ツづ地ツ鶏ツづ可シツ津ィ*/
    now = q2 * c1;

    /*ツ必ツ要ツ湘ーツ個渉づーツ鳴楪つスツつキツづヲツつ、ツづ可地ツ鶏ツづ個青板づーツ陳イツ静淞つオツづつ「ツつュ*/
    while (now > b) {
      now -= c1;
      q2--;
    }

    while (q2 + (b - now) / c2 < q1 && q2 > 0) {
      now -= c1;
      q2--;
    }

    if (q2 > 0) {
      cout << q2 << " " << (b - now) / c2 << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int q1, b, c1, c2, q2, now;

  while (cin >> q1 && q1) {

    cin >> b >> c1 >> c2 >> q2;

    /*ツ全ツづ地ツ鶏ツづ可シツ津ィ*/
    now = q2 * c1;

    /*ツ必ツ要ツ湘ーツ個渉づーツ鳴楪つスツつキツづヲツつ、ツづ可地ツ鶏ツづ個青板づーツ陳イツ静淞つオツづつ「ツつュ*/
    while (now > b) {
      now -= c1;
      q2--;
    }

    while (q2 + (b - now) / c2 < q1) {
      now -= c1;
      q2--;
    }

    if (q2 > 0) {
      cout << q2 << " " << (b - now) / c2 << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}
Problem p00186 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int q1, b, c1, c2, q2;
  while (cin >> q1 && q1) {
    cin >> b >> c1 >> c2 >> q2;
    int X = 0, Y = 0;
    while (X < q2 && b - c1 >= 0)
      X++, b -= c1;
    while (b - c2 >= 0) {
      Y++;
      b -= c2;
    }
    while (X && X + Y < q1) {
      X--;
      b += c1;
      while (b - c2 >= 0) {
        Y++;
        b -= c2;
      }
    }
    while (b - c2 >= 0) {
      Y++;
      b -= c2;
    }
    if (X + Y < q1 || !X || b < 0)
      cout << "NA" << endl;
    else
      cout << X << " " << Y << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n;
  while (cin >> n, n) {
    int need, ans = 0;
    vector<int> d(n);
    rep(i, n) cin >> d[i];

    cin >> need;
    int l = 0, r = n - 1;
    while (l < r) {
      ans++;
      int m = (l + r) / 2;
      if (d[m] > need)
        r = m - 1;
      else if (d[m] < need)
        l = m + 1;
      else {
        ans--;
        break;
      }
    }
    cout << ans + 1 << endl;
  }
}
Problem p00186 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int q1, b, c1, c2, q2;
long lev;

bool C(long x) {
  lev = (b - c1 * x) / c2; // ツ閉津環鶏ツ禿キツ。
  if (b - c1 * x < 0)
    return false;
  if (q2 < x)
    return false;
  if (lev < 0)
    return false;
  return x + lev >= q1;
}

void solve() {
  long l = 0, u = 10000000;

  for (int i = 0; i < 100; i++) {
    long m = (l + u) / 2;
    C(m) ? l = m : u = m;
  }
  if (u - 1 + lev >= q1 && u - 1 > 0) {
    if (lev < 0)
      lev = 0;
    cout << u - 1 << ' ' << lev << "\\n";
  } else
    cout << "NA\\n";
}

int main() {
  while (cin >> q1) {
    if (!q1)
      break;
    cin >> b >> c1 >> c2 >> q2;
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

void solve() {
  long long int q1, b, c1, c2, q2;
  while (cin >> q1, q1) {
    cin >> b >> c1 >> c2 >> q2;
    if (b / c1 > q1 && b / c1 <= q2) {
      cout << b / c1 << " " << 0 << endl;
    } else {
      long long int chicken = (q1 * c1 - b) / (c1 - c2) + 1;
      long long int aizu_chicken = q1 - chicken;
      bool flag = true;
      if (aizu_chicken <= 0) {
        cout << "NA" << endl;
        flag = false;
      }
      if (flag) {
        if (aizu_chicken > q2) {
          aizu_chicken = q2;
          chicken = (b - c1 * q2) / c2;
        }
        cout << aizu_chicken << " " << chicken << endl;
      }
    }
  }
}

int main() {
  solve();
  return (0);
}
Problem p00186 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  for (;;) {
    int q1, b, c1, c2, q2;
    cin >> q1 >> b >> c1 >> c2 >> q2;
    if (q1 == 0)
      return 0;

    int x = 0;
    int y = q2;
    while (x != y) {
      int z = (x + y + 1) / 2;
      if (b - z * c1 >= 0 && (b - z * c1) / c2 >= q1 - z)
        x = z;
      else
        y = z - 1;
    }

    if (x == 0)
      cout << "NA" << endl;
    else
      cout << x << ' ' << ((b - x * c1) / c2) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  for (;;) {
    int q1, b, c1, c2, q2;
    if (!(cin >> q1 >> b >> c1 >> c2 >> q2))
      return 0;

    if ((b - c1) / min(c1, c2) < q1 - 1)
      cout << "NA" << endl;
    else {
      int x = 1;
      int y = q2;
      while (x != y) {
        int z = (x + y + 1) / 2;
        if (b - z * c1 >= 0 && (b - z * c1) / c2 >= q1 - z)
          x = z;
        else
          y = z - 1;
      }
      cout << x << ' ' << ((b - x * c1) / c2) << endl;
    }
  }
}
Problem p00186 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

void failed() { cout << "NA" << endl; }

int main() {

  int Q1, B, C1, C2, Q2;

  while (cin >> Q1 >> B >> C1 >> C2 >> Q2) {
    if (Q1 == 0)
      break;
    int AizuCh = min(B / C1, Q2);
    int NCh = 0;
    for (;;) {
      if (AizuCh <= 0) {
        failed();
        break;
      }
      NCh = (B - AizuCh * C1) / C2;
      if (AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl;
        break;
      } else
        AizuCh--;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <sstream>

using namespace std;

void failed() { cout << "NA" << endl; }

int main() {

  int Q1, B, C1, C2, Q2;

  while (cin >> Q1) {

    if (Q1 == 0)
      break;

    cin >> B >> C1 >> C2 >> Q2;

    assert(Q2 >= 1);

    /*
    if(B/C1 == 0) {
      failed(); continue;
    }
    */

    int AizuCh = min(B / C1, Q2);
    if (AizuCh <= 0) {
      failed();
      continue;
    }
    int NCh = 0;
    for (;;) {

      NCh = (B - AizuCh * C1) / C2;

      /*
      if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
        NCh = (B - AizuCh*C1)/C2;
      }
      */
      if (AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl;
        break;
      }
      /*
      else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
        NCh ++, AizuCh --;
      }
      */
      else
        AizuCh--;

      if (AizuCh <= 0) {
        failed();
        break;
      }
    }
  }

  return 0;
}
Problem p00186 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <sstream>

using namespace std;

void failed() { cout << "NA" << endl; }

int main() {

  int Q1, B, C1, C2, Q2;

  while (cin >> Q1) {

    if (Q1 == 0)
      break;

    cin >> B >> C1 >> C2 >> Q2;

    assert(Q2 >= 0);

    /*
    if(B/C1 == 0) {
      failed(); continue;
    }
    */

    int AizuCh = min(B / C1, Q2);
    if (AizuCh <= 0) {
      failed();
      continue;
    }
    int NCh = 0;
    for (;;) {

      NCh = (B - AizuCh * C1) / C2;

      /*
      if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
        NCh = (B - AizuCh*C1)/C2;
      }
      */
      if (AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl;
        break;
      }
      /*
      else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
        NCh ++, AizuCh --;
      }
      */
      else
        AizuCh--;

      if (AizuCh <= 0) {
        failed();
        break;
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <sstream>

using namespace std;

void failed() { cout << "NA" << endl; }

int main() {

  int Q1, B, C1, C2, Q2;

  while (cin >> Q1) {

    if (Q1 == 0)
      break;

    cin >> B >> C1 >> C2 >> Q2;

    stringstream ss;
    ss << "Q: " << Q1 << " B: " << B << " C1: " << C1 << " C2: " << C2
       << " Q2: " << Q2;

    assert(Q2 >= 1 && ss.str().c_str());

    /*
    if(B/C1 == 0) {
      failed(); continue;
    }
    */

    int AizuCh = min(B / C1, Q2);
    if (AizuCh <= 0) {
      failed();
      continue;
    }
    int NCh = 0;
    for (;;) {

      NCh = (B - AizuCh * C1) / C2;

      /*
      if(B - (NCh+1)*C2 - AizuCh*C1 >= 0) {
        NCh = (B - AizuCh*C1)/C2;
      }
      */
      if (AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl;
        break;
      }
      /*
      else if(B - (NCh+1)*C2 - (AizuCh-1)*C1 >= 0) {
        NCh ++, AizuCh --;
      }
      */
      else
        AizuCh--;

      if (AizuCh <= 0) {
        failed();
        break;
      }
    }
  }

  return 0;
}
Problem p00186 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

int main() {
  int q1, b, c1, c2, q2;

  while (scanf("%d", &q1), q1) {
    scanf("%d %d %d %d", &b, &c1, &c2, &q2);

    int cnt1 = b / c1;
    if (cnt1 > q2) {
      cnt1 = q2;
    }
    b -= cnt1 * c1;

    int cnt2 = b / c2;
    if (cnt2 < 0) {
      cnt2 = 0;
    }
    b -= cnt2 * c2;

    while (true) {
      if (cnt1 + cnt2 >= q1 || cnt1 <= 0) {
        break;
      }

      cnt1--;
      b += c1;

      b += cnt2 * c2;
      cnt2 = b / c2;
      b -= cnt2 * c2;
    }

    if (cnt1 <= 0) {
      puts("NA");
    } else {
      printf("%d %d\\n", cnt1, cnt2);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

int main() {
  int q1, b, c1, c2, q2;

  while (scanf("%d", &q1), q1) {
    scanf("%d %d %d %d", &b, &c1, &c2, &q2);

    int cnt1 = b / c1;
    if (cnt1 > q2) {
      cnt1 = q2;
    }

    b -= cnt1 * c1;

    if (0 <= b && q1 <= cnt1) {
      printf("%d %d\\n", cnt1, 0);
      continue;
    }

    int cnt2 = b / c2;
    b -= cnt2 * c2;

    while (true) {
      if (cnt1 + cnt2 >= q1 && b >= 0) {
        break;
      }

      cnt1--;
      b += c1;

      b += cnt2 * c2;
      cnt2 = b / c2;
      b -= cnt2 * c2;
    }

    if (cnt1 > 0) {
      printf("%d %d\\n", cnt1, cnt2);
    } else {
      puts("NA");
    }
  }

  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int n;
int a[112345];

int main() {
  while (cin >> n, n) {
    REP(i, n) scanf("%d", a + i);

    int k;
    cin >> k;

    int l = 0, r = n - 1;
    int c = 0;
    while (r - l >= 0) {
      int m = (l + r) / 2;
      c++;

      if (a[m] == k)
        break;
      if (a[m] < k)
        l = m + 1;
      else
        r = m - 1;
    }

    cout << c << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int n;
int a[112345];

int main() {
  while (cin >> n, n) {
    REP(i, n) scanf("%d", a + i);

    int k;
    cin >> k;

    int l = 0, r = n - 1;
    int c = 0;
    while (true) {
      int m = (l + r) / 2;
      c++;

      if (a[m] == k)
        break;
      if (r == l)
        break;

      if (a[m] < k)
        l = m + 1;
      else
        r = m - 1;
    }

    cout << c << endl;
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second
#define all(v) v.begin(), v.end()
#define rev(v) v.rbegin(), v.rend()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int INF = 1 << 29;

int num[101];
int main(void) {
  int n;
  while (cin >> n, n) {
    rep(i, n) cin >> num[i];
    int x;
    cin >> x;

    int lb = 0, ub = n - 1;
    int ans = 0;
    while (ub >= lb) {
      int mid = (ub + lb) / 2;
      ans++;
      if (num[mid] == x)
        break;
      if (num[mid] < x)
        lb = mid + 1;
      else
        ub = mid - 1;
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second
#define all(v) v.begin(), v.end()
#define rev(v) v.rbegin(), v.rend()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int INF = 1 << 29;

int num[101];
int main(void) {
  int n;
  while (cin >> n, n) {
    rep(i, n) cin >> num[i];
    int x;
    cin >> x;

    int lb = 0, ub = n - 1;
    int ans = 0;
    while (1) {
      int mid = (ub + lb) / 2;
      ans++;
      if (ub == lb)
        break;
      if (num[mid] == x)
        break;
      if (num[mid] < x)
        lb = mid + 1;
      else
        ub = mid - 1;
    }

    cout << ans << endl;
  }

  return 0;
}