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

#include <bits/stdc++.h>

using namespace std;

#define N_CONTEST (26)
#define N_DAY (365)

static void get_input(int &D, vector<int> &c, vector<vector<int>> &s) {
  cin >> D;
  assert(D == N_DAY);
  for (int i = 1; i <= N_CONTEST; i++) {
    cin >> c.at(i);
  }
  for (int i = 1; i <= N_DAY; i++) {
    for (int j = 1; j <= N_CONTEST; j++) {
      cin >> s.at(i).at(j);
    }
  }
}

// static long long calc_score (vector<int> &c, vector<vector<int>> &s,
// vector<int> &t) {
//     vector<int> last(N_CONTEST+1, 0);
//
//     long long score = 0;
//
//     for ( int d = 1; d <= N_DAY; d++ ) {
//         score += s.at(d).at(t.at(d));
//         last.at(t.at(d)) = d;
//         for ( int con = 1; con <= N_CONTEST; con++ ) {
//             score -= c.at(con) * (d - last.at(con));
//         }
//         cout << score << endl;
//     }
//
//     return score;
// }

static int calc_greedy_ans(vector<int> &c, vector<vector<int>> &s,
                           vector<int> &last, int day) {
  int max_score = INT_MIN;
  int ans_c = 1;

  for (int con = 1; con <= N_CONTEST; con++) {
    int score = s.at(day).at(con);
    for (int con = 1; con <= N_CONTEST; con++) {
      score -= c.at(con) * (day - last.at(con));
    }
    if (score > max_score) {
      max_score = score;
      ans_c = con;
    }
  }

  last.at(ans_c) = day;

  return ans_c;
}

int main(void) {
  int D;
  vector<int> c(N_CONTEST + 1);
  vector<vector<int>> s(N_DAY + 1, vector<int>(N_CONTEST + 1, 0));

  get_input(D, c, s);

  vector<int> last(N_CONTEST + 1, 0);
  for (int d = 1; d <= N_DAY; d++) {
    int greedy_c = calc_greedy_ans(c, s, last, d);
    cout << greedy_c << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define N_CONTEST (26)
#define N_DAY (5)

static void get_input(int &D, vector<int> &c, vector<vector<int>> &s) {
  cin >> D;
  assert(D == N_DAY);
  for (int i = 1; i <= N_CONTEST; i++) {
    cin >> c.at(i);
  }
  for (int i = 1; i <= N_DAY; i++) {
    for (int j = 1; j <= N_CONTEST; j++) {
      cin >> s.at(i).at(j);
    }
  }
}

static long long calc_score(vector<int> &c, vector<vector<int>> &s,
                            vector<int> &t) {
  vector<int> last(N_CONTEST + 1, 0);

  long long score = 0;

  for (int d = 1; d <= N_DAY; d++) {
    score += s.at(d).at(t.at(d));
    last.at(t.at(d)) = d;
    for (int con = 1; con <= N_CONTEST; con++) {
      score -= c.at(con) * (d - last.at(con));
    }
    cout << score << endl;
  }

  return score;
}

static int calc_greedy_ans(vector<int> &c, vector<vector<int>> &s,
                           vector<int> &last, int day) {
  int max_score = 0;
  int ans_c = 0;

  for (int con = 1; con <= N_CONTEST; con++) {
    int score = s.at(day).at(con);
    for (int con = 1; con <= N_CONTEST; con++) {
      score -= c.at(con) * (day - last.at(con));
    }
    if (score > max_score) {
      max_score = score;
      ans_c = con;
    }
  }

  last.at(ans_c) = day;

  return ans_c;
}

int main(void) {
  int D;
  vector<int> c(N_CONTEST + 1);
  vector<vector<int>> s(N_DAY + 1, vector<int>(N_CONTEST + 1, 0));

  get_input(D, c, s);

  vector<int> last(N_CONTEST + 1, 0);
  for (int d = 1; d <= N_DAY; d++) {
    int greedy_c = calc_greedy_ans(c, s, last, d);
    cout << greedy_c << endl;
  }

  //    vector<int> t(N_DAY+1, 0);
  //    for ( int i = 1; i <= N_DAY; i++ ) {
  //        cin >> t.at(i);
  //    }
  //
  //    calc_score(c, s, t);

  return 0;
}