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

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

typedef pair<pair<int, int>, int> P;
bool comp(P hl, P hr) {
  if (hl.first.first != hr.first.first)
    return hl.first.first > hr.first.first;
  if (hl.first.second != hr.first.second)
    return hl.first.second < hr.first.second;
  return hl.second > hr.second;
}

int main() {
  while (1) { // start

    // init
    int pm[51];
    int solved[51];
    int wa[51][11];
    for (int i = 0; i < 51; i++) {
      pm[i] = 0;
      solved[i] = 0;
      for (int j = 0; j < 11; j++)
        wa[i][j] = 0;
    }

    int M, T, P, R;
    cin >> M >> T >> P >> R;
    if (M == 0 and T == 0 and P == 0 and R == 0)
      break;

    for (int i = 0; i < R; i++) {
      int m, t, p, j;
      cin >> m >> t >> p >> j;

      // ac
      if (j == 0) {
        solved[t]++;
        pm[t] += m + wa[t][p] * 20;
      }
      // wa
      else
        wa[t][p]++;
    }

    vector<pair<pair<int, int>, int>> v;
    for (int i = 1; i <= T; i++) {
      v.push_back(make_pair(make_pair(solved[i], pm[i]), i));
    }

    sort(v.begin(), v.end(), comp);

    // debug
    // for(int i = 0; i < (int)v.size(); i++) cout << v[i].first.first << " " <<
    // v[i].first.second << " " << v[i].second << endl;

    int tm = v[0].first.second;
    int sol = v[0].first.first;
    cout << v[0].second;
    for (int i = 1; i < (int)v.size(); i++) {
      if (sol == v[i].first.first and tm == v[i].first.second)
        cout << "=";
      else
        cout << ",";
      cout << v[i].second;
      sol = v[i].first.first;
      tm = v[i].first.second;
    }
    cout << endl;

  } // end
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
P info[51][11];
// bool team[51];
vector<P> v;

map<int, P> mp;
// int D[51];
vector<int> order;

// 自作の比較関数
bool comp(P lhs, P rhs) {
  if (lhs.first != rhs.first) {
    return lhs.first < rhs.first;
  }
  if (lhs.second != rhs.second) {
    return lhs.second > rhs.second;
  }
}

int main() {
  while (1) { // start

    int M, T, P, R;
    cin >> M >> T >> P >> R;
    if (M == 0 and T == 0 and P == 0 and R == 0)
      break;

    // 初期化
    for (int i = 0; i < 51; i++) {
      for (int j = 0; j < 11; j++) {
        info[i][j].first = 0;
        info[i][j].second = 0;
      }
    }
    bool team[51] = {0};
    int D[51] = {0};
    int OK[51] = {0};

    // データを格納
    for (int i = 0; i < R; i++) {
      int m, t, p, j;
      cin >> m >> t >> p >> j;
      team[t] = true;

      if (j == 0) {
        info[t][p].second = m + 20 * info[t][p].first;
        OK[t - 1] += 1;
      } else {
        info[t][p].first += 1;
      }
    }

    // vの内部はキーがチーム名(解いた数/合計時間)
    for (int i = 1; i <= T; i++) {
      int sum = 0, ok = 0;
      for (int j = 0; j < 11; j++)
        sum += info[i][j].second;
      // for(int j = 0; j < 11; j++) if(info[i][j].second) ok += 1;
      int tmp = OK[i - 1];
      v.push_back(make_pair(tmp, sum));
      mp[i] = make_pair(tmp, sum);
    }

    sort(v.begin(), v.end(), comp);
    reverse(v.begin(), v.end());
    // debug
    // for(int i = 0; i < (int)v.size(); i++) cout << v[i].first << " " <<
    // v[i].second << endl;

    // 重複カウンタを作成
    int d = 1;
    D[0] = 1;
    for (int i = 1; i < (int)v.size(); i++) {
      if (v[i - 1].first == v[i].first and v[i - 1].second == v[i].second) {
        D[i] = d;
      } else {
        d++;
        D[i] = d;
      }
    }

    // debug
    // for(int i = 0; i < (int)v.size(); i++) cout << D[i] << endl;

    // ソート結果にチーム名を割り当てる
    bool vis[51] = {false};
    for (int i = 0; i < (int)v.size(); i++) {
      for (int j = (int)v.size(); 1 <= j; j--) {
        if (v[i] == mp[j]) {
          if (vis[j] == false) {
            order.push_back(j);
            vis[j] = true;
          }
        }
      }
    }

    // debug
    // for(int i = 0; i < (int)v.size(); i++) cout << order[i] << endl;

    // 出力
    // まず重複数を記録する
    int num[51] = {0};
    for (int i = 0; i < 51; i++)
      num[D[i]] += 1;

    string ans = "A";
    int idx = 0;
    for (int i = 1; i <= (int)order.size(); i++) {
      if (num[i] == 0)
        break;
      else if (num[i] == 1) {
        ans += to_string(order[idx]);
        idx++;
      } else {
        for (int j = 0; j < num[i]; j++) {
          if (j == num[i] - 1)
            ans += to_string(order[idx]);
          else
            ans += to_string(order[idx]) + "=";
          idx++;
        }
      }
      if (idx != (int)order.size())
        ans += ",";
    }

    ans.erase(ans.begin() + 0);
    // ans.erase(ans.end() - 1);
    cout << ans << endl;

    // 初期化
    v.clear();
    mp.clear();
    order.clear();

  } // end

  return 0;
}