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

// BISMILLAHHIR RAHMANIR RAHIM
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define min3(a, b, c) min(a, min(b, c))
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define PI 2 * acos(0.0)
#define MOD 9982443
int main() {
  vector<int> v, ch;
  vector<pair<int, int>> mp;
  bool have = false;
  int x, n;
  cin >> x >> n;
  int ara[n];
  for (int i = 0; i < n; i++) {
    cin >> ara[i];
    if (ara[i] == x)
      have = true;
  }
  sort(ara, ara + n);
  if (!have)
    cout << x << endl;
  else {
    int j = 0;
    for (int i = 0; i <= 101; i++) {
      v.push_back(i);
    }
    int sz = v.size();
    for (int i = 0; i < sz; i++) {
      if (ara[j] == v[i])
        j++;
      else
        ch.push_back(v[i]);
    }
    // for(int i=0; i<ch.size(); i++)cout<<ch[i]<<" ";
    // cout<<endl;
    int l = ch.size();
    for (int i = 0; i < l; i++) {
      mp.push_back(make_pair(abs(ch[i] - x), ch[i]));
    }
    sort(mp.begin(), mp.end());
    cout << mp[0].S << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

// BISMILLAHHIR RAHMANIR RAHIM
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define min3(a, b, c) min(a, min(b, c))
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define PI 2 * acos(0.0)
#define MOD 9982443
int main() {
  vector<int> v, ch;
  vector<pair<int, int>> mp;
  bool have = false;
  int x, n;
  cin >> x >> n;
  int ara[n];
  for (int i = 0; i < n; i++) {
    cin >> ara[i];
    if (ara[i] == x)
      have = true;
  }
  sort(ara, ara + n);
  if (!have)
    cout << x << endl;
  else {
    int j = 0, sz = v.size();
    for (int i = 0; i <= n + 8; i++) {
      v.push_back(i);
    }
    for (int i = 0; i < sz; i++) {
      if (ara[j] == v[i])
        j++;
      else
        ch.push_back(v[i]);
    }
    // for(int i=0; i<ch.size(); i++)cout<<ch[i]<<" ";
    // cout<<endl;
    int l = ch.size();
    for (int i = 0; i < l; i++) {
      mp.push_back(make_pair(abs(ch[i] - x), ch[i]));
    }
    sort(mp.begin(), mp.end());
    cout << mp[0].S << endl;
  }
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \\
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, x;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vector<int> nmap(200);
  for (int i = 0; i < 200; i++) {
    nmap[i] = i - 50;
  }

  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    nmap[temp + 50] = -200;
  }

  sort(nmap.begin(), nmap.end());

  int ans = 100, diff = 100;
  for (int i = nmap.size(); i >= 0; i--) {
    if (abs(nmap[i] - x) <= diff) {
      diff = abs(nmap[i] - x);
      ans = nmap[i];
    }
  }

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

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \\
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, x;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vector<int> nmap(100);
  for (int i = 0; i < 100; i++) {
    nmap[i] = i + 1;
  }

  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    nmap.erase(nmap.begin() + temp - 1);
  }

  sort(nmap.begin(), nmap.end());

  int ans = 100, diff = 100;
  for (int i = nmap.size(); i >= 0; i--) {
    if (abs(nmap[i] - x) < diff) {
      diff = abs(nmap[i] - x);
      ans = nmap[i];
    }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <unordered_map>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int main() {
  int x, n;

  unordered_map<int, int> a;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    int y;
    cin >> y;
    a[y]++;
  }
  for (int i = x, j = x;; i--, j++) {
    if (!a[i]) {
      cout << i << endl;
      break;
    } else if (!a[j]) {
      cout << j << endl;
      break;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <unordered_map>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int main() {
  int x, n;
  while (1) {
    unordered_map<int, int> a;
    cin >> x >> n;
    if (n == 0) {
      cout << x << endl;
      break;
    }
    for (int i = 1; i <= n; i++) {
      int y;
      cin >> y;
      a[y]++;
    }
    for (int i = x, j = x;; i--, j++) {
      if (!a[i]) {
        cout << i << endl;
        break;
      } else if (!a[j]) {
        cout << j << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int X, N;
  cin >> X >> N;
  vector<int> vec(102);

  if (N == 0) {
    cout << X << endl;
    return 0;
  }

  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    vec.at(x) = 1;
  }
  pair<int, int> ans(99999, -1);

  for (int i = 0; i < 102; i++) {
    if (vec.at(i) == 0) {
      ans = min(ans, make_pair(abs(X - i), i));
    }
  }
  cout << ans.second << endl;
}


Error: 0

Fixed submission:

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

int main() {
  int X, N;
  cin >> X >> N;
  vector<int> vec(100);

  if (N == 0) {
    cout << X << endl;
    return 0;
  }

  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    vec.at(x - 1) = 1;
  }

  for (int i = 0; i < 100; i++) {
    int a = X + i;
    int b = X - i;

    if (a <= 100 && b >= 0) {
      if (vec.at(a - 1) == 0 && vec.at(b - 1) == 0) {
        cout << b << endl;
        break;
      } else if (vec.at(a - 1) == 0) {
        cout << a << endl;
        break;
      } else if (vec.at(b - 1) == 0) {
        cout << a << endl;
        break;
      }
    } else if (a > 100 && b >= 0) {
      if (vec.at(b - 1) == 0) {
        cout << a << endl;
        break;
      }
    } else if (a <= 100 && b < 0) {
      if (vec.at(a - 1) == 0) {
        cout << a << endl;
        break;
      }
    } else if (a > 100 && b < 0) {
      cout << -1 << endl;
      break;
    }
  }
}
Problem p02641 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int a[105];
bool flag[105];
int main() {
  int x, n, i;
  cin >> x >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    flag[a[i]] = true;
  }
  for (i = 0; i <= x; i++)
    if (!flag[x - i]) {
      cout << x - i;
      return 0;
    } else if (!flag[x + i]) {
      cout << x + i;
      return 0;
    }
  return 0;
}

Error: TLE

Fixed submission:

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

#pragma GCC optimize("O2")
typedef long long ll;
bool p[105];
int x, n;
int main() {
  ios::sync_with_stdio(false);
  cin >> x >> n;
  memset(p, 0, sizeof(p));
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    p[t] = 1;
  }
  int ans1, ans2, p1, p2;
  for (int i = x; i <= 100; i++) {
    if (!p[i]) {
      ans1 = i;
      p1 = abs(x - i);
      break;
    }
  }
  for (int i = x; i >= 0; i++) {
    if (!p[i]) {
      ans2 = i;
      p2 = abs(x - i);
    }
  }
  if (p1 < p2)
    cout << ans1 << endl;
  else if (p1 > p2)
    cout << ans2 << endl;
  else
    cout << ans2 << endl;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define debug(s) cout << s << endl
#define debug1(s, t) cout << s << " " << t << endl
#define debug2(s, t, u) cout << s << " " << t << " " << u << endl
#define printvec(v)                                                            \\
  for (auto &&i : v)                                                           \\
    cout << i << " ";                                                          \\
  cout << endl
#define rep(n, i) for (int i = 0; i < n; i++)

using namespace std;
using i64 = long long;

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> p(n);
  for (auto &&pi : p) {
    cin >> pi;
  }

  vector<vector<int>> list(1000);
  for (int y = 0; y <= 110; y++) {
    bool ok = true;
    for (auto &&pi : p) {
      ok &= pi != y;
    }
    if (ok) {
      list[abs(x - y)].push_back(y);
    }
  }

  for (auto &&li : list) {
    if (!li.empty()) {
      cout << li.front() << endl;
      return 0;
    }
  }
  cout << x << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define debug(s) cout << s << endl
#define debug1(s, t) cout << s << " " << t << endl
#define debug2(s, t, u) cout << s << " " << t << " " << u << endl
#define printvec(v)                                                            \\
  for (auto &&i : v)                                                           \\
    cout << i << " ";                                                          \\
  cout << endl
#define rep(n, i) for (int i = 0; i < n; i++)

using namespace std;
using i64 = long long;

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> p(n);
  for (auto &&pi : p) {
    cin >> pi;
  }

  vector<vector<int>> list(200);
  for (int y = -100; y <= 100; y++) {
    bool ok = true;
    for (int i = 0; i < n; i++) {
      ok &= p[i] != y;
    }
    if (ok) {
      list[abs(x - y)].push_back(y);
    }
  }

  for (auto &&li : list) {
    if (!li.empty()) {
      cout << li.front() << endl;
      return 0;
    }
  }

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

#include <iostream>

using namespace std;

bool CheckValue(int value, int p[], int n) {
  for (int i = 0; i < n; i++) {
    if (value == p[i])
      return false;
  }
  return true;
}

int main() {
  int x, n;
  int *p;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }

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

  int a = 0;
  int ans = 0;
  while (1) {
    if (CheckValue(x - a, p, n)) {
      ans = x - a;
      break;
    }
    if (CheckValue(x + a, p, n)) {
      ans = x + a;
      break;
    }
    a++;
  }
  cout << ans << endl;

  delete[] p;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

bool CheckValue(int value, int p[], int n) {
  for (int i = 0; i < n; i++) {
    if (value == p[i])
      return false;
    else
      true;
  }
}

int main() {
  int x, n;
  int *p;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }

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

  int a = 1;
  int ans = 0;
  while (1) {
    if (CheckValue(x - a, p, n)) {
      ans = x - a;
      break;
    }
    if (CheckValue(x + a, p, n)) {
      ans = x + a;
      break;
    }
    a++;
  }
  cout << ans << endl;

  delete[] p;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  int X, N;
  cin >> X >> N;

  vector<int> P(N);
  vector<int> Q(N);

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

  Q = P;

  if (N == 0) {
    cout << X << endl;
  } else {
    for (int i = 0; i < N; i++) {
      Q[i] = abs(Q[i] - X);
    }

    int M = *max_element(Q.begin(), Q.end());

    for (int i = 0; i < M + 1; i++) {

      auto F1 = find(P.begin(), P.end(), X - i);
      auto F2 = find(P.begin(), P.end(), X + i);

      if (F1 == P.end()) {
        X -= i;
        break;
      }

      else if (F2 == P.end()) {
        X += i;
        break;
      }

      if (i == M) {
        X -= (M + 1);
      }
    }

    cout << X << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {

  int X, N;
  cin >> X >> N;

  vector<int> P(N);
  vector<int> Q(N);

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

  Q = P;

  for (int i = 0; i < N; i++) {
    Q[i] = abs(Q[i] - X);
  }

  int M = *max_element(Q.begin(), Q.end());

  for (int i = 0; i < M + 1; i++) {

    auto F1 = find(P.begin(), P.end(), X - i);
    auto F2 = find(P.begin(), P.end(), X + i);

    if (F1 == P.end()) {
      X -= i;
      break;
    }

    else if (F2 == P.end()) {
      X += i;
      break;
    }

    if (i == M) {
      X -= (M + 1);
    }
  }

  cout << X << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  //
  int X, N;
  cin >> X >> N;

  if (N == 0) {
    cout << X << endl;
    exit(0);
  }

  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p.at(i);
  }
  sort(p.begin(), p.end());

  //
  vector<int> num;
  for (int i = 0; i <= 101; i++) {
    for (int j = 0; j < N; j++) {
      if (i == p.at(j)) {
        break;
      }
      if (j == N - 1) {
        num.push_back(i);
      }
    }
  }

  //
  vector<int> d(num.size());
  for (int i = 0; i < num.size(); i++) {
    d.at(i) = abs(X - num.at(i));
  }
  sort(d.begin(), d.end());

  //
  for (int i = 0; i < d.size(); i++) {
    if (num.at(i) == X - d.at(0)) {
      cout << X - d.at(0) << endl;
      exit(0);
    }
  }
  cout << X + d.at(0) << endl;
}

Error: 0

Fixed submission:

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

int main() {
  //
  int X, N;
  cin >> X >> N;
  /*
  if (N==0) {
      cout << X << endl;
      exit(0);
  }
  */
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p.at(i);
  }
  sort(p.begin(), p.end());

  //
  vector<int> num;
  for (int i = 0; i <= 101; i++) {
    for (int j = 0; j < N; j++) {
      if (i == p.at(j)) {
        break;
      }
      if (j == N - 1) {
        num.push_back(i);
      }
    }
  }

  //
  vector<int> d(num.size());
  for (int i = 0; i < num.size(); i++) {
    d.at(i) = abs(X - num.at(i));
  }
  sort(d.begin(), d.end());

  //
  for (int i = 0; i < d.size(); i++) {
    if (num.at(i) == X - d.at(0)) {
      cout << X - d.at(0) << endl;
      exit(0);
    }
  }
  cout << X + d.at(0) << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
  ll n, x, temp;
  cin >> x >> n;
  ll a[400] = {0};

  for (int i = 0; i < n; i++) {
    cin >> temp;
    a[temp] = 1;
  }
  for (int i = 0;; i++) {
    if (a[x - i] == 0) {
      cout << x - i << endl;
      break;
    }
    if (a[x + i] == 0) {
      cout << x + i << endl;
      break;
    }
  }

  return 0;
}


Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
  ll n, x;
  cin >> x >> n;
  ll a[102];
  for (int i = 0; i < 202; i++)
    a[i] = 0;
  if (n == 0)
    cout << 0;
  else {
    for (int i = 0; i < n; i++) {
      int temp;
      cin >> temp;
      a[temp] = 1;
    }
    for (int i = 0;; i++) {
      if (a[x - i] == 0) {
        cout << x - i << endl;
        return 0;
      }
      if (a[x + i] == 0) {
        cout << x + i << endl;
        return 0;
      }
    }
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> d(102);
  rep(i, n) {
    int p;
    cin >> p;
    d[p] = 1;
  }
  pair<int, int> ans(99999, -1);
  for (int i = 0; i <= 101; ++i) {
    if (d[i] == 1)
      continue;
    int dif = abs(x - i);
    ans = min(ans, pair<int, int>(dif, i));
  }
  cout << ans.second << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> p(n);
  rep(i, n) { cin >> p[i]; }

  sort(p.begin(), p.end());
  int ans = p[0];

  rep(i, n) {
    if (abs(x - ans) > abs(x - p[i])) {
      ans = p[i];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int x, n;
  cin >> x >> n;
  map<int, bool> mp;
  for (int i = 0, y; i < n; ++i) {
    cin >> y;
    mp[y] = true;
  }

  int ans = 101;

  for (int i = 101; i >= -1; --i) {
    if (!mp[i] && abs(x - i) <= abs(x - ans)) {
      ans = i;
    }
  }

  cout << ans << "\\n";
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, n;
  cin >> x >> n;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    v.push_back(num);
  }
  int min = 1000, max = *max_element(v.begin(), v.end());
  for (int j = -1; j <= max + 1; j++) {
    if (find(v.begin(), v.end(), j) == v.end()) {
      if (abs(x - j) < min) {
        min = j;
      }
    }
  }
  cout << min;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;

int main() {
  ll x, n;
  cin >> x >> n;
  ll bucket[101] = {0};

  rep(i, n) {
    ll tmp;
    cin >> tmp;
    bucket[tmp] = 1;
  }

  if (bucket[x] == 0) {
    cout << x << endl;
    return 0;
  }

  int left = x;
  int right = x;
  while (true) {
    left++;
    right--;
    if (bucket[right] != 1 && bucket[left] != 1) {
      cout << right << endl;
      return 0;
    } else if (bucket[right] != 1) {
      cout << right << endl;
      return 0;
    } else if (bucket[left] != 1) {
      cout << left << endl;
      return 0;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;

int main() {
  ll x, n;
  cin >> x >> n;
  ll bucket[101] = {0};

  rep(i, n) {
    ll tmp;
    cin >> tmp;
    bucket[tmp] = 1;
  }

  if (bucket[x] == 0) {
    cout << x << endl;
    return 0;
  }

  int left = x + 1;
  int right = x - 1;
  while (true) {
    if ((0 < right && bucket[right] == 0) &&
        (left < 101 && bucket[left] == 0)) {
      cout << right << endl;
      return 0;
    } else if (0 < right && bucket[right] == 0) {
      cout << right << endl;
      return 0;
    } else if (left < 101 && bucket[left] == 0) {
      cout << left << endl;
      return 0;
    } else {
      left++;
      right--;
    }
  }

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


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> mark(102, 0);
  int in;
  rep(i, n) {
    cin >> in;
    mark.at(in) = 1;
  }
  int diff = 0;
  while (true) {
    if (mark.at(x - diff) == 0) {
      cout << x - diff << endl;
      return 0;
    } else if (mark.at(x + diff) == 0) {
      cout << x + diff << endl;
      return 0;
    }
    diff++;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int x, n;
  cin >> x >> n;

  // nが0なら必ずx自身が答え
  if (n == 0) {
    cout << x << endl;
  } else {
    // 1から100までが入ったベクトルを作る
    vector<int> vec(100);
    rep(i, 100) { vec.at(i) = i; }

    // vecのうちpにある要素を削除する
    int p;
    for (int i = 0; i < n; i++) {
      cin >> p;
      for (double j = 0; j < vec.size(); j++) {
        if (vec.at(j) == p) {
          vec.erase(vec.begin() + j + 1);
        }
      }
    }
    int diff = 0;
    while (true) {
      // vecの各要素にイテレート
      for (double i = 0; i < vec.size(); i++) {
        // もしxとの差がdiffのものがあればそれを出力
        // 小さい順に数えて、差がdiffのものが出た時点でbreakする
        if (abs(vec.at(i) - x) == diff) {
          cout << vec.at(i) << endl;
          goto end;
        }
        // 差がdiffのものがなかったらdiffを+1する
        diff += 1;
      }
    }
  }
end:
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ld = long double;

int main() {
  int x, n;
  cin >> x >> n;

  int p;
  vector<pair<int, int>> ans(102);
  for (int i = 0; i < 102; i++) {
    ans[i].first = i;
    ans[i].second = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> p;
    ans[p].second = 1;
  }

  int res = INT_MAX;
  int tmp;
  for (int i = 0; i < 102; i++) {
    if (ans[i].second == 1)
      continue;

    if (res > abs(x - ans[i].first)) {
      res = abs(x - ans[i].first);
      tmp = ans[i].first;
    }
  }

  cout << tmp << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ld = long double;

int main() {
  int x, n;
  cin >> x >> n;

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

  int res = x;
  for (int i = -1;; i++) {
    if (find(p.begin(), p.end(), res) == p.end())
      break;
    res = x + i;
    if (find(p.begin(), p.end(), res) == p.end())
      break;
    i *= -1;
    res = x + i;
    if (find(p.begin(), p.end(), res) == p.end())
      break;
  }

  cout << res << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0)
    cout << x << endl;
  else if (n == 100) {
    if (x <= 50)
      cout << "0" << endl;
    else
      cout << "101" << endl;
  } else {
    vector<int> p(n);
    vector<int> vec(100, 1);
    for (int i = 0; i < n; i++) {
      cin >> p.at(i);
      vec.at(p.at(i) - 1) = 0;
    }
    // 最もxに近いpの値を保持
    int ans = 0;
    // xとpの差の最小値を保持
    int p_min = 100;
    for (int i = 0; i < 100; i++) {
      if (vec.at(i) == 1) {
        if (p_min > abs((i + 1) - x)) {
          p_min = abs((i + 1) - x);
          ans = i + 1;
        }
        if (p_min == abs((i + 1) - x))
          ans = min(ans, i + 1);
      }
    }
    if (p_min >= x)
      ans = 0;
    else if (p_min > (101 - x))
      ans = 101;
    cout << ans << endl;
  }
}


Error: WA

Fixed submission:

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

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0)
    cout << x << endl;

  else {
    vector<int> p(n);
    vector<int> vec(100, 1);
    for (int i = 0; i < n; i++) {
      cin >> p.at(i);
      vec.at(p.at(i)) = 0;
    }
    // 最もxに近いpの値を保持
    int ans = 0;
    // xとpの差の最小値を保持
    int p_min = 100;
    for (int i = 0; i < 100; i++) {
      if (vec.at(i) == 1) {
        if (p_min > abs((i + 1) - x)) {
          p_min = abs((i + 1) - x);
          ans = i + 1;
        }
        if (p_min == abs((i + 1) - x))
          ans = min(ans, i + 1);
      }
    }
    cout << ans << endl;
  }
}