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

// Template
#include <bits/stdc++.h>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, r) for (int i = (l); i < (r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003;
template <typename T> inline bool chmin(T &x, const T &y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmax(T &x, const T &y) {
  if (x < y) {
    x = y;
    return 1;
  }
  return 0;
}
struct IOSET {
  IOSET() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
  }
} ioset;

constexpr int D = 365;
int c[26], d[D][26], t[D];

void input() {
  cin >> c[0];
  rep(i, 26) cin >> c[i];
  rep(i, D) rep(j, 26) cin >> d[i][j];
}
void output() { rep(i, D) cout << t[i] + 1 << "\\n"; }

// day 日目までのスコアを求める
int score(int day) {
  int ans = 0;
  int last[26];
  rep(i, 26) last[i] = -1;
  rep(i, day + 1) {
    ans += d[i][t[i]];
    rep(j, 26) ans -= c[j] * (i - last[j]);
  }
  return ans;
}

int main() {
  input();

  // 貪欲
  rep(i, D) {
    int max_score = -inf, max_id = -1;
    rep(j, 26) {
      t[i] = j;
      if (chmax(max_score, score(i)))
        max_id = j;
    }
    t[i] = max_id;
  }

  output();
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, n) for (int i = 1; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr int inf = 1000000000;
constexpr ll INF = 3000000000000000000;

constexpr int N = 18;

#include <random>

int score;
vector<int> c, t;
vector<vector<int>> s;
vector<set<int>> S1, S2;

void kaeru(int d, int q) {
  score -= s[d][t[d]];
  score += s[d][q];
  int x = *(S2[t[d]].upper_bound(-d));
  x *= (-1);
  int y = *(S1[t[d]].upper_bound(d));
  score += c[t[d]] * (d - x - 1) * (d - x) / 2;
  score += c[t[d]] * (y - d - 1) * (y - d) / 2;
  score -= c[t[d]] * (y - x - 1) * (y - x) / 2;
  x = *(S2[q].upper_bound(-d));
  x *= (-1);
  y = *(S1[q].upper_bound(d));
  score += c[q] * (y - x - 1) * (y - x) / 2;
  score -= c[q] * (d - x - 1) * (d - x) / 2;
  score -= c[q] * (y - d - 1) * (y - d) / 2;
  S1[t[d]].erase(d);
  S2[t[d]].erase(-d);
  S1[q].insert(d);
  S2[q].insert(-d);
  t[d] = q;
}

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

  int D;
  cin >> D;
  c = vector<int>(26);
  s = vector<vector<int>>(D, vector<int>(26));
  rep(i, 26) cin >> c[i];
  rep(i, D) rep(j, 26) cin >> s[i][j];

  random_device rd;
  mt19937 mt(rd());
  uniform_int_distribution<int> dist1(0, D - 1);
  uniform_int_distribution<int> dist2(0, 25);

  int x = 0, y = 0;
  rep(i, 26) {
    if (c[i] > x) {
      x = c[i];
      y = i;
    }
  }
  t = vector<int>(D, y);
  S1 = S2 = vector<set<int>>(26);
  rep(i, D) score += s[i][y];
  rep(i, D) {
    if (i != y)
      score -= (D + 1) * D / 2 * c[i];
  }
  rep(i, D) {
    S1[t[i]].insert(i);
    S2[t[i]].insert(-i);
  }
  rep(i, 26) {
    S1[i].insert(-1);
    S2[i].insert(1);
    S1[i].insert(D);
    S2[i].insert(-D);
  }

  rep(_, 200000) {
    int memo_score = score;
    vector<int> d(N), q(N), memo(N);
    rep(i, N) {
      d[i] = dist1(mt);
      q[i] = dist2(mt);
      memo[i] = t[d[i]];
    }
    rep(i, N) kaeru(d[i], q[i]);
    if (score < memo_score) {
      rep(i, N) kaeru(d[i], memo[i]);
    }
  }

  // cout << "Score: " << score << "\\n";
  rep(i, D) cout << t[i] + 1 << "\\n";

  return 0;
}