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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip> //double精度 setprecision
#include <iostream>
#include <map>
#include <numeric> //lcm
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define rrep1(i, n) for (int i = (n); i >= 1; --i)
#define REP(i, n, m) for (int i = (n); i < (m); ++i)
#define all(vec) (vec).begin(), (vec).end()

#define debug(vec)                                                             \\
  for (auto v : vec)                                                           \\
    cerr << v << " ";                                                          \\
  cerr << endl;
#define debug2D(vec2D, w)                                                      \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << setw(w) << v << " ";                                             \\
    cerr << endl;                                                              \\
  }
#define debugP(vec)                                                            \\
  for (auto v : vec)                                                           \\
    cerr << "(" << v.first << "," << v.second << ") ";                         \\
  cerr << endl;
#define debug2DP(vec2D)                                                        \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << "(" << v.first << "," << v.second << ") ";                       \\
    cerr << endl;                                                              \\
  }

const char en = '\\n';

typedef long long ll;

constexpr ll INF = 1000000000; // 10^10
// const ll MOD = 998244353;
constexpr ll MOD = 1000000007;

typedef ll Weight;
void add_edge(vector<vector<Weight>> &g, Weight from, Weight to,
              bool isDirected) {
  g[from].push_back(to);
  if (!isDirected)
    g[to].push_back(from);
}

template <class T> inline bool chmax(T &a, T b) {
  if (a <= b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}

int d = 365;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  cin >> d;
  int c[26];
  rep(i, 26) cin >> c[i];
  // int s[365][26];
  vector<vector<int>> s(d, vector<int>(26));
  // int t[365];
  vector<int> t(d);
  set<int> last[26];
  rep(i, 26) last[i].insert(-1);
  auto prevday = [&last, &t](int day, int contest) {
    auto it = last[contest].lower_bound(day);
    if (it == last[contest].end() || *it != day)
      return *prev(it);
    else /*(*it == day)*/
      return day;
  };

  rep(i, d) {
    int maxt = -1, maxcontest = -1;
    rep(j, 26) {
      cin >> s[i][j];
      if (chmax(maxt, s[i][j]))
        maxcontest = j;
    }
    t[i] = maxcontest;
  }

  rep(i, d) { cerr << t[i] << " "; }
  cerr << en;

  rep(i, d) { last[t[i]].insert(i); }

  // cerr << "t : ";
  // for(auto ti : t) cerr << ti << " " ; cerr << endl;
  // rep(i,26) { for(auto day : last[i])  cerr << day << " "; cerr << endl;}

  auto score = [&last, &c, &s, &t, prevday]() {
    ll ret = 0;
    rep(i, d) {
      ret += s[i][t[i]];
      rep(j, 26) ret -= (ll)c[j] * (i - prevday(i, j));
      // cout << ret << endl;
    }
    return ret;
  };

  ll ans = score();

  auto modify = [&](int day, int q) {
    ll ans0 = 0;
    ans0 -= s[day][t[day]];
    ans0 += c[t[day]] * (day - prevday(day, t[day]));
    last[t[day]].erase(day);
    last[q].insert(day);
    ans0 += s[day][q];
    ans0 -= c[q] * (day - prevday(day, q));

    if (ans0 >= 0) {
      ans += ans0;
      t[day] = q;
    }
    // cout << ans << endl;
  };

  int cnt = 0;
  while (cnt < 1) {
    rep(i, d) rep(j, 26) modify(i, j);
    cnt++;
    // cout << ans << endl;
  }

  for (auto ti : t)
    cout << ti + 1 << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip> //double精度 setprecision
#include <iostream>
#include <map>
#include <numeric> //lcm
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define rrep1(i, n) for (int i = (n); i >= 1; --i)
#define REP(i, n, m) for (int i = (n); i < (m); ++i)
#define all(vec) (vec).begin(), (vec).end()

#define debug(vec)                                                             \\
  for (auto v : vec)                                                           \\
    cerr << v << " ";                                                          \\
  cerr << endl;
#define debug2D(vec2D, w)                                                      \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << setw(w) << v << " ";                                             \\
    cerr << endl;                                                              \\
  }
#define debugP(vec)                                                            \\
  for (auto v : vec)                                                           \\
    cerr << "(" << v.first << "," << v.second << ") ";                         \\
  cerr << endl;
#define debug2DP(vec2D)                                                        \\
  for (auto vec : vec2D) {                                                     \\
    for (auto v : vec)                                                         \\
      cerr << "(" << v.first << "," << v.second << ") ";                       \\
    cerr << endl;                                                              \\
  }

const char en = '\\n';

typedef long long ll;

constexpr ll INF = 1000000000; // 10^10
// const ll MOD = 998244353;
constexpr ll MOD = 1000000007;

typedef ll Weight;
void add_edge(vector<vector<Weight>> &g, Weight from, Weight to,
              bool isDirected) {
  g[from].push_back(to);
  if (!isDirected)
    g[to].push_back(from);
}

template <class T> inline bool chmax(T &a, T b) {
  if (a <= b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}

int d = 365;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  cin >> d;
  int c[26];
  rep(i, 26) cin >> c[i];
  // int s[365][26];
  vector<vector<int>> s(d, vector<int>(26));
  // int t[365];
  vector<int> t(d);
  set<int> last[26];
  rep(i, 26) last[i].insert(-1);
  auto prevday = [&last, &t](int day, int contest) {
    auto it = last[contest].lower_bound(day);
    if (it == last[contest].end() || *it != day)
      return *prev(it);
    else /*(*it == day)*/
      return day;
  };

  rep(i, d) {
    int maxt = -1, maxix = -1;
    rep(j, 26) {
      cin >> s[i][j];
      if (chmax(maxt, s[i][j]))
        maxix = i;
    }
    t[i] = maxix;
  }

  // rep(i,d) { cin >> t[i]; t[i]--;}

  rep(i, d) last[t[i]].insert(i);

  // cerr << "t : ";
  // for(auto ti : t) cerr << ti << " " ; cerr << endl;
  // rep(i,26) { for(auto day : last[i])  cerr << day << " "; cerr << endl;}

  auto score = [&last, &c, &s, &t, &prevday]() {
    ll ret = 0;
    rep(i, d) {
      ret += s[i][t[i]];
      rep(j, 26) ret -= (ll)c[j] * (i - prevday(i, j));
      // cout << ret << endl;
    }
    return ret;
  };

  ll ans = score();

  auto modify = [&](int day, int q) {
    ll ans0 = 0;
    ans0 -= s[day][t[day]];
    ans0 += c[t[day]] * (day - prevday(day, t[day]));
    last[t[day]].erase(day);
    last[q].insert(day);
    ans0 += s[day][q];
    ans0 -= c[q] * (day - prevday(day, q));

    if (ans0 >= 0) {
      ans += ans0;
      t[day] = q;
    }
    // cout << ans << endl;
  };

  int cnt = 0;
  while (cnt < 20) {
    rep(i, d) rep(j, 26) modify(i, j);
    cnt++;

    // cout << ans << endl;
  }

  for (auto ti : t)
    cout << ti + 1 << endl;
}