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

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

int calc(int x, int n) {
  vector<int> digits;
  while (x > 0 || digits.size() < n) {
    digits.emplace_back(x % 10);
    x /= 10;
  }

  sort(all(digits));
  reverse(all(digits));
  int a = 0;
  for (auto d : digits) {
    a *= 10;
    a += d;
  }
  reverse(all(digits));
  int b = 0;
  for (auto d : digits) {
    b *= 10;
    b += d;
  }

  return a - b;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int x, n;
  while (cin >> x >> n, n) {
    set<int> st;
    vector<int> v;
    int i;
    for (i = 0; st.count(x) == 0; i++) {
      st.insert(x);
      v.emplace_back(x);
      x = calc(x, n);
    }
    int j;
    for (j = 0; v[j] != x; j++)
      ;
    cout << j << " " << x << " " << i - j << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

int calc(int x, int n) {
  set<int> digits;
  while (x > 0 || digits.size() < n) {
    digits.insert(x % 10);
    x /= 10;
  }

  string s = "";
  for (auto c : digits) {
    s += c;
  }
  int b = stoi(s);
  reverse(all(s));
  int a = stoi(s);

  return a - b;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int x, n;
  while (cin >> x >> n, n) {
    set<int> st;
    int i;
    for (i = 0; st.count(x) == 0; i++) {
      st.insert(x);
      x = calc(x, n);
    }
    cout << i << endl;
  }
}
Problem p00761 (C++) — buggy submission, verdict: Runtime Error

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

int a[30];
string _min, _max;
int buf, L;

void solve() {
  for (int i = 1; i < 21; ++i) {
    // L桁に整形
    ostringstream ss;
    ss << setw(L) << setfill('0') << a[i - 1];
    string s(ss.str());

    // 辞書順最大と最小を求める
    sort(s.begin(), s.end());
    _min = s;
    reverse(s.begin(), s.end());
    _max = s;

    // a_iを求める
    a[i] = stoi(_max) - stoi(_min);
    // cout << _max << " - " << _min << " = " << a[i] << endl;
    s = to_string(a[i]);

    // a_i = a_j を満たす最小のiを求める
    for (int j = 0; j < i; ++j) {
      if (a[j] == a[i]) {
        cout << j << ' ' << a[i] << ' ' << i - j << endl;
        return;
      }
    }
  }
}

int main() {
  while (cin >> a[0] >> L && a[0] && L)
    solve();
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  stoi


Fixed submission:

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

int a[30];
string _min, _max;
int buf, L;
ostringstream ss;

void solve() {
  for (int i = 1; i < 21; ++i) {
    // L桁に整形

    ss << setw(L) << setfill('0') << a[i - 1];
    string s(ss.str());

    // 辞書順最大と最小を求める
    sort(s.begin(), s.end());
    _min = s;
    reverse(s.begin(), s.end());
    _max = s;

    // a_iを求める
    a[i] = stoi(_max) - stoi(_min);
    // cout << _max << " - " << _min << " = " << a[i] << endl;
    s = to_string(a[i]);

    // a_i = a_j を満たす最小のiを求める
    for (int j = 0; j < i; ++j) {
      if (a[j] == a[i]) {
        cout << j << ' ' << a[i] << ' ' << i - j << endl;
        return;
      }
    }
  }
}

int main() {
  while (cin >> a[0] >> L)
    solve();
  return 0;
}
Problem p00763 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#define REP(i, a, n) for (int i = (a); i <= n; i++)
#define INF 100000000
using namespace std;

int N, M, C, S, G;
int D[21][101][101];
int P[51], Q[21][51], R[21][51];
int cost[101][101];

int main(void) {
  while (cin >> N >> M >> C >> S >> G, N) {
    REP(i, 1, C) REP(j, 1, N) REP(k, 1, N) D[i][j][k] = INF;
    REP(i, 1, C) REP(j, 1, N) D[i][j][j] = 0;
    REP(i, 1, M) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      D[c][x][y] = min(D[c][x][y], d);
      D[c][y][x] = min(D[c][x][y], d);
    }

    REP(c, 1, C) {
      REP(k, 1, N) REP(i, 1, N) REP(j, 1, N) {
        D[c][i][j] = min(D[c][i][j], D[c][i][k] + D[c][k][j]);
      }
    }

    REP(i, 1, C) cin >> P[i];
    REP(i, 1, C) {
      REP(j, 1, P[i] - 1) cin >> Q[i][j];
      REP(j, 1, P[i]) cin >> R[i][j];
    }
    REP(i, 1, N) REP(j, 1, N) cost[i][j] = INF;
    REP(i, 1, N) cost[i][i] = 0;
    REP(c, 1, C) {
      int k = 1;
      int d[2000001];
      d[0] = 0;
      REP(i, 1, 2000000) {
        if (k < P[c] && i > Q[c][k])
          k++;
        d[i] = d[i - 1] + R[c][k];
      }
      REP(i, 1, N) REP(j, 1, N) {
        if (D[c][i][j] >= INF)
          continue;
        cost[i][j] = min(cost[i][j], d[D[c][i][j]]);
      }
    }

    REP(k, 1, N) REP(i, 1, N) REP(j, 1, N) {
      cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
    }

    cout << (cost[S][G] >= INF ? -1 : cost[S][G]) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#define REP(i, a, n) for (int i = (a); i <= n; i++)
#define INF 100000000
using namespace std;

int N, M, C, S, G;
int D[21][101][101];
int P[51], Q[21][51], R[21][51];
int d[21][2000001];
int cost[101][101];

int main(void) {
  while (cin >> N >> M >> C >> S >> G, N) {
    REP(i, 1, C) REP(j, 1, N) REP(k, 1, N) D[i][j][k] = INF;
    REP(i, 1, C) REP(j, 1, N) D[i][j][j] = 0;
    REP(i, 1, M) {
      int x, y, d, c;
      cin >> x >> y >> d >> c;
      D[c][x][y] = D[c][y][x] = d;
    }

    REP(c, 1, C) {
      REP(k, 1, N) REP(i, 1, N) REP(j, 1, N) {
        D[c][i][j] = min(D[c][i][j], D[c][i][k] + D[c][k][j]);
      }
    }

    REP(i, 1, C) cin >> P[i];
    REP(i, 1, C) {
      REP(j, 1, P[i] - 1) cin >> Q[i][j];
      REP(j, 1, P[i]) cin >> R[i][j];
    }
    REP(i, 1, C) {
      int k = 1;
      d[i][0] = 0;
      REP(j, 1, 2000000) {
        if (k < P[i] && j > Q[i][k])
          k++;
        d[i][j] = d[i][j - 1] + R[i][k];
      }
    }
    REP(i, 1, N) REP(j, 1, N) {
      cost[i][j] = INF;
      REP(c, 1, C) {
        if (D[c][i][j] >= INF)
          continue;
        cost[i][j] = min(cost[i][j], d[c][D[c][i][j]]);
      }
    }

    REP(k, 1, N) REP(i, 1, N) REP(j, 1, N) {
      cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
    }

    cout << (cost[S][G] >= INF ? -1 : cost[S][G]) << endl;
  }

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

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

#define INF (1 << 28)
#define REP(i, n) for (int i = 0; i < n; i++)

int a[110][110], wf[30][110][110], p[30], q[30][100], r[30][100];
int n, m, c, s, g, x, y, d, id, tmp;

int main() {
  while (scanf("%d%d%d%d%d", &n, &m, &c, &s, &g), n) {
    REP(i, n) REP(j, n) {
      a[i][j] = INF;
      REP(k, c) wf[k][i][j] = INF;
    }

    REP(i, m) {
      scanf("%d%d%d%d", &x, &y, &d, &id);
      x--;
      y--;
      id--;
      wf[id][x][y] = min(wf[id][x][y], d);
      wf[id][y][x] = min(wf[id][y][x], d);
    }

    REP(l, c) {
      scanf("%d", &p[l]);
      REP(k, n)
      REP(i, n) REP(j, n) wf[l][i][j] =
          min(wf[l][i][j], wf[l][i][k] + wf[l][k][j]);
    }

    REP(k, c) {
      q[k][0] = 0;
      REP(i, p[k] - 1) scanf("%d", &q[k][i + 1]);
      q[k][p[k]] = INF;
      REP(i, p[k]) scanf("%d", &r[k][i]);

      REP(i, n) for (int j = i; j < n; j++) {
        if (wf[k][i][j] < INF) {
          tmp = 0;
          REP(l, p[k]) {
            if (wf[k][i][j] <= q[k][l + 1]) {
              tmp += r[k][l] * (wf[k][i][j] - q[k][l]);
              break;
            } else
              tmp += r[k][l] * (q[k][l + 1] - q[k][l]);
          }
          a[i][j] = a[j][i] = min(a[i][j], tmp);
        }
      }
    }

    REP(k, n) REP(i, n) REP(j, n) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);

    if (a[s - 1][g - 1] >= INF)
      printf("-1\\n");
    else
      printf("%d\\n", a[s - 1][g - 1]);
  }
}

Error: 0

Fixed submission:

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

#define INF (1 << 28)
#define REP(i, n) for (int i = 0; i < n; i++)

int a[110][110], wf[30][110][110], p[30], q[30][100], r[30][100];
int n, m, c, s, g, x, y, d, id, tmp;

int main() {
  while (scanf("%d%d%d%d%d", &n, &m, &c, &s, &g), n) {
    REP(i, n) REP(j, n) {
      a[i][j] = INF;
      REP(k, n) wf[k][i][j] = INF;
    }

    REP(i, m) {
      scanf("%d%d%d%d", &x, &y, &d, &id);
      x--;
      y--;
      id--;
      wf[id][x][y] = min(wf[id][x][y], d);
      wf[id][y][x] = min(wf[id][y][x], d);
    }

    REP(l, c) {
      scanf("%d", &p[l]);
      REP(k, n)
      REP(i, n) REP(j, n) wf[l][i][j] =
          min(wf[l][i][j], wf[l][i][k] + wf[l][k][j]);
    }

    REP(k, c) {
      q[k][0] = 0;
      REP(i, p[k] - 1) scanf("%d", &q[k][i + 1]);
      q[k][p[k]] = INF;
      REP(i, p[k]) scanf("%d", &r[k][i]);

      REP(i, n) for (int j = i; j < n; j++) {
        if (wf[k][i][j] < INF) {
          tmp = 0;
          REP(l, p[k]) {
            if (wf[k][i][j] <= q[k][l + 1]) {
              tmp += r[k][l] * (wf[k][i][j] - q[k][l]);
              break;
            } else
              tmp += r[k][l] * (q[k][l + 1] - q[k][l]);
          }
          a[i][j] = a[j][i] = min(a[i][j], tmp);
        }
      }
    }

    REP(k, n) REP(i, n) REP(j, n) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);

    if (a[s - 1][g - 1] >= INF)
      printf("-1\\n");
    else
      printf("%d\\n", a[s - 1][g - 1]);
  }
}
Problem p00767 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int INF = (int)((unsigned int)-1 >> 1);

int main() {
  int taikaku[151][151];
  for (int i = 1; i <= 150; i++) {
    for (int j = i + 1; j <= 150; j++) {
      // jが横, iが縦
      taikaku[i][j] = i * i + j * j;
    }
  }

  while (1) {
    int h, w;
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    int input_taikaku = h * h + w * w;

    int ans_taikaku = 150 * 150;
    int ansh = 150, answ = 150;
    for (int i = 1; i < 150; i++) {
      for (int j = i + 1; j < 150; j++) {
        // jが横, iが縦
        if (input_taikaku < taikaku[i][j]) {
          if (ans_taikaku == taikaku[i][j]) {
            if (ansh > i) {
              ansh = i;
              answ = j;
            }
          } else if (ans_taikaku > taikaku[i][j]) {
            ans_taikaku = taikaku[i][j];
            ansh = i;
            answ = j;
          }
        } else if (input_taikaku == taikaku[i][j] && h < i) {
          if (ans_taikaku == taikaku[i][j]) {
            if (ansh > i) {
              ansh = i;
              answ = j;
            }
          } else if (ans_taikaku > taikaku[i][j]) {
            ans_taikaku = taikaku[i][j];
            ansh = i;
            answ = j;
          }
        }
      }
    }
    cout << ansh << " " << answ << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int INF = (int)((unsigned int)-1 >> 1);

int main() {
  int taikaku[151][151];
  for (int i = 1; i <= 150; i++) {
    for (int j = i + 1; j <= 150; j++) {
      // jが横, iが縦
      taikaku[i][j] = i * i + j * j;
    }
  }

  while (1) {
    int h, w;
    cin >> h >> w;
    int input_taikaku = h * h + w * w;

    int ans_taikaku = 150 * 150;
    int ansh = 150, answ = 150;
    for (int i = 1; i < 150; i++) {
      for (int j = i + 1; j < 150; j++) {
        // jが横, iが縦
        if (input_taikaku < taikaku[i][j]) {
          if (ans_taikaku == taikaku[i][j]) {
            if (ansh > i) {
              ansh = i;
              answ = j;
            }
          } else if (ans_taikaku > taikaku[i][j]) {
            ans_taikaku = taikaku[i][j];
            ansh = i;
            answ = j;
          }
        }
      }
    }
    cout << ansh << " " << answ << endl;
  }
}
Problem p00767 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define INF 1e9

int len(int i, int j) {
  int res = i * i + j * j;
  return res;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (1) {
    int h, w;
    cin >> h >> w;

    if (h == 0)
      break;

    int L = len(h, w);

    pair<int, int> ans = {10000, 10000};

    for (int i = 1; i < 1000; i++) {
      for (int j = i + 1; j < 1000; j++) {
        if (i == h && j == w)
          continue;
        int LL = len(i, j);
        if (LL == L && i < h)
          continue;
        if (LL >= L && LL < len(ans.first, ans.second)) {
          ans = {i, j};
        }
      }
    }
    cout << ans.first << " " << ans.second << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1e9

int len(int i, int j) {
  int res = i * i + j * j;
  return res;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (1) {
    int h, w;
    cin >> h >> w;

    int L = len(h, w);

    pair<int, int> ans = {10000, 10000};

    for (int i = 1; i < 1000; i++) {
      for (int j = i + 1; j < 1000; j++) {
        if (i == h && j == w)
          continue;
        int LL = len(i, j);
        if (LL == L && i < h)
          continue;
        if (LL >= L && LL < len(ans.first, ans.second)) {
          ans = {i, j};
        }
      }
    }
    cout << ans.first << " " << ans.second << endl;
  }
}
Problem p00767 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define dump(x) cout << (x) << '\\n';
typedef int64_t Int;
Int mod = 1e9 + 7;
Int INF = 1e9 + 7;

// vector<pair<int, int>> v[19803];

int main() {
  vector<pair<int, int>> v;
  while (true) {
    int h, w;
    cin >> h >> w;
    if (h == 0)
      break;
    int n = h * h + w * w;
    int mi = 1e9;
    pair<int, int> p;
    for (Int i = 150; i >= 1; i--) {
      for (Int j = 150; j > i; j--) {
        int x = i * i + j * j;
        if (x > n && mi >= x) {
          mi = x;
          p = {i, j};
        } else if (x == n && i > h) {
          mi = x;
          p = {i, j};
        }
      }
    }
    v.emplace_back(p);
  }
  // cout << endl;
  for (auto x : v)
    cout << x.first << " " << x.second << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define dump(x) cout << (x) << '\\n';
typedef int64_t Int;
Int mod = 1e9 + 7;
Int INF = 1e9 + 7;

vector<pair<int, int>> v[19803];

int main() {
  for (int i = 1; i <= 99; i++) {
    for (int j = i + 1; j <= 109; j++) {
      v[i * i + j * j].emplace_back(i, j);
    }
  }
  // ios::sync_with_stdio(false);
  while (true) {
    int h, w;
    cin >> h >> w;
    if (h == 0)
      break;
    int n = h * h + w * w;
    if (h == v[n].back().first) {
      while (v[n].empty())
        n++;
      cout << v[n][0].first << " " << v[n][0].second << endl;
    } else {
      int i = 0;
      while (v[n][i].first != h)
        i++;
      i++;
      cout << v[n][i].first << " " << v[n][i].second << endl;
    }
  }
  return 0;
}
Problem p00767 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

struct rect {
  int h, w, len;
};

int main() {
  int h, w;
  while (cin >> h >> w, h || w) {
    struct rect ans;
    ans.h = ans.w = 150;
    ans.len = 150 * 150 + 150 * 150;
    range(i, 1, 150) { // i:width
      range(j, 1, i) { // j:height
        if ((h * h + w * w < j * j + i * i && j * j + i * i < ans.len) ||
            (h * h + w * w < j * j + i * i && j * j + i * i == ans.len &&
             j < ans.h) ||
            (h * h + w * w == j * j + i * i && h < j && j < ans.h)) {
          if (i == w && j == h)
            continue;
          ans.h = j;
          ans.w = i;
          ans.len = i * i + j * j;
        }
      }
    }
    cout << ans.h << ' ' << ans.w << endl;
  }
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

string sort(string str, int len, int x) {
  while (len != str.size())
    str += '0';
  if (x == 1) {
    rep(i, str.size() - 1) {
      rep(j, str.size() - 1) {
        if (str[j] > str[j + 1]) {
          char temp = str[j];
          str[j] = str[j + 1];
          str[j + 1] = temp;
        }
      }
    }
  } else if (x == 0) {
    rep(i, str.size() - 1) {
      rep(j, str.size() - 1) {
        if (str[j] < str[j + 1]) {
          char temp = str[j];
          str[j] = str[j + 1];
          str[j + 1] = temp;
        }
      }
    }
  }
  return str;
}

int changeNum(string str) {
  int num = 0, K = 1;
  for (int i = str.size() - 1; i >= 0; i--) {
    num += (str[i] - '0') * K;
    K *= 10;
  }
  return num;
}

string changeStr(int n, int len) {
  string str;
  int K = 1;
  rep(i, len) K *= 10;
  rep(i, len + 1) {
    if (n / K == 0)
      str += '0';
    else
      str += ('0' + n / K);
    n %= K;
    K /= 10;
  }
  return str;
}

bool checkNum(int a[25], int i) {
  rep(j, i) {
    if (a[i] == a[j] && i != j) {
      cout << j << ' ' << a[i] << ' ' << i - j << endl;
      return 1;
    }
  }
  return 0;
}

int main() {
  string str;
  int len, A[25];
  while (cin >> str >> len, str[0] != '0' || len) {
    A[0] = changeNum(sort(str, len, -1));
    range(i, 1, 24) {
      A[i] = changeNum(sort(str, len, 0)) - changeNum(sort(str, len, 1));
      str = changeStr(A[i], len - 1);
      if (checkNum(A, i))
        break;
    }
  }
}
Problem p00767 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

void solve(int a, int b) {
  int c = a * a + b * b;
  bool s = false;
  while (1) {
    for (int i = 1; i <= 150; i++) {
      for (int j = i + 1; j <= 150; j++) {
        if ((s || i > a) && i * i + j * j == c) {
          cout << i << " " << j << endl;
          return;
        }
      }
    }
    c++;
    s = true;
  }
}

int main(void) {
  int a, b;
  while (cin >> a >> b) {
    if (a == 0)
      break;
    solve(a, b);
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int h[100];
  int w[100];
  int m;
  for (int i = 0; i < 100; i++) {
    cin >> h[i] >> w[i];
    if (h[i] == 0) {
      m = i;
      break;
    }
  }
  int a;
  for (int i = 0; i < m; i++) {
    int b = 0;
    int c = 0;
    a = h[i] * h[i] + w[i] * w[i];
    while (1) {
      for (int j = 1; j < 150; j++) {
        for (int k = j + 1; k < 150; k++) {
          if (j < k && j * j + k * k == a + 1) {
            cout << j << " " << k << endl;
            b++;
            c++;
            break;
          } else if (j < k && h[i] < j && j * j + k * k == a) {
            cout << j << " " << k << endl;
            b++;
            c++;
            break;
          }
        }
        if (c != 0) {
          break;
        }
      }
      if (b != 0) {
        break;
      }
      a++;
    }
  }
}
Problem p00767 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<int, int> pii;
typedef pair<int, pii> P;

int main() {
  int h, w;
  while (cin >> h >> w, h || w) {
    P in = make_pair(w * w + h * h, pii(h, w));
    P ans = make_pair(1e9, pii(100000, 100000));
    reep(i, 1, 1000) {
      reep(j, i + 1, 1000) {
        P tmp = make_pair(i * i + j * j, pii(i, j));
        if (tmp > in) {
          ans = min(ans, tmp);
        }
      }
    }
    cout << ans.second.first << " " << ans.second.second << endl;
  }
}

Error: TLE

Fixed submission:

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

typedef pair<int, int> pii;
typedef pair<int, pii> P;

int main() {
  int h, w;
  while (cin >> h >> w, h || w) {
    P in = make_pair(w * w + h * h, pii(h, w));
    P ans = make_pair(1e9, pii(100000, 100000));
    reep(i, 1, 10000) {
      reep(j, i + 1, 10000) {
        P tmp = make_pair(i * i + j * j, pii(i, j));
        if (tmp > in) {
          ans = min(ans, tmp);
        }
      }
    }
    cout << ans.second.first << " " << ans.second.second << endl;
  }
}
Problem p00768 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int M, T, P, R, m, t, p, j;
  while (cin >> M >> T >> P >> R, M || T || P || R) {
    vector<vector<pair<int, int>>> data(
        T + 1, vector<pair<int, int>>(P + 1, pair<int, int>(0, 0)));
    vector<tuple<int, int, int>> score(T + 1);
    for (int i = 0; i < R; i++) {
      cin >> m >> t >> p >> j;
      if (j == 0) {
        data[t][p].first = m + data[t][p].second * 20;
        get<0>(score[t])--;
      } else {
        data[t][p].second++;
      }
    }
    for (int i = 1; i <= T; i++) {
      int sco = 0, pena = 0;
      for (int j = 0; j <= P; j++) {
        if (data[i][j].first) {
          sco--;
          pena += data[i][j].first;
        }
      }
      score[i] = tuple<int, int, int>(get<0>(score[i]), pena, -i);
    }
    sort(score.begin() + 1, score.end());
    tuple<int, int, int> temp = score[1];
    cout << -get<2>(score[1]);
    for (int i = 2; i <= T; i++) {
      if (get<0>(temp) == get<0>(score[i]) &&
          get<1>(temp) == get<1>(score[i])) {
        cout << "=";
      } else {
        cout << ",";
      }
      cout << -get<2>(score[i]);
      temp = score[i];
    }
    cout << endl;
  }
  return 0;
}

Error: -6
free(): invalid size


Fixed submission:

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

int main() {
  int M, T, P, R, m, t, p, j;
  while (cin >> M >> T >> P >> R, M || T || P || R) {
    vector<vector<pair<int, int>>> data(
        T + 1, vector<pair<int, int>>(P, pair<int, int>(0, 0)));
    vector<tuple<int, int, int>> score(T + 1);
    for (int i = 0; i < R; i++) {
      cin >> m >> t >> p >> j;
      if (j == 0) {
        data[t][p].first = m + data[t][p].second * 20;
        get<0>(score[t])--;
      } else {
        data[t][p].second++;
      }
    }
    for (int i = 1; i <= T; i++) {
      int sco = 0, pena = 0;
      for (int j = 0; j < P; j++) {
        if (data[i][j].first) {
          sco--;
          pena += data[i][j].first;
        }
      }
      score[i] = tuple<int, int, int>(get<0>(score[i]), pena, -i);
    }
    sort(score.begin() + 1, score.end());
    tuple<int, int, int> temp = score[1];
    cout << -get<2>(score[1]);
    for (int i = 2; i <= T; i++) {
      if (get<0>(temp) == get<0>(score[i]) &&
          get<1>(temp) == get<1>(score[i])) {
        cout << "=";
      } else {
        cout << ",";
      }
      cout << -get<2>(score[i]);
      temp = score[i];
    }
    cout << endl;
  }
  return 0;
}