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

#include <cstdio>
#include <iostream>
using namespace std;

#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)

int n, l[100], t;

void solve() {
  rep(i, n) cin >> l[i];
  cin >> t;

  int lb = 0, ub = n - 1, res = 0;
  while (ub >= lb) {
    int mid = (ub + lb) / 2;
    if (l[mid] == t) {
      res++;
      break;
    }
    if (l[mid] < t) {
      lb = mid + 1;
    } else if (l[mid] > t) {
      if (mid != 0)
        ub = mid - 1;
    }
    res++;
  }

  cout << res << endl;
}

int main(int argc, char const *argv[]) {
  while (cin >> n, n) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)

int n, l[100], t;

void solve() {
  rep(i, n) cin >> l[i];
  cin >> t;

  int lb = 0, ub = n - 1, res = 1;
  while (ub != lb) {
    // if (lb > ub) break;
    int mid = (ub + lb) / 2;
    // printf("lb=%d ub=%d mid=%d\\n", lb, ub, mid);

    if (l[mid] < t)
      lb = mid + 1;
    else if (l[mid] > t) {
      if (mid != 0)
        ub = mid - 1;
      else
        ub = mid;
    } else {
      break;
    }
    res++;
  }

  cout << res << endl;
}

int main(int argc, char const *argv[]) {
  while (cin >> n, n) {
    solve();
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

////////////////*My library*////////////////////////
#define rep_to(a, i, n) for (i = a; i < n; i++)
#define rep(i, n) for (i = 0; i < n; i++)
#define rep_r(i, n) for (i = n - 1; i >= 0; i--)
#define debug_i(x) printf("x:%d\\n", x)
#define debug_f(x) printf("x:%f\\n", x)
#define debug_s(str) printf("str:%s\\n", str)
#define debug_c(c) printf("c:%c\\n", c)
#define s_i1(x) scanf("%d", &x)
#define s_i2(x, y) scanf("%d %d", &x, &y)
#define s_i3(x, y, z) scanf("%d %d %d", &x, &y, &z)
////////////////////////////////////////////////////
////////////////////////
/*prime()*/
/*primecount()*/
/*sort()*/
///////////////////////
int main() {
  int i, j;
  int n, num[100000] = {}, key;

  while (1) {
    s_i1(n);
    if (n == 0)
      break;
    rep(i, n) s_i1(num[i]);
    s_i1(key);

    /////????????¢?´¢//////////////////

    int min = 0;
    int max = n - 1;
    int mid = (min + max) / 2;
    int count = 0;
    while (min <= max) {
      count++;
      mid = (min + max) / 2;
      if (key == num[mid])
        break;
      else if (key > num[mid])
        min = mid + 1;
      else if (key < num[mid])
        max = mid - 1;
    }
    ///////////////////////////////
    printf("%d\\n", count);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

////////////////*My library*////////////////////////
#define rep_to(a, i, n) for (i = a; i < n; i++)
#define rep(i, n) for (i = 0; i < n; i++)
#define rep_r(i, n) for (i = n - 1; i >= 0; i--)
#define debug_i(x) printf("x:%d\\n", x)
#define debug_f(x) printf("x:%f\\n", x)
#define debug_s(str) printf("str:%s\\n", str)
#define debug_c(c) printf("c:%c\\n", c)
#define s_i1(x) scanf("%d", &x)
#define s_i2(x, y) scanf("%d %d", &x, &y)
#define s_i3(x, y, z) scanf("%d %d %d", &x, &y, &z)
////////////////////////////////////////////////////
////////////////////////
/*prime()*/
/*primecount()*/
/*sort()*/
///////////////////////
int main() {
  int i, j;
  int n, num[100000] = {}, key;

  while (1) {
    s_i1(n);
    if (n == 0)
      break;
    rep(i, n) s_i1(num[i]);
    s_i1(key);

    /////????????¢?´¢//////////////////

    int min = 0;
    int max = n - 1;
    int mid = (min + max) / 2;
    int count = 0;
    while (1) {
      count++;
      mid = (min + max) / 2;
      if (min == max)
        break;
      // else if(key!=num[mid] && min==max)break;
      else if (key > num[mid])
        min = mid + 1;
      else if (key < num[mid])
        max = mid - 1;
    }
    ///////////////////////////////
    printf("%d\\n", count);
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

std::vector<int> v;

int main() {
  int n;
  while (std::cin >> n, n) {
    v.clear();

    for (int i = 0; i != n; ++i) {
      int a;
      std::cin >> a;
      v.push_back(a);
    }

    int b;
    std::cin >> b;

    int ans = 0;
    int l = 0, r = v.size() - 1;
    while (l <= r) {
      int mid = (l + r) / 2;

      ++ans;

      if (r <= l)
        break;

      if (v[mid] < b)
        l = mid + 1;
      else if (v[mid] > b)
        r = mid - 1;
      else
        break;
    }

    std::cout << ans << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

std::vector<int> v;

int main() {
  int n;
  while (std::cin >> n, n) {
    v.clear();

    for (int i = 0; i != n; ++i) {
      int a;
      std::cin >> a;
      v.push_back(a);
    }

    int b;
    std::cin >> b;

    int ans = 0;
    int l = 0, r = v.size() - 1;
    while (l <= r) {
      int mid = (l + r) / 2;

      ++ans;

      if (r <= l)
        break;

      if (v[mid] < b)
        l = mid + 1;
      else if (v[mid] > b)
        r = mid - 1;
    }

    std::cout << ans << std::endl;
  }

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

#include <iostream>

using namespace std;

#define MAX_N 101

int a[MAX_N];

int main() {
  int n, f;
  while (cin >> n) {
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    cin >> f;
    int count = 0;
    int r = n - 1, l = 0;
    int mid;
    while (true) {
      mid = (r + l) / 2;
      if (r < l)
        break;
      count++;
      if (a[mid] == f) {
        break;
      }
      if (r == l)
        break;
      if (a[mid] < f) {
        l = mid + 1;
      }
      if (a[mid] > f) {
        r = mid - 1;
      }
    }
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

#define MAX_N 101

int a[MAX_N];

int main() {
  int n, f;
  while (cin >> n) {
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    cin >> f;
    int count = 0;
    int r = 0, l = n - 1;
    int mid;
    while (true) {
      mid = (r + l) / 2;
      count++;
      if (a[mid] == f) {
        break;
      }
      if (l == r) {
        break;
      }
      if (a[mid] < f) {
        r = mid + 1;
      }
      if (a[mid] > f) {
        l = mid - 1;
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  while (1) {
    int n;
    cin >> n;

    if (n == 0)
      break;

    int A[n];
    for (int i = 0; i < n; i++)
      cin >> A[i];

    int x;
    cin >> x;
    int ans = 0;
    int r = n - 1, l = 0;

    while (l <= r) {
      ans++;
      int mid = (r + l) / 2;
      if (A[mid] == x || l > r)
        break;
      else if (A[mid] < x)
        l = mid + 1;
      else if (A[mid] > x)
        r = mid - 1;
    }

    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  while (1) {
    int n;
    cin >> n;

    if (n == 0)
      break;

    int A[n];
    for (int i = 0; i < n; i++)
      cin >> A[i];

    int x;
    cin >> x;
    int ans = 0;
    int r = n - 1, l = 0;

    while (l != r) {
      ans++;
      int mid = (r + l) / 2;
      if (A[mid] == x)
        break;
      else if (A[mid] < x)
        l = mid + 1;
      else if (A[mid] > x)
        r = mid - 1;
    }

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

// 43
#include <iostream>

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    int a[100];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int ans = 0;
    int f = 0, l = n - 1;
    int g;
    cin >> g;
    for (;;) {
      int m = f + (l - f) / 2;
      if (l - f < 0) {
        break;
      } else {
        ans++;
        if (a[m] == g) {
          break;
        } else if (a[m] > g) {
          l = m - 1;
        } else {
          f = m + 1;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 43
#include <iostream>

using namespace std;

int main() {
  for (int n; cin >> n, n;) {
    int a[100];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int ans = 1;
    int f = 0, l = n - 1;
    int g;
    cin >> g;
    for (;; ans++) {
      int m = f + (l - f) / 2;
      if (a[m] == g || l - f == 0) {
        break;
      } else if (a[m] > g) {
        l = m - 1;
      } else {
        f = m + 1;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n;
  for (; cin >> n, n;) {
    int data[n];
    for (int i = 0; i < n; i++) {
      cin >> data[i];
    }
    int s;
    cin >> s;
    int hi = 0, lo = n - 1;
    int cnt = 0;
    while (true) {
      int p = (hi + lo) / 2;

      //	  cout<<"HI:"<<hi<<" LO:"<<lo<<" "<<cnt<<endl;
      //	  cout<<"data[p]:"<<data[p]<<" s:"<<s<<endl;
      if (hi > lo)
        break;
      cnt++;
      if (data[p] == s)
        break;
      else if (data[p] < s)
        hi = p + 1;
      else
        lo = p - 1;
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  for (; cin >> n, n;) {
    int data[n];
    for (int i = 0; i < n; i++) {
      cin >> data[i];
    }
    int s;
    cin >> s;
    int hi = 0, lo = n - 1;
    int cnt = 0;
    while (true) {
      int p = (hi + lo) / 2;
      cnt++;
      //  cout<<hi<<" "<<lo<<" "<<cnt<<endl;
      if (data[p] == s)
        break;
      if (hi == lo)
        break;
      if (data[p] < s)
        hi = p + 1;
      else
        lo = p - 1;
    }
    cout << cnt << endl;
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

int main() {

  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int P[105] = {};
    for (int i = 0; i < n; i++) {
      cin >> P[i];
    }

    sort(P, P + n);
    int C = 0;
    int x;
    cin >> x;
    int H = (n - 1) / 2;
    int L = n, S = 0;
    while (S < L) {
      // cout<<L<<" "<<S<<endl;
      if (L == S) {
        // cout<<"%";
        C++;
        break;
      }
      if (P[H] == x) {
        C++;
        break;
      }

      if (P[H] < x) {
        S = H + 1;
        H = (S + L - 1) / 2;
        C++; // cout<<"#";//cout<<H<<endl;//break;
      } else if (P[H] > x) {
        L = H;
        H = (S + L - 1) / 2;
        C++; // cout<<"$";//cout<<H;//break;
      }
    }
    cout << C << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

int main() {

  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int P[105] = {};
    for (int i = 0; i < n; i++) {
      cin >> P[i];
    }

    sort(P, P + n);
    int C = 0;
    int x;
    cin >> x;
    int H = (n - 1) / 2;
    int L = n - 1, S = 0;
    while (1) {
      // cout<<L<<" "<<S<<endl;
      if (L == S) {
        // cout<<"%";
        C++;
        break;
      }
      if (P[H] == x) {
        C++;
        break;
      }

      else if (P[H] < x) {
        S = H + 1;
        H = (S + L) / 2;
        C++; // cout<<"#";//cout<<H<<endl;//break;
      } else if (P[H] > x) {
        L = H - 1;
        H = (S + L) / 2;
        C++; // cout<<"$";//cout<<H;//break;
      }
    }
    cout << C << endl;
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    int target;
    int cnt = 0;
    int l = 0, r = n - 1, m;
    int data[100001];

    for (int i = 0; i < n; i++)
      cin >> data[i];
    cin >> target;

    while (l <= r) {
      m = (l + r) / 2;
      cnt++;
      if (data[m] == target)
        break;
      if (target > data[m])
        l = m + 1;
      else
        r = m - 1;
    }

    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    int target;
    int cnt = 1;
    int l = 0, r = n - 1, m;
    int data[100001];

    for (int i = 0; i < n; i++)
      cin >> data[i];
    cin >> target;

    while (l != r) {
      m = (l + r) / 2;
      if (data[m] == target)
        break;
      if (target > data[m])
        l = m + 1;
      else
        r = m - 1;
      cnt++;
    }

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

#include <iostream>
using namespace std;

int find(int *a, int b, int e, int t) {
  int m = (b + e) / 2;
  if (b > e) {
    return 1;
  }
  if (t < a[m] && m != b) {
    return 1 + find(a, b, m - 1, t);
  }
  if (t > a[m] && m != e) {
    return 1 + find(a, m + 1, e, t);
  }
  return 1;
}

int main() {
  int n;
  while (cin >> n && n) {
    int a[100] = {0};
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    int d;
    cin >> d;
    cout << find(a, 0, n - 1, d) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int find(int *a, int b, int e, int t) {
  if (b >= e) {
    return 1;
  }
  int m = (b + e / 2);
  if (t < a[m]) {
    return 1 + find(a, b, m - 1, t);
  }
  if (t > a[m]) {
    return 1 + find(a, m + 1, e, t);
  }
  return 1;
}

int main() {
  int n;
  while (cin >> n && n) {
    int a[100] = {0};
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    int d;
    cin >> d;
    cout << find(a, 0, n - 1, d) << endl;
  }

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

#include <iostream>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    int a[100], k;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cin >> k;
    int low = 0, up = n - 1, cnt = 0;
    while (1) {
      cnt++;
      if (up == low)
        break;
      int mid = (up + low) / 2;
      if (a[mid] == k)
        break;
      if (a[mid] < k)
        low = mid + 1;
      else
        up = mid - 1;
      if (up < low)
        break;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    int a[100], k;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cin >> k;
    int low = 0, up = n - 1, cnt = 0;
    while (1) {
      cnt++;
      if (up == low)
        break;
      int mid = (up + low) / 2;
      if (a[mid] == k)
        break;
      if (a[mid] < k)
        low = mid + 1;
      else
        up = mid - 1;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main(void) {
  int i, n, x[104], search;
  int l, r, c;
  int count;

  while (cin >> n && n) {
    for (i = 0; i < n; i++)
      cin >> x[i];
    cin >> search;

    for (count = l = 0, r = n - 1, c = (l + r) / 2; l <= r;
         c = (l + r) / 2, count++) {
      if (search < x[c])
        r = c - 1;
      else if (search > x[c])
        l = c + 1;
      else {
        count++;
        break;
      }
    }
    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main(void) {
  int i, n, x[104], search;
  int l, r, c;
  int count;

  while (cin >> n && n) {
    for (i = 0; i < n; i++)
      cin >> x[i];
    cin >> search;

    for (count = 1, l = 0, r = n - 1, c = (l + r) / 2; l != r;
         c = (l + r) / 2, count++) {
      if (search < x[c])
        r = c - 1;
      else if (search > x[c])
        l = c + 1;
      else
        break;
    }
    cout << count << endl;
  }

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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

int n, a[100], t;

int main() {
  while (cin >> n && n) {
    rep(i, n) cin >> a[i];
    cin >> t;
    int lb = 0, ub = n - 1;
    int ans = 0;
    while (++ans) {
      // cout<<ans<<" "<<a[lb]<<" "<<a[ub]<<" "<<a[(lb+ub)/2]<<endl;
      if (a[(lb + ub) / 2] == t || lb == ub || lb > ub) {
        cout << ans << endl;
        break;
      } else if (a[(lb + ub) / 2] < t)
        lb = (lb + ub) / 2 + 1;
      else
        ub = (lb + ub) / 2 - 1;
      if (lb > ub) {
        cout << ans << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

int n, a[100], t;

int main() {
  while (cin >> n && n) {
    rep(i, n) cin >> a[i];
    cin >> t;
    int lb = 0, ub = n - 1;
    int ans = 0;
    while (++ans) {
      // cout<<ans<<" "<<lb<<" "<<ub<<" "<<a[(lb+ub)/2]<<endl;
      if (a[(lb + ub) / 2] == t || lb == ub) {
        cout << ans << endl;
        break;
      } else if (a[(lb + ub) / 2] < t)
        lb = (lb + ub) / 2 + 1;
      else
        ub = (lb + ub) / 2 - 1;
    }
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int a;
  while (cin >> a, a) {
    int b[100];
    for (int c = 0; c < a; c++)
      scanf("%d", &b[c]);
    int d;
    cin >> d;
    int MIN = 0, MAX = a - 1;
    int i = 0;
    while (MIN - MAX != 1) {
      int k = floor(double(MIN + MAX) / (double)2);
      if (b[k] > d) {
        MAX = k - 1;
        i++;
      } else if (b[k] < d) {
        MIN = k + 1;
        i++;
      } else {
        i++;
        break;
      }
    }
    cout << i << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  int a;
  while (cin >> a, a) {
    int b[100];
    for (int c = 0; c < a; c++)
      cin >> b[c];
    int d;
    cin >> d;
    int MIN = 0, MAX = a - 1, sum = 1;
    while (1) {
      int e = (MIN + MAX) / 2;
      if (MIN == MAX)
        break;
      if (b[e] < d)
        MIN = e + 1;
      else if (b[e] > d)
        MAX = e - 1;
      else
        break;
      sum++;
    }
    cout << sum << endl;
  }
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int INF = 1 << 30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
typedef pair<int, int> Pii;
typedef pair<int, Pii> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  int N;
  while (scanf("%d", &N), N) {
    int array[128];
    rep(i, N) { scanf("%d", &array[i]); }
    int M;
    scanf("%d", &M);
    int l = 0;
    int r = N;
    int cnt = 0;
    while (l < r) {
      cnt++;
      int med = (l + r - 1) / 2;
      if (array[med] == M)
        break;
      if (array[med] > M)
        r = med;
      else
        l = med + 1;
    }
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int INF = 1 << 30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
typedef pair<int, int> Pii;
typedef pair<int, Pii> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  int N;
  while (scanf("%d", &N), N) {
    int array[128];
    rep(i, N) { scanf("%d", &array[i]); }
    int M;
    scanf("%d", &M);
    int l = 0;
    int r = N - 1;
    int cnt = 1;
    while (l != r) {
      int med = (l + r) / 2;
      if (array[med] > M)
        r = med - 1;
      else
        l = med + 1;
      cnt++;
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00188 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

int main(void) {
  int n;
  int a[100];
  while (cin >> n && n) {
    rep(i, n) cin >> a[i];
    int f;
    cin >> f;
    int l = 0;
    int r = n - 1;
    int ans = 0;
    while (l <= r) {
      ans++;
      int m = (l + r) / 2;
      if (a[m] == f)
        break;
      else if (a[m] < f)
        l = m + 1;
      else
        r = m - 1;
      m = (l + r) / 2;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

int main() {
  int n;
  int a[1000];
  while (cin >> n && n) {
    int cnt = 0;
    rep(i, n) cin >> a[i];
    int f;
    cin >> f;
    int l = 0, r = n - 1;
    // [l,r]
    while (1) {
      // cout << l << " " << r << endl;
      int m = (l + r) / 2;
      cnt++;
      if (a[m] == f) {
      }
      if (a[m] < f) {
        l = m + 1;
      } else {
        r = m - 1;
      }
      if (l == r)
        break;
    }
    cout << cnt + 1 << endl;
  }
}
Problem p00188 (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 a[100000];
int lef;
int rig;

int ans;

bool binary_search_2(int num) {
  if (lef > rig) {
    return false;
  }
  int middle = (lef + rig) / 2;
  ans++;
  if (a[middle] > num) {
    rig = middle - 1;
    binary_search_2(num);
  } else if (a[middle] < num) {
    lef = middle + 1;
    binary_search_2(num);
  } else if (a[middle] == num) {
    return true;
  }
}

bool binary_search(int num, int n) {
  lef = 0;
  rig = n - 1;
  binary_search_2(num);
}

int main() {

  int n, k;

  while (true) {

    cin >> n;

    if (n == 0) {
      break;
    }

    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

    cin >> k;

    ans = 0;

    binary_search(k, n);

    cout << ans << 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 a[100000];
int lef;
int rig;

int ans;

bool binary_search_2(int num) {
  if (lef == rig) {
    ans++;
    return false;
  }
  int middle = (lef + rig) / 2;
  ans++;
  if (a[middle] > num) {
    rig = middle - 1;
    binary_search_2(num);
  } else if (a[middle] < num) {
    lef = middle + 1;
    binary_search_2(num);
  } else if (a[middle] == num) {
    return true;
  }
}

bool binary_search(int num, int n) {
  lef = 0;
  rig = n - 1;
  binary_search_2(num);
}

int main() {

  int n, k;

  while (true) {

    cin >> n;

    if (n == 0) {
      break;
    }

    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

    cin >> k;

    ans = 0;

    binary_search(k, n);

    cout << ans << endl;
  }

  return 0;
}