Problem p02641 (C++) — buggy submission, verdict: Runtime Error



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

/// File input & output /////
#define fRead(x) freopen("x.txt", "r", stdin)
#define fWrite(x) freopen("x.txt", "w", stdout)

#define mt make_tuple
#define ld long double
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define INF 2e16
#define PI acos(-1.0)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<LL, LL>

#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define SQR(a) ((a) * (a))
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, b) for (int i = 0; i < b; i++)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ABS(x) ((x) < 0 ? -(x) : (x))

#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())

#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(nullptr);

///..................Sort by second element in a pair in decending
///order............. ///
bool SortbySecDesc(const pair<long long, long long> &a,
                   const pair<long long, long long> &b) {
  return a.second > b.second;
}

///.........................first element of pair in descending
///order..........................//////
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.first);
}

///.............................................................TEMPLATE................................................./////

// long long  Prime[3000000],nPrime;
// long long  mark[10000002];
//
// void seive(long long int n)
//{
//	long long int i,j,limit=sqrt(n*1.0)+2;
//	mark[1]=1;
//	for(i=4; i<=n ;i+=2)
//	mark[i]=1;
//	Prime[nPrime++]=2;
//	for(i=3; i<=n; i+=2)
//	{
//		if(!mark[i])
//		{
//			Prime[nPrime++]=i;
//			if(i<=limit)
//			{
//				for(j=i*i; j<=n ;j+=i*2)
//				{
//					mark[j]=1;
//				}
//			}
//		}
//	}
// }

ll pos, pos1;

int main() {
  FASTIO;

  int x, n;

  cin >> x >> n;

  if (n == 0) {
    cout << x << endl;
    return 0;
  }

  ll a[102];

  ll b[102];

  // MEM(b,-1);

  // MEM(a,-1);

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

  for (int i = x;; i++) {

    int f = 0;

    for (int j = 0; j < n; j++) {
      if (a[j] == i) {
        f = 1;
        break;
      }
    }

    if (f == 0) {
      pos = i;
      break;
    }
  }

  for (int i = x;; i--) {

    int f = 0;

    for (int j = 0; j < n; j++) {
      if (a[j] == i) {
        f = 1;
        break;
      }
    }

    if (f == 0) {
      pos1 = i;
      break;
    }
  }

  if (abs(x - pos1) > abs(x - pos)) {
    cout << pos << endl;
  } else {
    cout << pos1 << endl;
  }

  return 0;
}


Error: 0

Fixed submission:



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

/// File input & output /////
#define fRead(x) freopen("x.txt", "r", stdin)
#define fWrite(x) freopen("x.txt", "w", stdout)

#define mt make_tuple
#define ld long double
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define INF 2e16
#define PI acos(-1.0)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<LL, LL>

#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define SQR(a) ((a) * (a))
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, b) for (int i = 0; i < b; i++)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ABS(x) ((x) < 0 ? -(x) : (x))

#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())

#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(nullptr);

///..................Sort by second element in a pair in decending
///order............. ///
bool SortbySecDesc(const pair<long long, long long> &a,
                   const pair<long long, long long> &b) {
  return a.second > b.second;
}

///.........................first element of pair in descending
///order..........................//////
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.first);
}

///.............................................................TEMPLATE................................................./////

// long long  Prime[3000000],nPrime;
// long long  mark[10000002];
//
// void seive(long long int n)
//{
//	long long int i,j,limit=sqrt(n*1.0)+2;
//	mark[1]=1;
//	for(i=4; i<=n ;i+=2)
//	mark[i]=1;
//	Prime[nPrime++]=2;
//	for(i=3; i<=n; i+=2)
//	{
//		if(!mark[i])
//		{
//			Prime[nPrime++]=i;
//			if(i<=limit)
//			{
//				for(j=i*i; j<=n ;j+=i*2)
//				{
//					mark[j]=1;
//				}
//			}
//		}
//	}
// }

int main() {
  FASTIO;

  int x, n;

  cin >> x >> n;

  if (n == 0) {
    cout << x << endl;
    return 0;
  }

  ll a[102];

  ll b[102];

  MEM(b, -1);

  // MEM(a,-1);

  for (int i = 0; i < n; i++) {
    cin >> n;

    b[n] = 1;

    a[i] = n;
  }

  sort(a, a + n);

  ll mini = a[0];

  vector<ll> v;

  for (int i = mini; i <= 100; i++) {
    if (b[i] == -1) {
      v.pb(i);

      // cout << i << endl;
    }
  }

  vector<pair<ll, ll>> v1;

  for (int i = 0; i < v.size(); i++) {
    v1.pb(mp(abs(v[i] - x), v[i]));

    // cout << abs(v[i]-x) << " " << v[i] << endl;
  }

  SORT(v1);

  cout << v1[0].second << endl;

  return 0;
}