alexjercan/bugnet huggingface dataset:
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

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

int A[100000], n;

int Search(int key) {
  int l = 0;
  int r = n;
  int m;
  while (l < r) {
    m = (l + r) / 2;
    if (A[m] < key)
      l = m + 1;
    if (A[m] > key)
      r = m;
    else if (A[m] == key)
      return 1;
  }

  return 0;
}

int main() {
  int q, k, s = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> k;

    if (Search(k))
      s++;
  }

  cout << s << "\\n";
  return 0;
}

Error: 0

Fixed submission:

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

int A[10000], n;

int Search(int key) {
  int l = 0;
  int r = n;
  int m;
  while (l < r) {
    m = (l + r) / 2;
    if (A[m] < key)
      l = m + 1;
    if (A[m] > key)
      r = m;
    else if (A[m] == key)
      return 1;
  }

  return 0;
}

int main() {
  int q, k, s = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> k;

    if (Search(k))
      s++;
  }

  cout << s << "\\n";
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <stdlib.h>
using namespace std;

bool twodiv_search(const int n, int S[], const int key) {
  int start = 0;
  int end = n;
  int mid;
  bool res = false;

  while (end - start > 0) {
    mid = (start + end) / 2;
    //        printf("s %d e %d mid %d S[mid] %d\\n", start, end, mid, S[mid]);
    if (S[mid] < key) {
      if (start == mid)
        break;
      start = mid;
    } else if (S[mid] > key) {
      if (end == mid)
        break;
      end = mid;
    } else {
      res = true;
      break;
    }
  }
  return res;
}

int main(void) {

  int n;
  int q;
  int key;
  int count = 0;

  scanf("%d", &n);
  int *S = new int[n + 1];
  for (int i = 0; i < n; i++)
    scanf("%d", &S[i]);

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    count += twodiv_search(n, S, key);
  }
  printf("%d\\n", count);

  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
using namespace std;

bool twodiv_search(const int n, int S[], const int key) {
  int start = 0;
  int end = n - 1;
  int mid;
  bool res = false;

  while (end - start > 0) {
    mid = (start + end) / 2;
    if (S[mid] < key) {
      start = mid;
    } else if (S[mid] > key) {
      end = mid;
    } else {
      res = true;
      break;
    }
  }
  return res;
}

int main(void) {

  int n;
  int q;
  int key;
  int count = 0;

  scanf("%d", &n);
  int *S = new int[n + 1];
  for (int i = 0; i < n; i++)
    scanf("%d", &S[i]);

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    count += twodiv_search(n, S, key);
  }
  printf("%d\\n", count);

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

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int ans = 0;
  int n, q;
  vector<int> s;
  vector<int> t;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int input;
    cin >> input;
    s.push_back(input);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int input;
    cin >> input;
    t.push_back(input);
  }

  //??蔵?卒蔵?????????:t[i]
  for (int i = 0; i < q; i++) {
    int low = 0;  //???????????????
    int high = n; //???????????????
    int mid;
    while (low <= high) {
      mid = (low + high) / 2;
      if (s[mid] == t[i]) {
        ans++;
        break;
      } else if (s[mid] < t[i]) {
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int ans = 0;
  int n, q;
  vector<int> s;
  vector<int> t;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int input;
    cin >> input;
    s.push_back(input);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int input;
    cin >> input;
    t.push_back(input);
  }
  sort(t.begin(), t.end());

  int prev = 0;
  for (int i = 0; i < q; i++) {
    for (int j = prev + 1; j < n; j++) {
      if (t[i] == s[j]) {
        ans++;
        break;
        prev = j;
      }
    }
  }
  cout << ans << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

#define MAX_N 100000
#define MAX_Q 50000

using namespace std;

int S[MAX_N + 5], T[MAX_Q + 5];

bool searchS(int x, int length) {
  int left = 0, right = length, mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (S[mid] == x) {
      return true;
    } else if (S[mid] < x) {
      left = mid + 1;
    } else if (S[mid] > x) {
      right = mid;
    }
  }
  return false;
}

int main() {
  int n, q, sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &T[i]);
    if (searchS(T[i], n)) {
      sum++;
    }
  }
  printf("%d\\n", sum);

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

#define MAX_N 100000
#define MAX_Q 50000

using namespace std;

int S[MAX_N + 5], T[MAX_Q + 5];

bool searchS(int x, int length) {
  int left = 0, right = length - 1;
  while (left < right) {
    int mid = (left + right) / 2;
    if (S[mid] == x) {
      return true;
    } else if (S[mid] < x) {
      left = mid;
    } else if (S[mid] > x) {
      right = mid;
    }
  }
  return false;
}

int main() {
  int n, q, sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &T[i]);
    if (searchS(T[i], n)) {
      sum++;
    }
  }
  printf("%d\\n", sum);

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

#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool binSearch(int *s, int n, int target) {
  int lo = 0;
  int hi = n - 1;
  while (lo <= hi) {
    int mid = lo + (hi - lo) / 2;
    if (s[mid] == target)
      return true;
    else if (target > s[mid])
      lo = mid + 1;
    else
      hi = mid - 1;
  }
  return false;
}

int main() {
  int val, n, q;
  int s[100000], t[50000];

  cin >> n;
  for (int i = 0; i < n && cin >> val; ++i)
    s[i] = val;

  int count = 0;
  cin >> q;
  while (q--) {
    cin >> val;
    if (binSearch(s, n, val))
      count++;
  }

  cout << count << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool binSearch(int *s, int n, int target) {
  int lo = 0;
  int hi = n - 1;
  while (lo <= hi) {
    int mid = lo + (hi - lo) / 2;
    if (s[mid] == target)
      return true;
    else if (target > s[mid])
      lo = mid + 1;
    else
      hi = mid - 1;
  }
  return false;
}

int main() {
  int val, n, q;
  int s[10000], t[500];

  cin >> n;
  for (int i = 0; i < n && cin >> val; ++i)
    s[i] = val;

  int count = 0;
  cin >> q;
  while (q--) {
    cin >> val;
    if (binSearch(s, n, val))
      count++;
  }

  cout << count << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

int main() {
  int n, m, ans = 0;

  int A[100001];
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", A + i);
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int a;
    scanf("%d", &a);
    int j = 0;
    A[n] = a;
    while (A[j] != a)
      j++;
    if (j < n)
      ans++;
  }
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>

int main() {
  int n, m, ans = 0;

  int A[100001];
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    scanf("%d", A + i);

  scanf("%d", &m);

  for (int i = 0; i < m; i++) {
    int a;
    scanf("%d", &a);
    int j = 0;
    while (A[j] != a)
      j++;
    if (j < n)
      ans++;
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

bool binSearch(int A[], int target, int size) {
  int left = 0, right = size, mid;
  while (left < right) {
    mid = (left + right) / 2;
    // cout <<  "left : " << left << ", right : " << right << ", mid : " << mid
    // << endl;
    if (A[mid] == target) {
      // cout << target << " found." << endl;
      return true;
    } else {
      if (A[mid] < target)
        left = mid + 1;
      else
        right = mid;
    }
  }
  return false;
}

int main() {
  int size, query, queryNum, count = 0;

  cin >> size;
  int *A = new int[size];
  for (int i = 0; i < size; i++)
    scanf("%d", &A[i]);

  cin >> queryNum;
  for (int i = 0; i < queryNum; i++) {
    scanf("%d", &query);
    if (binSearch(A, query, size))
      count++;
  }

  cout << count << endl;

  delete[] A;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool binSearch(int A[], int target, int size) {
  int left = 0, right = size - 1, mid;
  while (left <= right) {
    mid = (left + right) / 2;
    // cout <<  "left : " << left << ", right : " << right << ", mid : " << mid
    // << endl;
    if (A[mid] == target) {
      // cout << target << " found." << endl;
      return true;
    } else {
      if (A[mid] < target)
        left = mid + 1;
      else
        right = mid;
    }
  }
  return false;
}

int main() {
  int size, query, queryNum, count = 0;

  cin >> size;
  int *A = new int[size];
  for (int i = 0; i < size; i++)
    scanf("%d", &A[i]);

  cin >> queryNum;
  for (int i = 0; i < queryNum; i++) {
    scanf("%d", &query);
    if (binSearch(A, query, size))
      count++;
  }

  cout << count << endl;

  delete[] A;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int tmp, s[100000], t, n, m = 1, q, count = 0;

  cin >> n >> s[0];
  for (int i = 1; i < n; i++) {
    cin >> tmp;
    if (tmp > s[m - 1]) {
      s[m] = tmp;
      m++;
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    for (int j = 0; j < m; j++) {
      if (s[j] < t)
        continue;
      else if (s[j] == t) {
        count++;
        break;
      } else
        break;
    }
  }

  cout << count << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int tmp, t, n, m = 1, q, count = 0;
  bool s[1000000001] = {};

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    s[tmp] = true;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    if (s[t])
      count++;
  }

  cout << count << endl;

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

#include <bits/stdc++.h>
#define ll long long int
#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n)
using namespace std;

bool binarySearch(int key, int n, int s[]) {
  int left = 0, right = n, mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (key == s[mid])
      return true;
    if (key > s[mid])
      left = mid + 1;
    else if (key < s[mid])
      right = mid;
  }
  return false;
}

int main() {
  int n, q, count, left, right, mid;
  cin >> n;
  int s[n];
  REP(i, n) { cin >> s[i]; }
  cin >> q;
  int t[q];
  REP(i, q) { cin >> t[i]; }

  count = 0;
  REP(i, q) {
    if (binarySearch(t[i], n, s))
      count++;
  }

  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n)
using namespace std;

int main() {
  int n, q, count, left, right, mid;
  cin >> n;
  int s[n];
  REP(i, n) { cin >> s[i]; }
  cin >> q;
  int t[q];
  REP(i, q) { cin >> t[i]; }

  count = 0;
  REP(i, q) {
    left = 0;
    right = n - 1;
    while (left < right) {
      mid = (left + right) / 2;
      if (s[mid] == t[i]) {
        count++;
      } else if (s[mid] > t[i]) {
        right = mid - 1;
      } else if (s[mid] < t[i]) {
        left = mid + 1;
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int main(void) {
  int n, S[100000], i, j, q, T[50000], t, C, L, R, M;
  scanf("%d", &n);
  for (i = 0; i != n; i++) {
    scanf("%d", &S[i]);
  }
  scanf("%d", &q);
  for (j = 0; j != q; j++) {
    scanf("%d", &T[j]);
  }

  C = 0;
  for (i = 0; i != q; i++) {
    L = 0;
    R = n;
    while (L < R) {
      M = (L + R) / 2;
      if (S[M] == T[i]) {
        C++;

        break;
      }
      if (S[M] > T[i]) {
        R = M;
      } else {
        L = M + 1;
      }
    }
  }
  printf("%d\\n", C);
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main(void) {
  int n, S[10000], i, j, q, T[500], t, C, L, R, M;
  scanf("%d", &n);
  for (i = 0; i != n; i++) {
    scanf("%d", &S[i]);
  }
  scanf("%d", &q);
  for (j = 0; j != q; j++) {
    scanf("%d", &T[j]);
  }

  C = 0;
  for (i = 0; i != q; i++) {
    L = 0;
    R = n;
    while (L < R) {
      M = (L + R) / 2;
      if (S[M] == T[i]) {
        C++;

        break;
      }
      if (S[M] > T[i]) {
        R = M;
      } else {
        L = M + 1;
      }
    }
  }
  printf("%d\\n", C);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122

int main() {
  int n;
  cin >> n;
  int s[n];
  rep(i, 0, n) cin >> s[i];
  int q;
  cin >> q;
  int t[q];
  rep(i, 0, q) cin >> t[i];

  int ans = 0;
  rep(i, 0, q) {
    int left = 0;
    int right = n - 1;
    while (left <= right) {
      int mid = (left + right) / 2;
      if (t[i] == s[mid]) {
        ans++;
        break;
      } else if (t[i] < s[mid]) {
        right = mid - 1;
      } else {
        left = mid + 1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122

int main() {
  int n;
  cin >> n;
  int s[n];
  rep(i, 0, n) cin >> s[i];
  int q;
  cin >> q;
  int t[q];
  rep(i, 0, q) cin >> t[i];

  int ans = 0;
  rep(i, 0, q) {
    int left = 0;
    int right = n;
    while (left < right) {
      int mid = (left + right) / 2;
      if (t[i] == s[mid]) {
        ans++;
        break;
      } else if (t[i] < s[mid]) {
        right = mid;
      } else {
        left = mid;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int s[100001];

int nibun(int k, int n) {
  int h, t;
  h = 0;
  t = n;
  while (h < t) {
    if (k == s[(h + t) / 2]) {
      return 1;
    } else if (k < s[(h + t) / 2])
      t = (h + t) / 2;
    else if (k > s[(h + t) / 2])
      h = (h + t) / 2 + 1;
  }
  return 0;
}

int main() {
  int n, q, t, h, k;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    h = 0;
    cin >> t;
    h = nibun(t, n);
    if (h == 1)
      k++;
  }
  cout << k << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int m[1000000001] = {0}, a, b, cou;
  cin >> a;
  for (int i = 0; i < a; i++) {
    cin >> b;
    m[b] = 1;
  }
  cin >> b;
  cou = 0;
  for (int j = 0; j < b; j++) {
    cin >> a;
    cou += m[a];
  }
  cout << cou << endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h> /* pow */
#include <stack>
#include <stdio.h> /* printf */
#include <stdlib.h>
#include <string>
#include <vector>

bool binarySearch(std::vector<int> &list, int L, int R, int T) {
  if (L > R)
    return false;
  int m = floor((L + R) / 2);
  if (T < list[m]) {
    return binarySearch(list, L, m - 1, T);
  } else if (T > list[m]) {
    return binarySearch(list, m + 1, R, T);
  } else {
    return true;
  }
}

int linearSearch(std::vector<int> &list, std::vector<int> &seq) {
  int found = 0;
  for (int i = 0; i < seq.size(); i++) {
    if (binarySearch(list, 0, list.size() - 1, seq[i]))
      found++;
  }
  return found;
}

int main(void) {
  int value;
  std::vector<int> list;
  int n;
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> value;
    list.push_back(value);
  }
  std::vector<int> seq;
  int q;
  std::cin >> q;
  for (int j = 0; j < q; j++) {
    std::cin >> value;
    seq.push_back(value);
  }
  std::cout << linearSearch(list, seq) << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h> /* pow */
#include <stack>
#include <stdio.h> /* printf */
#include <stdlib.h>
#include <string>
#include <vector>

int linearSearch(std::vector<int> &list, std::vector<int> &seq) {
  int found = 0;
  for (int i = 0; i < seq.size(); i++) {
    int j = 0;
    while (j < list.size() && list[j] <= seq[i]) {
      if (list[j] == seq[i]) {
        j = list.size();
        found++;
      } else {
        j++;
      }
    }
  }
  return found;
}

int main(void) {
  int value;
  std::vector<int> list;
  int n;
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> value;
    list.push_back(value);
  }
  std::vector<int> seq;
  int q;
  std::cin >> q;
  for (int j = 0; j < q; j++) {
    std::cin >> value;
    seq.push_back(value);
  }
  std::cout << linearSearch(list, seq) << std::endl;
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int Binary(int a[], int sizea, int b[], int sizeb) {
  int num = 0;
  int j = 0;
  int i = 0;
  do {
    if (i >= sizea || j >= sizeb) {
      break;
    }

    if (a[i] == b[j]) {
      num++;
      i++;
      j++;
    } else if (a[i] > b[j]) {
      j++;
      if (j >= sizeb) {
        break;
      }
    } else if (a[i] < b[j]) {
      i++;
      if (i >= sizea) {
        break;
      }
    }
  } while (i <= sizea || j <= sizeb);
  return num;
}

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

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

  sort(T, T + q);

  cout << Binary(S, n, T, q) << endl;
}


Error: TLE

Fixed submission:

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

int Binary(int a[], int sizea, int b[], int sizeb) {
  int num = 0;
  int j = 0;
  int i = 0;
  do {
    if (i >= sizea || j >= sizeb) {
      break;
    }

    if (a[i] == b[j]) {
      num++;
      i++;
      j++;
    } else if (a[i] > b[j]) {
      j++;
      if (j >= sizeb) {
        j = sizeb - 1;
      }
    } else if (a[i] < b[j]) {
      i++;
      if (i >= sizea) {
        i = sizea - 1;
      }
    }
  } while (i <= sizea || j <= sizeb);
  return num;
}

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

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

  sort(T, T + q);

  cout << Binary(S, n, T, q) << endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
int a[1000000], n;
int binarysearch(int key) {
  int left = 0;
  int right = n;
  while (left < right) {
    int mid = (left + right) / 2;
    if (a[mid] == key)
      return 1;
    if (a[mid] < key)
      left = mid + 1;
    else if (key < a[mid])
      right = mid;
  }
  return 0;
}
int main() {
  int i, q, k, sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &k);
    if (binarysearch(k))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int a[1000000], n;
int binarysearch(int key) {
  int start = 0;
  int end = n;
  while (start <= end) {
    int mid = (start + end) / 2;
    if (a[mid] == key)
      return 1;
    if (a[mid] < key)
      start = mid + 1;
    else if (key < a[mid])
      end = mid;
  }
}
int main() {
  int i, q, k, sum = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &k);
    if (binarysearch(k))
      sum++;
  }
  printf("%d\\n", sum);
  return 0;
}
Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> vc(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &vc[i]);
  int ans = 0, q, a;
  scanf("%d", &q);
  sort(vc.begin(), vc.end());
  while (q--) {
    cin >> a;
    if (binary_search(vc.begin(), vc.end(), a))
      ans++;
  }
  printf("%d\\n", ans);
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

int main(void) {
  int n, q;
  std::vector<int> S(n);
  int T;
  int cnt = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &S[i]);

  std::cin >> q;
  while (q--) {
    std::cin >> T;
    ;
    if (std::binary_search(S.begin(), S.end(), T))
      cnt++;
  }
  std::cout << cnt << std::endl;
}
Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main(void) {
  int n, q, si, ti, l, r, m, c = 0;
  int S[100005];
  int T[50005];

  scanf("%d", &n);
  for (si = 0; si < n; si++) {
    scanf("%d", &S[si]);
  }
  scanf("%d", &q);
  for (ti = 0; ti < q; ti++) {
    scanf("%d", &T[ti]);
  }
  for (ti = 0; ti < q; ti++) {
    l = 0;
    r = n;
    while (l < r) {
      m = (l + r) / 2;
      if (S[m] > T[ti]) {
        r = m;
      } else if (S[m] < T[ti]) {
        l = m + 1;
      } else {
        c++;
        break;
      }
    }
  }

  printf("%d\\n", c);

  return 0;
}


Error: TLE

Fixed submission:

/*16D8103008D Yukki Nishimura 5/14 */

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

#define MAX 100000

bool check(int C[], int y, int j) {
  if (j != 0) {
    for (int i = 0; i < j; i++) {
      if (C[i] == y) {
        return true;
      }
    }
  }

  return false; // 探索失敗
}

int func(int S[], int T[], int q, int n) {
  int s = n;
  int t = q;
  int j = 0;

  int C[MAX] = {};

  for (int i = 0; i < s; i++) {

    for (int k = 0; k < t; k++) {
      if (check(C, S[i], j)) {
        break;
      }

      else if (S[i] == T[k]) {
        C[j] = S[i];
        j++;
        break;
      }
    }
  }

  return j;
}

int main(void) {
  int n, q, c;

  int S[MAX] = {}, T[MAX] = {};

  cin >> n;

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

  cin >> q;

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

  c = func(S, T, q, n);

  cout << c << endl;

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

#include <stdio.h>

int main() {
  int n, q, i, j, ans = 0;
  int S[100000], T[50000];

  int l, r, m;

  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &S[i]);

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &T[i]);

    l = 0;
    r = n;
    while (l < r) {
      m = (l + r) / 2;
      if (T[i] == S[m]) {
        ans++;
        break;
      } else if (T[i] < S[m]) {
        r = m;
      } else {
        l = m + 1;
      }
    }
  }

  printf("%d\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {
  int n, q, i, j, ans = 0;
  int S[100000], T[50000];

  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &S[i]);

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &T[i]);
    for (j = 0; j < n; j++) {
      if (T[i] == S[j]) {
        ans++;
        break;
      }
    }
  }

  printf("%d\\n", ans);

  return 0;
}