Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int D = 365, T = 26;

vector<int> c(T);
vector<vector<int>> s(D, vector<int>(T));
vector<vector<int>> pena(T, vector<int>(D + 2));

class Timer {
private:
  chrono::high_resolution_clock::time_point start;

public:
  Timer() { reset(); }
  void reset() { start = chrono::high_resolution_clock::now(); }
  chrono::milliseconds::rep elapsed() {
    auto end = chrono::high_resolution_clock::now();
    return chrono::duration_cast<chrono::milliseconds>(end - start).count();
  }
} timer;

auto current = timer.elapsed();

struct State {
  vector<int> ct;
  vector<vector<int>> h;
  State() : ct(D), h(T) {
    for (int t = 0; t < T; ++t) {
      h[t].push_back(-1);
      h[t].push_back(D);
    }
  }
  void add(int d, int i) {
    auto it = lower_bound(h[i].begin(), h[i].end(), d);
    h[i].insert(it, d);
    ct[d] = i;
  }
  void rmv(int d) {
    h[ct[d]].erase(remove(h[ct[d]].begin(), h[ct[d]].end(), d), h[ct[d]].end());
  }
  int calc_each(int i) {
    int res = 0;
    for (int d = 1; d < h[i].size() - 1; ++d) {
      res += s[h[i][d]][i];
    }
    for (int d = 0; d < h[i].size() - 1; ++d) {
      res -= pena[i][h[i][d + 1] - h[i][d]];
    }
    return res;
  }
  int calc() {
    int res = 0;
    for (int t = 0; t < T; ++t)
      res += calc_each(t);
    return res;
  }
  int calc_diff(int d, int bf, int af) {
    int res = s[d][af] - s[d][bf];
    {
      auto it = lower_bound(h[bf].begin(), h[bf].end(), d);
      int r = *next(it) - d, l = d - *prev(it);
      res -= pena[bf][l + r] - pena[bf][r] - pena[bf][l];
    }
    {
      auto it = lower_bound(h[af].begin(), h[af].end(), d);
      int r = *it - d, l = d - *prev(it);
      res += pena[af][l + r] - pena[af][r] - pena[af][l];
    }
    return res;
  }
  void output() {
    for (int i = 0; i < D; ++i)
      cout << ct[i] + 1 << '\n';
  }
  void update() {
    int d = rand() % D, bf = ct[d], af = rand() % T;
    if (bf == af)
      return;
    if (calc_diff(d, bf, af) > 0) {
      rmv(d);
      add(d, af);
    }
  }
  void swap() {
    int b = rand() % 13 + 1;
    int d = rand() % (D - b);
    int n = d + b;
    int bf = ct[d], af = ct[n];
    if (calc_diff(d, bf, af) + calc_diff(n, af, bf) > 0) {
      ct[d] = n;
      ct[n] = d;
      rmv(d);
      rmv(n);
      add(d, af);
      add(n, bf);
    }
  }
  void exe() {
    update();
    swap();
  }
};

State state;

void input() {
  int _;
  cin >> _;
  for (int i = 0; i < T; ++i)
    cin >> c[i];
  for (int i = 0; i < D; ++i) {
    for (int j = 0; j < T; ++j)
      cin >> s[i][j];
  }
}

void init() {
  for (int i = 0; i < D; ++i) {
    state.ct[i] = max_element(s[i].begin(), s[i].end()) - s[i].begin();
    state.add(i, state.ct[i]);
  }
  for (int t = 0; t < T; ++t) {
    for (int i = 2; i <= D + 1; ++i) {
      pena[t][i] = pena[t][i - 1] + c[t] * (i - 1);
    }
  }
}

int main() {
  srand(time(NULL));
  input();
  init();
  cerr << state.calc() << '\n';
  while (current < 1900) {
    current = timer.elapsed();
    state.exe();
  }
  state.output();
  cerr << state.calc() << '\n';
  return 0;
}


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

const int D = 365, T = 26;

vector<int> c(T);
vector<vector<int>> s(D, vector<int>(T));
vector<vector<int>> pena(T, vector<int>(D + 2));

class Timer {
private:
  chrono::high_resolution_clock::time_point start;

public:
  Timer() { reset(); }
  void reset() { start = chrono::high_resolution_clock::now(); }
  chrono::milliseconds::rep elapsed() {
    auto end = chrono::high_resolution_clock::now();
    return chrono::duration_cast<chrono::milliseconds>(end - start).count();
  }
} timer;

auto current = timer.elapsed();

struct State {
  vector<int> ct;
  vector<vector<int>> h;
  State() : ct(D), h(T) {
    for (int t = 0; t < T; ++t) {
      h[t].push_back(-1);
      h[t].push_back(D);
    }
  }
  void add(int d, int i) {
    auto it = lower_bound(h[i].begin(), h[i].end(), d);
    h[i].insert(it, d);
    ct[d] = i;
  }
  void rmv(int d) {
    h[ct[d]].erase(remove(h[ct[d]].begin(), h[ct[d]].end(), d), h[ct[d]].end());
  }
  int calc_each(int i) {
    int res = 0;
    for (int d = 1; d < h[i].size() - 1; ++d) {
      res += s[h[i][d]][i];
    }
    for (int d = 0; d < h[i].size() - 1; ++d) {
      res -= pena[i][h[i][d + 1] - h[i][d]];
    }
    return res;
  }
  int calc() {
    int res = 0;
    for (int t = 0; t < T; ++t)
      res += calc_each(t);
    return res;
  }
  int calc_diff(int d, int bf, int af) {
    int res = s[d][af] - s[d][bf];
    {
      auto it = lower_bound(h[bf].begin(), h[bf].end(), d);
      int r = *next(it) - d, l = d - *prev(it);
      res -= pena[bf][l + r] - pena[bf][r] - pena[bf][l];
    }
    {
      auto it = lower_bound(h[af].begin(), h[af].end(), d);
      int r = *it - d, l = d - *prev(it);
      res += pena[af][l + r] - pena[af][r] - pena[af][l];
    }
    return res;
  }
  void output() {
    for (int i = 0; i < D; ++i)
      cout << ct[i] + 1 << '\n';
  }
  void update() {
    int d = rand() % D, bf = ct[d], af = rand() % T;
    if (bf == af)
      return;
    if (calc_diff(d, bf, af) > 0) {
      rmv(d);
      add(d, af);
    }
  }
  void swap() {
    int b = rand() % 13 + 1;
    int d = rand() % (D - b);
    int n = d + b;
    int bf = ct[d], af = ct[n];
    if (calc_diff(d, bf, af) + calc_diff(n, af, bf) > 0) {
      ct[d] = af;
      ct[n] = bf;
      rmv(d);
      rmv(n);
      add(d, af);
      add(n, bf);
    }
  }
  void exe() {
    update();
    swap();
  }
};

State state;

void input() {
  int _;
  cin >> _;
  for (int i = 0; i < T; ++i)
    cin >> c[i];
  for (int i = 0; i < D; ++i) {
    for (int j = 0; j < T; ++j)
      cin >> s[i][j];
  }
}

void init() {
  for (int i = 0; i < D; ++i) {
    state.ct[i] = max_element(s[i].begin(), s[i].end()) - s[i].begin();
    state.add(i, state.ct[i]);
  }
  for (int t = 0; t < T; ++t) {
    for (int i = 2; i <= D + 1; ++i) {
      pena[t][i] = pena[t][i - 1] + c[t] * (i - 1);
    }
  }
}

int main() {
  srand(time(NULL));
  input();
  init();
  cerr << state.calc() << '\n';
  while (current < 1900) {
    current = timer.elapsed();
    state.exe();
  }
  state.output();
  cerr << state.calc() << '\n';
  return 0;
}