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

// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

#define INF (1 << 30)

typedef pair<int, int> pii;

vector<int> R;

vector<int> calc_rank(vector<pair<int, int>> status);

int calc_rank_all(vector<pii> S, vector<pii> C, vector<pii> P) {
  int N = S.size();
  vector<int> rank_S = calc_rank(S);
  vector<int> rank_C = calc_rank(C);
  vector<int> rank_P = calc_rank(P);

  vector<int> rank(S.size());
  vector<pii> score(S.size());
  for (int i = 0; i < N; i++) {
    score[i].first = R[rank_S[i] - 1] + R[rank_C[i] - 1] + R[rank_P[i] - 1];
    score[i].second = i;
    // cerr << "score[" << i << "] = " << score[i].first << endl;
  }
  sort(score.begin(), score.end(), greater<pii>());

  rank[score[0].second] = 1;

  for (int i = 1; i < N; i++) {
    if (score[i].first == score[i - 1].first)
      rank[score[i].second] = rank[score[i - 1].second];
    else
      rank[score[i].second] = i + 1;
  }

  return rank[0];
}

vector<int> calc_rank(vector<pair<int, int>> status) {
  int N = status.size();

  sort(status.begin(), status.end(), greater<pii>());

  vector<int> rank(N);
  rank[status[0].second] = 1;

  for (int i = 1; i < N; i++) {
    if (status[i].first == status[i - 1].first)
      rank[status[i].second] = rank[status[i - 1].second];
    else
      rank[status[i].second] = i + 1;
  }

  // cerr << "--------------------" << endl;
  // for (int i=0; i<N; i++) {
  //     cerr << status[i].first << ", " << status[i].second << ", " << rank[i]
  //     << endl;
  // }

  return rank;
}

/*
vector<int> calc_rank(vector<pii> status)
{
    int N = status.size();
    sort(status.begin(), status.end(), greater<pii>());
}
*/

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

  vector<pair<int, int>> S(N), P(N), C(N);
  R.resize(N);

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

  for (int i = 0; i < N; i++) {
    cin >> S[i].first >> P[i].first >> C[i].first;
    S[i].second = i;
    P[i].second = i;
    C[i].second = i;
  }

  int ans = INF;
  for (int j = 1; j <= 100; j++) {
    // cerr << "------------------" << endl;
    // cerr << "j = " << j << endl;

    S[0].first += j;
    int rank = calc_rank_all(S, C, P);
    if (rank <= 8) {
      ans = min(ans, j);
    }

    S[0].first -= j;
  }

  for (int j = 1; j <= 100; j++) {
    P[0].first += j;
    int rank = calc_rank_all(S, C, P);
    if (rank <= 8) {
      ans = min(ans, j);
    }
    P[0].first -= j;
  }

  for (int j = 1; j <= 100; j++) {
    C[0].first += j;
    int rank = calc_rank_all(S, C, P);
    if (rank <= 8) {
      ans = min(ans, j);
    }
    C[0].first -= j;
  }

  if (ans == INF)
    cout << "Saiko" << endl;
  else
    cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

#define INF (1 << 30)

typedef pair<int, int> pii;

vector<int> R;

vector<int> calc_rank(vector<pair<int, int>> status);

int calc_rank_all(vector<pii> S, vector<pii> C, vector<pii> P) {
  int N = S.size();
  vector<int> rank_S = calc_rank(S);
  vector<int> rank_C = calc_rank(C);
  vector<int> rank_P = calc_rank(P);

  vector<int> rank(S.size());
  vector<pii> score(S.size());
  for (int i = 0; i < N; i++) {
    score[i].first = R[rank_S[i] - 1] + R[rank_C[i] - 1] + R[rank_P[i] - 1];
    score[i].second = i;
    // cerr << "score[" << i << "] = " << score[i].first << endl;
  }
  sort(score.begin(), score.end(), greater<pii>());

  rank[score[0].second] = 1;

  for (int i = 1; i < N; i++) {
    if (score[i].first == score[i - 1].first)
      rank[score[i].second] = rank[score[i - 1].second];
    else
      rank[score[i].second] = i + 1;

    if (score[i].second == 0)
      return rank[0];
  }

  assert(false);
  return INF;
}

vector<int> calc_rank(vector<pair<int, int>> status) {
  int N = status.size();

  sort(status.begin(), status.end(), greater<pii>());

  vector<int> rank(N);
  rank[status[0].second] = 1;

  for (int i = 1; i < N; i++) {
    if (status[i].first == status[i - 1].first)
      rank[status[i].second] = rank[status[i - 1].second];
    else
      rank[status[i].second] = i + 1;
  }

  // cerr << "--------------------" << endl;
  // for (int i=0; i<N; i++) {
  //     cerr << status[i].first << ", " << status[i].second << ", " << rank[i]
  //     << endl;
  // }

  return rank;
}

/*
vector<int> calc_rank(vector<pii> status)
{
    int N = status.size();
    sort(status.begin(), status.end(), greater<pii>());
}
*/

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

  vector<pair<int, int>> S(N), P(N), C(N);
  R.resize(N);

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

  for (int i = 0; i < N; i++) {
    cin >> S[i].first >> P[i].first >> C[i].first;
    S[i].second = i;
    P[i].second = i;
    C[i].second = i;
  }

  int ans = INF;
  for (int j = 1; j <= 100; j++) {
    // cerr << "------------------" << endl;
    // cerr << "j = " << j << endl;

    S[0].first += j;
    int rank = calc_rank_all(S, C, P);
    if (rank <= 8) {
      ans = min(ans, j);
    }

    S[0].first -= j;
  }

  for (int j = 1; j <= 100; j++) {
    P[0].first += j;
    int rank = calc_rank_all(S, C, P);
    if (rank <= 8) {
      ans = min(ans, j);
    }
    P[0].first -= j;
  }

  for (int j = 1; j <= 100; j++) {
    C[0].first += j;
    int rank = calc_rank_all(S, C, P);
    if (rank <= 8) {
      ans = min(ans, j);
    }
    C[0].first -= j;
  }

  if (ans == INF)
    cout << "Saiko" << endl;
  else
    cout << ans << endl;
  return 0;
}