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 rep_lr(i, l, r) for (int i = (l); i < (r); i++)
#define all(x) (x).begin(), (x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
constexpr int INF = INT_MAX >> 1;
constexpr ll LINF = 5000000000000000LL;
constexpr int MOD = 1000000007;
random_device rd;
mt19937 mt(rd());

int d;
vi c;
vvi s;

vi res;
vvi last;

vi ans, test;
ll mx = 0;

ll score(vi t, int st = 0) {

  rep(i, d) {

    ll plus = 0;
    plus += s[i][t[i]];
    rep_lr(j, st, 26) {
      if (j == t[i])
        last[i + 1][t[i]] = i + 1;
      else
        last[i + 1][j] = last[i][j];
      plus -= c[j] * ((ll)i + 1 - last[i + 1][j]);
    }

    res[i + 1] = res[i] + plus;
  }
  return res[d - 1];
}

void init() {

  c.resize(26);
  s.resize(d, vi(26));
  last.assign(d + 1, vi(26, 0));
  res.resize(d + 1, 0);
  ans.resize(d);
  test.resize(d);
}

int main() {

  cin >> d;

  double time = (double)clock() / CLOCKS_PER_SEC;

  init();
  rep(i, 26) cin >> c[i];
  rep(i, d) rep(j, 26) cin >> s[i][j];

  vi first(d);
  rep(i, d) first[i] = mt() % 26;

  rep(i, d) ans[i] = test[i] = first[i];
  mx = score(first);

  int loop = 0, rec = 0;
  while (((double)clock() / CLOCKS_PER_SEC) - time < 1.8) {

    int change = mt() % d;
    test[change] = mt() % 26;
    ll re = score(test, change);
    if (mx < re) {
      mx = re;
      rep(i, d) ans[i] = test[i];
      rec++;
    }

    loop++;
  }

  for (auto x : ans)
    cout << x + 1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep_lr(i, l, r) for (int i = (l); i < (r); i++)
#define all(x) (x).begin(), (x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
constexpr int INF = INT_MAX >> 1;
constexpr ll LINF = 5000000000000000LL;
constexpr int MOD = 1000000007;
random_device rd;
mt19937 mt(rd());

int d;
vi c;
vvi s;

vi res;
vvi last;

vi ans, test;
ll mx = 0;

ll score(vi t, int st = 0) {

  rep(i, d) {

    ll plus = 0;
    plus += s[i][t[i]];
    rep_lr(j, st, 26) {
      if (j == t[i])
        last[i + 1][t[i]] = i + 1;
      else
        last[i + 1][j] = last[i][j];
      plus -= c[j] * ((ll)i + 1 - last[i + 1][j]);
    }

    res[i + 1] = res[i] + plus;
  }
  return res[d - 1];
}

void init() {

  c.resize(26);
  s.resize(d, vi(26));
  last.assign(d + 1, vi(26, 0));
  res.resize(d + 1, 0);
  ans.resize(d);
  test.resize(d);
}

int main() {

  cin >> d;

  double time = clock() / CLOCKS_PER_SEC;

  init();
  rep(i, 26) cin >> c[i];
  rep(i, d) rep(j, 26) cin >> s[i][j];

  vi first(d);
  rep(i, d) first[i] = mt() % 26;

  rep(i, d) ans[i] = test[i] = first[i];
  mx = score(first);

  int loop = 0;
  while ((double)(clock() / CLOCKS_PER_SEC) - time < 1.8) {

    int change = mt() % d;
    test[change] = mt() % 26;
    ll re = score(test, change);
    if (mx < re) {
      mx = re;
      ans = test;
    }

    loop++;
  }

  for (auto x : ans)
    cout << x + 1 << endl;
}