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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
constexpr int INF = 2e9;

unsigned int randxor() {
  static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
  unsigned int t;
  t = (x ^ (x << 11));
  x = y;
  y = z;
  z = w;
  return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

int main() {
  int D;
  cin >> D;
  vector<int> c(26);
  rep(i, 26) cin >> c[i];
  vector<vector<int>> s(D, vector<int>(26));
  vector<int> last_day(26, 0);
  vector<int> t(D);
  rep(i, D) rep(j, 26) cin >> s[i][j];

  int score = 0;
  int res = 0;
  auto Scoring = [&](int d, int id) {
    last_day[id] = d;
    for (int i = 0; i < 26; i++)
      score -= c[i] * (d - last_day[i]);
  };

  auto init = [&]() {
    int ret = 0;
    for (int i = 0; i < D; i++) {
      int add = -INF;
      int p = -1;
      for (int j = 0; j < 26; j++) {
        int tmp = 0;
        tmp += s[i][j];
        last_day[j] = i + 1;
        for (int k = 0; k < 26; k++)
          tmp -= c[k] * (i + 1 - last_day[k]);
        if (add < tmp) {
          add = tmp;
          p = j + 1;
        }
      }
      t[i] = p;
      ret += add;
    }
    score = ret;
  };

  // 初期解生成
  init();
  /*
  rep(p, 40000000) {

    int d = randxor() % D;
    int change_q = randxor() % 26;
    int old_q = t[d];
    t[d] = change_q;
    int old_res = res;
    int old_score = score;
    Scoring(d + 1, change_q);
    if (res < old_res) {
      t[d] = old_q;
      res = old_res;
      score = old_score;
    }
  }
  */
  // cout << max(int(1e6 + score), 0) << endl;
  rep(i, D) cout << t[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
constexpr int INF = 2e9;

unsigned int randxor() {
  static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
  unsigned int t;
  t = (x ^ (x << 11));
  x = y;
  y = z;
  z = w;
  return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

int main() {
  int D;
  cin >> D;
  vector<int> c(26);
  rep(i, 26) cin >> c[i];
  vector<vector<int>> s(D, vector<int>(26));
  vector<int> last_day(26, 0);
  vector<int> t(D);
  rep(i, D) rep(j, 26) cin >> s[i][j];

  int score = 0;
  int res = 0;
  auto Scoring = [&](int d, int id) {
    last_day[id] = d;
    for (int i = 0; i < 26; i++)
      score -= c[i] * (d - last_day[i]);
  };

  auto init = [&]() {
    res = 0;
    score = 0;
    rep(i, 26) last_day[i] = 0;
    rep(i, D) t[i] = 0;
  };
  score = -1e6;
  auto calc = [&]() {
    while (max(int(1e6 + score), 0) < 1000) {
      init();
      for (int i = 0; i < D; i++) {
        int q = randxor() % 26;
        t[i] = q;
        Scoring(i + 1, q);
      }
    }
  };

  // 計算
  calc();
  /*
  rep(p, 40000000) {

    int d = randxor() % D;
    int change_q = randxor() % 26;
    int old_q = t[d];
    t[d] = change_q;
    int old_res = res;
    int old_score = score;
    Scoring(d + 1, change_q);
    if (res < old_res) {
      t[d] = old_q;
      res = old_res;
      score = old_score;
    }
  }
  */
  cout << max(int(1e6 + score), 0) << endl;
  rep(i, D) cout << t[i] + 1 << endl;
  return 0;
}