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

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

int D;
vector<int> C(26);
vector<vector<int>> S(365, vector<int>(26));
vector<int> last(26, 0);
vector<int> schedule(365);

const int CHANGE_NUM = 5;

int calc(vector<int> &schedule) {
  rep(i, 26) last[i] = 0;
  int score = 0;
  for (int day = 0; day < D; day++) {
    schedule[day] = min(schedule[day], 25);
    assert(schedule[day] >= 0 && schedule[day] < 26);
    score += S[day][schedule[day]];
    last[schedule[day]] = day + 1;
    for (int j = 0; j < 26; j++) {
      score -= C[j] * (day - last[j]);
    }
  }
  return score;
}

inline double get_militime(chrono::system_clock::time_point start) {
  auto end = chrono::system_clock::now();
  double ret = chrono::duration_cast<chrono::milliseconds>(end - start).count();
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  chrono::system_clock::time_point start;
  start = chrono::system_clock::now();
  random_device rd;
  mt19937 mt(rd());
  uniform_int_distribution<int> day_dist(1, 364);
  uniform_int_distribution<int> cont_dist(0, 25);
  cin >> D;
  rep(i, 26) cin >> C[i];
  rep(i, D) rep(j, 26) cin >> S[i][j];
  int score = 0;
  for (int day = 1; day <= D; day++) {
    int next_choice = 0;
    int max_advantage = -INF;
    for (int j = 0; j < 26; j++) {
      int next_score = score + S[day - 1][j];
      int tmp = last[j];
      last[j] = day;
      for (int k = 0; k < 26; k++) {
        next_score -= C[k] * (day - last[k]);
      }
      int adv = next_score - score;
      if (adv > max_advantage) {
        max_advantage = adv;
        next_choice = j;
      }
      last[j] = tmp;
    }
    schedule[day - 1] = next_choice;
    last[next_choice] = day;
    score = score + max_advantage;
  }
  vector<int> change_day(CHANGE_NUM);
  vector<int> original(CHANGE_NUM);
  int cnt = 0;
  while (1) {
    cnt++;
    if (get_militime(start) > 1950.0) {
      break;
    }
    rep(i, CHANGE_NUM) {
      change_day[i] = day_dist(mt);
      original[i] = schedule[change_day[i]];
    }
    rep(i, CHANGE_NUM) { schedule[change_day[i]] = cont_dist(mt); }
    int c = calc(schedule);
    if (score < c) {
      score = c;
    } else {
      rep(i, CHANGE_NUM) { schedule[change_day[i]] = original[i]; }
    }
  }
  rep(i, D) { cout << schedule[i] + 1 << endl; }
  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

int D;
vector<int> C(26);
vector<vector<int>> S(365, vector<int>(26));
vector<int> last(26, 0);

int calc(vector<int> &schedule) {
  rep(i, 26) last[i] = 0;
  int score = 0;
  for (int day = 1; day <= D; day++) {
    score += S[day - 1][schedule[day - 1]];
    last[schedule[day - 1]] = day;
    for (int j = 0; j < 26; j++) {
      score -= C[j] * (day - last[j]);
    }
  }
  return score;
}

inline double get_militime(chrono::system_clock::time_point start) {
  auto end = chrono::system_clock::now();
  double ret = chrono::duration_cast<chrono::milliseconds>(end - start).count();
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  chrono::system_clock::time_point start;
  start = chrono::system_clock::now();
  random_device rd;
  mt19937 mt(rd());
  uniform_int_distribution<int> day_dist(0, 364);
  uniform_int_distribution<int> cont_dist(0, 25);
  cin >> D;
  rep(i, 26) cin >> C[i];
  rep(i, D) rep(j, 26) cin >> S[i][j];
  int score = 0;
  vector<int> schedule(D);
  for (int day = 1; day <= D; day++) {
    int next_choice = 0;
    int max_advantage = -INF;
    for (int j = 0; j < 26; j++) {
      int next_score = score + S[day - 1][j];
      int tmp = last[j];
      last[j] = day;
      for (int k = 0; k < 26; k++) {
        next_score -= C[k] * (day - last[k]);
      }
      int adv = next_score - score;
      if (adv > max_advantage) {
        max_advantage = adv;
        next_choice = j;
      }
      last[j] = tmp;
    }
    schedule[day - 1] = next_choice + 1;
    last[next_choice] = day;
    score = score + max_advantage;
  }
  int CHANGE_NUM = 5;
  vector<int> change_day(CHANGE_NUM);
  while (1) {
    if (get_militime(start) > 1950.0) {
      break;
    }
    rep(i, CHANGE_NUM) change_day[i] = day_dist(mt);
    auto old = schedule;
    for (auto x : change_day) {
      schedule[x] = cont_dist(mt);
    }
    int c = calc(schedule);
    if (score < c) {
      score = c;
    } else {
      schedule = old;
    }
  }
  rep(i, D) { cout << schedule[i] << endl; }
  return 0;
}