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

#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int z[24][60], n, a, b, k = 0;
  for (int i = 0; i < 24; i++)
    for (int j = 0; j < 60; j++)
      z[i][j] = 0;
  for (int w = 0; w < 2; w++) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      z[a][b] = 1;
    }
  }
  for (int i = 0; i < 24; i++) {
    for (int j = 0; j < 60; j++) {
      if (z[i][j] == 1) {
        if (k)
          cout << " ";
        if (j < 10) {
          cout << i << ":"
               << "0" << j;
          k++;
        } else {
          cout << i << ":" << j;
          k++;
        }
      }
    }
  }
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int a[100][2], i, j, n, t, b, c;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i][0] >> a[i][1];
  cin >> n;
  j = i;
  for (i = j; i < j + n; i++)
    cin >> a[i][0] >> a[i][1];
  n = i;
  for (i = 0; i < n; i++) {
    for (j = n - 1; j > i; j--) {
      if (a[j][0] < a[j - 1][0]) {
        t = a[j][0];
        a[j][0] = a[j - 1][0];
        a[j - 1][0] = t;
        t = a[j][1];
        a[j][1] = a[j - 1][1];
        a[j - 1][1] = t;
      }
    }
  }
  for (i = 0; i < n; i++) {
    for (j = n - 1; j > i; j--) {
      if (a[j][0] == a[j - 1][0] && a[j][1] < a[j - 1][1]) {
        t = a[j][1];
        a[j][1] = a[j - 1][1];
        a[j - 1][1] = t;
      }
    }
  }
  for (i = 0; i < n; i++) {
    if (b == a[i][0] && c == a[i][1])
      continue;
    if (i)
      cout << " ";
    if (a[i][1] > 9)
      cout << a[i][0] << ":" << a[i][1];
    else
      cout << a[i][0] << ":0" << a[i][1];
    b = a[i][0];
    c = a[i][1];
  }
  cout << endl;
  return 0;
}
Problem p00293 (C++) — buggy submission, verdict: Runtime Error

#include <bitset>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int a[1000], b[1000], n, m, i, min1 = 100, min2 = 100, num, sa = 0, sa1 = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  cin >> m;
  for (int j = i; j < m + i; j++) {
    cin >> a[j] >> b[j];
  }
  for (int i = 0; i < m + n; i++) {
    for (int j = 0; j < m + n; j++) {
      if (i == j) {
        continue;
      }
      if (a[i] == a[j] && b[i] == b[j]) {
        sa++;
      }
    }
  }
  sa1 = sa / 2;
  for (int j = 0; j < n + m - sa1; j++) {
    for (int l = 0; l < n + m; l++) {
      if (min1 == a[l]) {
        if (min2 > b[l]) {
          min1 = a[l];
          min2 = b[l];
          num = l;
        }
      } else if (min1 > a[l]) {
        min1 = a[l];
        min2 = b[l];
        num = l;
      }
    }
    cout << min1 << ":";
    printf("%02d", min2);
    if (j == n + m - sa1 - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
    for (int k = 0; k < n + m; k++) {
      if (min1 == a[k] && min2 == b[k]) {
        a[k] = 100;
        b[k] = 100;
      }
    }
    a[num] = 1000;
    b[num] = 1000;
    min1 = 100;
    min2 = 100;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bitset>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int a[100], b[100], n, m, i, min1 = 100, min2 = 100, num, sa = 0, sa1 = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  cin >> m;
  for (int j = i; j < m + i; j++) {
    cin >> a[j] >> b[j];
  }
  for (int i = 0; i < m + n; i++) {
    for (int j = 0; j < m + n; j++) {
      if (i == j) {
        continue;
      }
      if (a[i] == a[j] && b[i] == b[j]) {
        sa++;
      }
    }
  }
  sa1 = sa / 2;
  for (int j = 0; j < n + m - sa1; j++) {
    for (int l = 0; l < n + m; l++) {
      if (min1 == a[l]) {
        if (min2 > b[l]) {
          min1 = a[l];
          min2 = b[l];
          num = l;
        }
      } else if (min1 > a[l]) {
        min1 = a[l];
        min2 = b[l];
        num = l;
      }
    }
    cout << min1 << ":";
    printf("%02d", min2);
    if (j == n + m - sa1 - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
    for (int k = 0; k < n + m; k++) {
      if (min1 == a[k] && min2 == b[k]) {
        a[k] = 100;
        b[k] = 100;
      }
    }
    a[num] = 1000;
    b[num] = 1000;
    min1 = 100;
    min2 = 100;
  }
  return 0;
}
Problem p00294 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int i, j, k, d[100005], n, m, s;
  cin >> n >> m >> s;
  for (i = 0; i < m; i++)
    cin >> d[i];
  d[m] = s;
  int ans, left, right;
  ans = 21000000;
  sort(d, d + m + 1);
  d[m + 1] = d[0];
  for (i = 0; i <= m; i++) {
    left = d[i] - s;
    if (left < 0)
      left += n;
    right = s - d[i + 1];
    if (right < 0)
      right += n;
    ans = min(ans, right * 2 + left);
    ans = min(ans, left * 2 + right);
  }
  cout << ans * 100 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int i, j, k, n, m, d[10000], s, left[10000], right[10000], x,
      flg[100000] = {0}, ans, rr, ll;
  cin >> n >> m >> s;
  for (i = 0; i < m; i++)
    cin >> d[i], flg[d[i]] = 1;
  ans = x = 0;
  for (i = s; i < n; i++) {
    if (flg[i] == 1) {
      ll = i;
      break;
    }
    if (i + 1 == n)
      i = -1;
  }
  for (i = s; i >= 0; i--) {
    if (flg[i] == 1) {
      rr = i;
      break;
    }
    if (i - 1 < 0)
      i = n;
  }
  for (i = s; i < n; i++) {
    if (rr == i)
      break;
    if (i + 1 == n)
      i = -1;
    ans++;
  }
  for (i = s; i >= 0; i--) {
    if (ll == i)
      break;
    if (i - 1 < 0)
      i = n;
    x++;
  }
  sort(d, d + m);
  for (i = 0; i < m - 1; i++)
    right[d[i]] = d[i + 1];
  right[d[m - 1]] = d[0];
  for (i = m - 1; i >= 1; i--)
    left[d[i]] = d[i - 1];
  left[d[0]] = d[m - 1];
  right[s] = rr, left[s] = ll;
  ans = min(ans, x);
  int ff, a, b, c;
  for (i = s + 1; i < n; i++) {
    if (flg[i] == 1 && right[s] != i) {
      a = i - s;
      b = s - right[i];
      if (a < 0 && b > 0)
        a += n;
      else if (a > 0 && b < 0)
        b += n;
      a *= 2;
      ans = min(ans, a + b);
    } else if (d[m - 1] == i)
      break;
    if (i + 1 == n)
      i = -1;
  }
  for (i = s - 1; i >= 0; i--) {
    if (flg[i] == 1 && left[s] != i) {
      a = i - s;
      b = left[i] - s;
      if (a < 0 && b > 0)
        a += n;
      else if (a > 0 && b < 0)
        b += n;
      a *= 2;
      ans = min(ans, a + b);
    } else if (d[0] == i)
      break;
    if (i - 1 < 0)
      i = n;
  }
  cout << ans * 100 << endl;
}
Problem p00294 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int N, M, p, ans = 100000;
  int lis[10001] = {0}, lismap[100001] = {0};
  cin >> N >> M >> p;
  for (int i = 0; i < M; i++) {
    int a;
    cin >> a;
    lis[i] = a;
    lismap[a] = 1;
  }
  sort(lis, lis + M);
  int che = 0;
  for (int i = 0;; i++) {
    if (lismap[(p + i) % N] == 1)
      che++;
    if (che == M) {
      ans = min(ans, i);
      break;
    }
  }
  che = 0;
  for (int i = 0;; i++) {
    if (lismap[(N + (p - i)) % N] == 1)
      che++;
    if (che == M) {
      ans = min(ans, i);
      break;
    }
  }
  for (int i = 0; i < M; i++) {
    int karians1 = 0, karians2 = 0;
    int a = lis[i] - p;
    if (a < 0)
      a += N;
    karians1 += a * 2;
    karians2 += a;
    if (i == M - 1) {
      int b = p - lis[0];
      if (b < 0)
        b += N;
      karians1 += b;
      karians2 += b * 2;
    } else {
      int b = p - lis[i + 1];
      if (b < 0)
        b += N;
      karians1 += b;
      karians2 += b * 2;
    }
    ans = min(ans, min(karians1, karians2));
  }
  cout << ans * 100 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int N, M, p, ans = 100000;
  int lis[10001] = {0}, lismap[100001] = {0};
  cin >> N >> M >> p;
  for (int i = 0; i < M; i++) {
    int a;
    cin >> a;
    lis[i] = a;
    lismap[a] = 1;
  }
  sort(lis, lis + M);
  int che = 0;
  for (int i = 0;; i++) {
    if (lismap[(p + i) % N] == 1)
      che++;
    if (che == M) {
      ans = min(ans, i);
      break;
    }
  }
  che = 0;
  for (int i = 0;; i++) {
    if (lismap[N + (p - i) % N] == 1)
      che++;
    if (che == M) {
      ans = min(ans, i);
      break;
    }
  }
  for (int i = 0; i < M; i++) {
    int karians1 = 0, karians2 = 0;
    int a = lis[i] - p;
    if (a < 0)
      a += N;
    karians1 += a * 2;
    karians2 += a;
    if (i == M - 1) {
      int b = p - lis[0];
      if (b < 0)
        b += N;
      karians1 += b;
      karians2 += b * 2;
    } else {
      int b = p - lis[i + 1];
      if (b < 0)
        b += N;
      karians1 += b;
      karians2 += b * 2;
    }
    ans = min(ans, min(karians1, karians2));
  }
  cout << ans * 100 << endl;
  return 0;
}
Problem p00294 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repp(i, k, n) for (int i = k; i < (int)n; i++)
#define PB(a) push_back(a)
using namespace std;
typedef vector<int> vi;
int debu(string a) { cout << a << endl; }
int debu() { cout << "ok" << endl; }
int debu(int a) { cout << a << endl; }
int in() {
  int x;
  cin >> x;
  return x;
}

int n, m, p, h;
vi a;
int main() {
  cin >> n >> m >> p;
  rep(i, m) { a.PB(in()); }
  sort(a.begin(), a.end());
  rep(i, m) { a.push_back(a[i] + n); }
  rep(i, m) { a.push_back(a[i] + n + n); }
  rep(i, a.size() - 1) {
    if (a[i] < p + n && p + n < a[i + 1]) {
      h = i;
      a.insert(a.begin() + h + 1, p + n);
      break;
    }
  }
  h++;

  int ans = 999999999;

  ans = min(ans, min((a[h + m] - a[h]) * 100, (a[h] - a[h - m]) * 100));

  rep(i, m) {
    ans = min(ans, ((a[h + i] - a[h]) * 2 + a[h] - a[h - (m - i)]) * 100);
    ans = min(ans, ((a[h] - a[h - i]) * 2 + a[h + (m - i)] - a[h]) * 100);
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repp(i, k, n) for (int i = k; i < (int)n; i++)
#define PB(a) push_back(a)
using namespace std;
typedef vector<int> vi;
int debu(string a) { cout << a << endl; }
int debu() { cout << "ok" << endl; }
int debu(int a) { cout << a << endl; }
int in() {
  int x;
  cin >> x;
  return x;
}
int n, m, p;
vi a;
int h;
int memo[10000][10000][2];
int hoge(int l, int r, int j) {
  if (j == l) {
    if (memo[l][r][0] != -1)
      return memo[l][r][0];
  } else {
    if (memo[l][r][1] != -1)
      return memo[l][r][1];
  }
  if (r - l >= m)
    return 0;
  int ans = 0;
  ans = min(hoge(l - 1, r, l - 1) + (a[j] - a[l - 1]) * 100,
            hoge(l, r + 1, r + 1) + (a[r + 1] - a[j]) * 100);

  if (j == l) {
    memo[l][r][0] = ans;
  } else {
    memo[l][r][1] = ans;
  }
  return ans;
}

int main() {
  memset(memo, -1, sizeof(memo));
  cin >> n >> m >> p;
  rep(i, m) a.PB(in());
  sort(a.begin(), a.end());
  rep(i, m) a.PB(a[i] + n);
  rep(i, m) a.PB(a[i] + 2 * n);
  rep(i, m) a.PB(a[i] + 3 * n);
  rep(i, m) a.PB(a[i] + 4 * n);
  rep(i, a.size() - 1) {
    if (a[i] < 2 * n + p && a[i + 1] > 2 * n + p) {
      h = i;
      break;
    }
  }
  a.insert(a.begin() + h + 1, 2 * n + p);
  cout << hoge(h + 1, h + 1, h + 1) << endl;
}
Problem p00294 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
//??¢???????§???????,?§??????°????¨???????????????????,???????????¨?????¨??????????§?
int main() {

  int n, m, p, min, mintmp;

  cin >> n >> m >> p;
  vector<int> vec(m);
  for (int i = 0; i < m; i++) {
    cin >> vec[i];
  }
  sort(vec.begin(), vec.end());

  bool flag = true;
  int i = 0;
  while (flag) {
    flag = false;

    if (vec[i] < p) {
      vec.push_back(vec[i] + n);
      i++;
      flag = true;
    }
  }

  vec.erase(vec.begin(), vec.begin() + i);

  reverse(vec.begin(), vec.end());

  int start, finish, startdif, finishdif;
  min = 1000000;

  i = 0;

  while (1) {
    if (i == vec.size() - 1) {
      mintmp = p + n - vec[i];
      if (min > mintmp) {
        min = mintmp;
      }
      break;
    }

    start = vec[i];
    finish = vec[i + 1];

    startdif = 2 * (p + n - start);
    finishdif = finish - p;

    mintmp = startdif + finishdif;

    if (min > mintmp) {
      min = mintmp;
    }

    i++;
  }

  reverse(vec.begin(), vec.end());

  i = 0;

  while (1) {
    if (i == vec.size() - 1) {
      mintmp = vec[i] - p;
      if (min > mintmp) {
        min = mintmp;
      }
      break;
    }

    start = vec[i];
    finish = vec[i + 1];

    startdif = 2 * (start - p);
    finishdif = n + p - finish;

    mintmp = startdif + finishdif;

    if (min > mintmp) {
      min = mintmp;
    }

    i++;
  }

  cout << min * 100 << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  int n, m, p, tmp, start;

  cin >> n >> m >> p;

  vector<char> doublevec(2 * n + p);
  vector<int> ans(2 + 2 * (n), 200000000);
  for (int i = 0; i < m; i++) {
    cin >> tmp;
    doublevec[tmp] = true;
    doublevec[tmp + n] = true;
  }

  for (int i = 0; i < p; i++) {
    doublevec[i + 2 * n] = doublevec[i];
  }

  doublevec.erase(doublevec.begin(), doublevec.begin() + p);

  start = n;

  int lastspot = 0;

  for (int i = 0; i < n; i++) {
    if (doublevec[i]) {
      lastspot = i;
    }
  }

  ans[0] = lastspot;

  for (int i = start; i > 0; i--) {
    if (doublevec[i]) {
      lastspot = i;
    }
  }

  ans[1] = start - lastspot;

  for (int i = start; i < doublevec.size(); i++) {
    for (int j = i; j > i - n; j--) {
      if (doublevec[j]) {
        lastspot = j;
      }
    }
    ans[2 + i - start] = (2 * (i - start) + start - lastspot);
    // cout << start << " " << i << " " << lastspot<<endl;
  }

  for (int i = start; i > 0; i--) {
    for (int j = i; j < i + n; j++) {
      if (doublevec[j]) {
        lastspot = j;
      }
    }
    ans[2 + doublevec.size() / 2 - (i - start)] =
        (2 * (start - i) + lastspot - start);
    //  cout << start << " " << i << " " << lastspot<<endl;
  }
  /*for(int i=0;i<ans.size();i++){
    cout << ans[i] << endl;
  }*/
  sort(ans.begin(), ans.end());

  if (ans[0] == 200000000) {
    ans[0] = 0;
  }

  cout << ans[0] * 100 << endl;

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m, p, q, l, r, a, b, ans = 100001;
  vector<int> d;
  cin >> n >> m >> p;
  for (int i = 0; i < m; i++) {
    cin >> q;
    d.push_back(q);
  }
  d.push_back(p);
  sort(d.begin(), d.end());
  for (int i = 0; i <= m; i++) {
    q = d[i];
    a = (i - 1 + m + 1) % (m + 1);
    b = (i + 1 + m + 1) % (m + 1);
    l = (p - q + n) % n * 2 + (d[a] - p + n) % n;
    r = (q - p + n) % n * 2 + (p - d[b] + n) % n;
    ans = min(ans, min(l, r));
  }
  cout << ans * 100 << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <stdexcept>
#include <vector>
using namespace std;

int main() {
  int n, m, p, s, l, r, cur, nex, ans;
  vector<int> d;
  cin >> n >> m >> p;
  for (int i = 0; i < m; i++) {
    cin >> s;
    d.push_back(s - p);
  }
  sort(d.begin(), d.end());
  vector<int>::iterator it = lower_bound(d.begin(), d.end(), 0);
  if (it == d.begin()) {
    ans = min(n - d[0], d[d.size() - 1]);
  } else if (it == d.end()) {
    ans = min(-d[0], n + d[d.size() - 1]);
  } else {
    ans = min(n + (*(it - 1)), n - (*(it + 1)));
  }

  if (m == 2)
    throw invalid_argument("");
  if (m > 1) {
    for (int i = 0; i < m; i++) {
      cur = d[i];
      nex = d[(i + 1) % m];
      if ((cur < 0 && nex > 0) || (i == m - 1 && (cur < 0 || nex > 0))) {
        if (m > 2) {
          continue;
        }
        if ((cur < 0 && nex > 0)) {
          l = -cur, r = nex;
        } else if (cur < 0) {
          l = -cur, r = nex + n - p;
        } else if (nex > 0) {
          l = p + n - cur, r = nex;
        }
      }
      if (cur < 0 && nex < 0) {
        l = -nex, r = n + cur;
      }
      if (cur > 0 && nex > 0) {
        l = n - nex, r = cur;
      }
      if (cur > 0 && nex < 0) {
        l = -nex, r = cur;
      }
      ans = min(ans, min(2 * l + r, 2 * r + l));
    }
  }
  cout << ans * 100 << endl;
}
Problem p00294 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, M, p;
  cin >> N >> M >> p;

  int d[100001];
  d[0] = p;

  for (int i = 1; i <= M; i++)
    cin >> d[i];

  sort(d, d + M + 1);

  int c_min = N - 1;

  for (int i = 0; i < M + 1; i++) {
    int q = d[i];
    int r1 = d[(i - 1 + M + 1) % (M + 1)];
    int r2 = d[(i + 1) % (M + 1)];

    int c1 = ((p - q + N) % N) * 2 + (r1 - p + N) % N;
    int c2 = ((q - p + N) % N) * 2 + (p - r2 + N) % N;

    c_min = min(c_min, min(c1, c2));
  }

  cout << c_min * 100 << endl;

  return 0;
}


Error: 0

Fixed submission:

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

int count = 0;

int Route(int *D, int m, int start, int n) {
  int route;

  int min = n;
  int max = 0;
  for (int i = 0; i < m; i++) {
    if (D[i] < n / 2 && max < D[i])
      max = D[i];
    if (D[i] >= n / 2 && min > D[i])
      min = D[i];
  }

  int r = start;
  int l = start;
  int route1 = 0;
  int route2 = 0;

  stack<int> s1;
  s1.push(r);
  while (r < n / 2 || r + 1 > start) {
    if (r + 1 == n) {
      n = 0;
      s1.push(r);
    } else {
      r++;
      s1.push(r);
    }
    if (s1.top() == max) {
      route1 = s1.size() - 1;
      break;
    }
  }

  stack<int> s2;
  s2.push(l);
  while (l >= n / 2 || l - 1 < start) {
    if (l - 1 == -1) {
      l = n - 1;
      s2.push(l);
    } else {
      l--;
      s2.push(l);
    }
    if (s2.top() == min) {
      route2 = s2.size() - 1;
      break;
    }
  }

  if (route1 < route2)
    route = 2 * route1 + route2;
  else
    route = 2 * route2 + route1;

  if (D[m - 1] < route)
    route = D[m - 1];

  return route;
}

int main() {
  int N, M;
  cin >> N >> M;

  int p;
  cin >> p;

  int d[100000];
  for (int i = 0; i < M; i++)
    cin >> d[i];

  sort(d, d + M);

  cout << Route(d, M, p, N) * 100 << endl;

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

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

using namespace std;

int N;
int M;
int p;
int d[10010];
int ans = 0;

int main() {
  cin >> N >> M >> p;

  for (int i = 0; i < M; i++) {
    cin >> d[i];

    d[i] -= p;

    if (d[i] < 0)
      d[i] += N;
  }

  sort(&d[0], &d[M]);

  ans = d[M - 1] * 100;

  ans = min(ans, ((N - d[0]) * 100));

  for (int i = 0; i < M - 1; i++) {
    int c1 = 2 * (d[i] * 100);
    int c2 = ((N - d[i + 1]) * 100);

    ans = min(ans, c1 + c2);
  }

  for (int i = M - 1; i > 0; i--) {
    int c1 = 2 * ((N - d[i]) * 100);
    int c2 = (d[i - 1] * 100);

    ans = min(ans, c1 + c2);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

int N;
int M;
int p;
int d[10010];
int ans = 0;

int main() {
  cin >> N >> M >> p;

  for (int i = 0; i < M; i++) {
    cin >> d[i];

    d[i] -= p;

    if (d[i] < 0)
      d[i] += N;
  }

  sort(&d[0], &d[M]);

  ans = d[M - 1] * 100;

  ans = min(ans, (N - d[0] * 100));

  for (int i = 0; i < M - 1; i++) {
    int c1 = 2 * (d[i] * 100);
    int c2 = (N - d[i + 1] * 100);

    ans = min(ans, c1 + c2);
  }

  for (int i = M - 1; i > 0; i++) {
    int c1 = 2 * (d[i] * 100);
    int c2 = (d[i - 1] * 100);

    ans = min(ans, c1 + c2);
  }

  cout << ans << endl;

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

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

int L[4][5] = {
    {1, 4, 7, 10, 16}, {3, 6, 9, 12, 13}, {1, 2, 3, 15, 19}, {7, 8, 9, 10, 13}};

int R[4][5] = {{24, 27, 30, 21, 18},
               {22, 25, 28, 19, 15},
               {28, 29, 30, 16, 21},
               {22, 23, 24, 12, 18}};

struct Cube {
  int cube[31], s;

  Cube() {}

  void rot(int t) {
    for (int i = 0; i < 5; ++i)
      swap(cube[L[t][i]], cube[R[t][i]]);
  }

  bool fail(int a, int b) {
    for (int i = a + 1; i <= b; ++i)
      if (cube[a] != cube[i])
        return 1;
    return 0;
  }

  bool solved() {
    if (fail(1, 9))
      return 0;
    if (fail(10, 12))
      return 0;
    if (fail(13, 15))
      return 0;
    if (fail(16, 18))
      return 0;
    if (fail(19, 21))
      return 0;
    if (fail(22, 30))
      return 0;
    return 1;
  }
};

int main() {
  int N;
  cin >> N;

  for (int i = 0; i < N; ++i) {
    Cube ini;
    for (int i = 1; i <= 30; ++i)
      cin >> ini.cube[i];
    ini.s = 0;

    queue<Cube> q;
    q.push(ini);

    while (!q.empty()) {
      Cube c = q.front();
      q.pop();

      if (c.solved()) {
        cout << c.s << endl;
        break;
      }

      ++c.s;

      for (int i = 0; i < 4; ++i) {
        c.rot(i);
        q.push(c);
        c.rot(i);
      }
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

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

int L[4][5] = {
    {1, 4, 7, 10, 16}, {3, 6, 9, 12, 13}, {1, 2, 3, 15, 19}, {7, 8, 9, 10, 13}};

int R[4][5] = {{24, 27, 30, 21, 16},
               {22, 25, 28, 19, 15},
               {28, 29, 30, 16, 21},
               {22, 23, 24, 12, 18}};

struct Cube {
  int cube[31], s;

  Cube() {}

  void rot(int t) {
    for (int i = 0; i < 5; ++i)
      swap(cube[L[t][i]], cube[R[t][i]]);
  }

  bool fail(int a, int b) {
    for (int i = a + 1; i <= b; ++i)
      if (cube[a] != cube[i])
        return 1;
    return 0;
  }

  bool solved() {
    if (fail(1, 9))
      return 0;
    if (fail(10, 12))
      return 0;
    if (fail(13, 15))
      return 0;
    if (fail(16, 18))
      return 0;
    if (fail(19, 21))
      return 0;
    if (fail(22, 30))
      return 0;
    return 1;
  }
};

int main() {
  int N;
  cin >> N;

  for (int i = 0; i < N; ++i) {
    Cube ini;
    for (int i = 1; i <= 30; ++i)
      cin >> ini.cube[i];
    ini.s = 0;

    queue<Cube> q;
    q.push(ini);

    while (!q.empty()) {
      Cube c = q.front();
      q.pop();

      if (c.solved()) {
        cout << c.s << endl;
        break;
      }

      ++c.s;

      for (int i = 0; i < 4; ++i) {
        c.rot(i);
        q.push(c);
        c.rot(i);
      }
    }
  }

  return 0;
}