Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<vector<int>> s;
vector<int> c, t;
vector<pair<int, int>> last;
vector<int64_t> b_score;
vector<vector<int64_t>> c_score;

int64_t calc_score() {
  b_score.assign(t.size(), 0);
  c_score.assign(t.size(), vector<int64_t>(26, 0));
  last.assign(26, {0, 0});
  for (auto i = 0; i < (int)t.size(); i++) {
    if (i != 0) {
      b_score[i] = b_score[i - 1];
      c_score[i] = c_score[i - 1];
    }
    b_score[i] += s[i][t[i]];
    last[t[i]] = {i + 1, last[t[i]].first};
    for (auto j = 0; j < 26; j++) {
      c_score[i][j] -= c[j] * (i + 1 - last[j].first);
    }
  }
  return accumulate(c_score[(int)t.size() - 1].begin(),
                    c_score[(int)t.size() - 1].end(), b_score.back());
}

int64_t calc_score(pair<int, int> q) {
  last.assign(26, {0, 0});
  for (auto i = 0; i < (int)t.size(); i++) {
    if (i != 0) {
      b_score[i] = b_score[i - 1];
      c_score[i][q.first] = c_score[i - 1][q.first];
      c_score[i][q.second] = c_score[i - 1][q.second];
    } else {
      b_score[i] = 0;
      c_score[i][q.first] = 0;
      c_score[i][q.second] = 0;
    }
    last[t[i]] = {i + 1, last[t[i]].first};
    b_score[i] += s[i][t[i]];
    c_score[i][q.first] -= c[q.first] * (i + 1 - last[q.first].first);
    c_score[i][q.second] -= c[q.second] * (i + 1 - last[q.second].first);
  }
  return accumulate(c_score[(int)t.size() - 1].begin(),
                    c_score[(int)t.size() - 1].end(), b_score.back());
}

int main() {
  int d;
  cin >> d;
  c.resize(26);
  for (auto i = 0; i < 26; i++)
    cin >> c[i];
  s.assign(d, vector<int>(26));
  for (auto i = 0; i < d; i++) {
    for (auto j = 0; j < 26; j++)
      cin >> s[i][j];
  }

  int64_t ans = 0;
  int index = 0;
  for (auto i = 0; i < d; i++) {
    t.resize(i + 1);
    for (auto j = 0; j < 26; j++) {
      t[i] = j;
      auto tmp = calc_score();
      if (tmp > ans) {
        ans = tmp;
        index = j;
      }
    }
    t[i] = index;
  }

  auto now = chrono::system_clock::now();
  auto end = chrono::system_clock::now() + chrono::milliseconds(1850);
  random_device seed;
  default_random_engine engine(seed());
  uniform_int_distribution<> day_dist(0, d - 1);
  uniform_int_distribution<> contest_dist(0, 25);
  int64_t score = calc_score();
  while (now < end) {
    int day = day_dist(engine);
    int contest = contest_dist(engine);
    int prev = t[day];
    t[day] = contest;
    auto tmp = calc_score(make_pair(prev, contest));
    if (score < tmp)
      score = tmp;
    else {
      t[day] = prev;
      calc_score(make_pair(contest, prev));
    }
    now = chrono::system_clock::now();
  }

  for (auto &elm : t)
    cout << elm + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

vector<vector<int>> s;
vector<int> c, t;
vector<pair<int, int>> last;
vector<int64_t> b_score;
vector<vector<int64_t>> c_score;

int64_t calc_score() {
  b_score.assign(t.size(), 0);
  c_score.assign(t.size(), vector<int64_t>(26, 0));
  last.assign(26, {0, 0});
  for (auto i = 0; i < (int)t.size(); i++) {
    if (i != 0) {
      b_score[i] = b_score[i - 1];
      c_score[i] = c_score[i - 1];
    }
    b_score[i] += s[i][t[i]];
    last[t[i]] = {i + 1, last[t[i]].first};
    for (auto j = 0; j < 26; j++) {
      c_score[i][j] -= c[j] * (i + 1 - last[j].first);
    }
  }
  return accumulate(c_score[(int)t.size() - 1].begin(),
                    c_score[(int)t.size() - 1].end(), b_score.back());
}

int64_t calc_score(pair<int, int> q) {
  last.assign(26, {0, 0});
  for (auto i = 0; i < (int)t.size(); i++) {
    if (i != 0) {
      b_score[i] = b_score[i - 1];
      c_score[i][q.first] = c_score[i - 1][q.first];
      c_score[i][q.second] = c_score[i - 1][q.second];
    } else {
      b_score[i] = 0;
      c_score[i][q.first] = 0;
      c_score[i][q.second] = 0;
    }
    last[t[i]] = {i + 1, last[t[i]].first};
    b_score[i] += s[i][t[i]];
    c_score[i][q.first] -= c[q.first] * (i + 1 - last[q.first].first);
    c_score[i][q.second] -= c[q.second] * (i + 1 - last[q.second].first);
  }
  return accumulate(c_score[(int)t.size() - 1].begin(),
                    c_score[(int)t.size() - 1].end(), b_score.back());
}

int main() {
  int d;
  cin >> d;

  c.resize(26);
  for (auto i = 0; i < 26; i++)
    cin >> c[i];

  s.assign(d, vector<int>(26));
  for (auto i = 0; i < d; i++) {
    for (auto j = 0; j < 26; j++)
      cin >> s[i][j];
  }

  int64_t ans = 0;
  int index = 0;
  for (auto i = 0; i < d; i++) {
    t.resize(i + 1);
    for (auto j = 0; j < 26; j++) {
      t[i] = j;
      auto tmp = calc_score();
      if (tmp > ans) {
        ans = tmp;
        index = j;
      }
    }
    t[i] = index;
  }

  auto now = chrono::system_clock::now();
  auto end = chrono::system_clock::now() + chrono::milliseconds(1900);
  random_device seed;
  default_random_engine engine(seed());
  uniform_int_distribution<> day_dist(0, d - 1);
  uniform_int_distribution<> contest_dist(0, 25);
  int64_t score = calc_score();
  while (now < end) {
    int day = day_dist(engine);
    int contest = contest_dist(engine);
    int prev = t[day];
    t[day] = contest;
    auto tmp = calc_score(make_pair(prev, contest));
    if (score < tmp)
      score = tmp;
    else {
      t[day] = prev;
      calc_score(make_pair(contest, prev));
    }
    now = chrono::system_clock::now();
  }

  for (auto &elm : t)
    cout << elm + 1 << endl;
  return 0;
}