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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
constexpr double eps = 1e-8;
const double pi = acos(-1);
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int Random(int mi, int ma) {
  random_device rnd;
  mt19937 mt(rnd()); // 32bit
  //[mi,ma]
  uniform_int_distribution<int> engine(mi, ma);
  return engine(mt);
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % mod;
  int memo = modpow(a, b / 2);
  return memo * memo % mod;
}
vector<int> kaijo, invkaijo;
void init_fact(int n) {
  kaijo.resize(n + 1);
  invkaijo.resize(n + 1);
  kaijo[0] = 1;
  for (int i = 1; i <= n; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= mod;
  }
  rep(i, n + 1) invkaijo[i] = modpow(kaijo[i], mod - 2);
}
int comb(int a, int b) {
  if (a < b)
    return 0;
  if (a < 0 || b < 0)
    return 0;
  return kaijo[a] * invkaijo[a - b] % mod * invkaijo[b] % mod;
}
int inv(int x) {
  x = modpow(x, mod - 2);
  return x;
}
int d, c[26], s[400][26], t[400];
int toscore(int dd, int q) {
  int score = 0;
  int from = t[dd], to = q;
  score -= s[dd][from], score += s[dd][to];
  int r = d, l = -1;
  rep(i, d) {
    if (t[i] == to) {
      if (i > dd) {
        r = i;
        break;
      }
      l = i;
    }
  }
  score += c[to] * (r - dd) * (dd - l);
  l = -1, r = d;
  rep(i, d) {
    if (t[i] == from) {
      if (i > dd) {
        r = i;
        break;
      }
      if (i != dd)
        l = i;
    }
  }
  score -= c[from] * (r - dd) * (dd - l);
  return score;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) {
    rep(j, 26) { cin >> s[i][j]; }
  }
  int score = 0;
  vector<pair<int, pair<vector<int>, vector<int>>>> last = {
      {0, {vector<int>(26, -1), {}}}, {0, {vector<int>(26, -1), {}}}};
  rep(i, d) {
    vector<pair<P, int>> ma = {{{-INF, -1}, -1}, {{-INF, -1}, -1}};
    rep(j, 26) {
      rep(l, 2) {
        int score = s[i][j];
        int memo = last[l].se.fi[j];
        last[l].se.fi[j] = i;
        rep(k, 26) score -= c[k] * (i - last[l].se.fi[k]);
        last[l].se.fi[j] = memo;
        ma.pb({{score, j}, l});
        sort(all(ma), greater<>());
        ma.resize(2);
      }
    }
    vector<pair<int, pair<vector<int>, vector<int>>>> next;
    for (auto p : ma) {
      vector<int> v = last[p.se].se.fi;
      vector<int> u = last[p.se].se.se;
      v[p.fi.se] = i;
      u.pb(p.fi.se);
      next.pb({last[p.se].fi + p.fi.fi, {v, u}});
    }
    sort(all(next));
    last = next;
  }
  rep(i, d) t[i] = last[0].se.se[i];
  score = 0;
  int Last[26];
  fill(Last, Last + 26, -1);
  rep(i, d) {
    score += s[i][t[i]];
    Last[t[i]] = i;
    rep(j, 26) score -= c[j] * (i - Last[j]);
  }
  rep(_, 100000) {
    int dd = Random(0, d - 1), q = -1;
    while (1) {
      int R = Random(0, 25);
      if (R != t[dd]) {
        q = R;
        break;
      }
    }
    int to = toscore(dd, q);
    if (to > 0) {
      score += to;
      t[dd] = q;
    }
  }
  rep(i, d) cout << t[i] + 1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
int Random(int mi, int ma) {
  random_device rnd;
  mt19937 mt(rnd()); // 32bit
  //[mi,ma]
  uniform_int_distribution<int> engine(mi, ma);
  return engine(mt);
}
int d, c[26], s[400][26], t[400];
set<int> st[26];
int toscore(int dd, int q) {
  int from = t[dd], to = q;
  int score = s[d][to] - s[d][from];
  auto it = st[from].upper_bound(dd);
  int r = *it;
  it = st[from].lower_bound(dd);
  it--;
  int l = *it;
  score -= (r - dd) * (dd - l);
  it = st[to].upper_bound(dd);
  r = *it;
  it = st[to].lower_bound(dd);
  it--;
  l = *it;
  score += (r - dd) * (dd - l);
  return score;
}
int score = 0;
void init() {
  vector<pair<int, pair<vector<int>, vector<int>>>> last = {
      {0, {vector<int>(26, -1), {}}}, {0, {vector<int>(26, -1), {}}}};
  rep(i, d) {
    vector<pair<P, int>> ma = {{{-INF, -1}, -1}, {{-INF, -1}, -1}};
    rep(j, 26) {
      rep(l, 2) {
        int score = s[i][j];
        int memo = last[l].se.fi[j];
        last[l].se.fi[j] = i;
        rep(k, 26) score -= c[k] * (i - last[l].se.fi[k]);
        last[l].se.fi[j] = memo;
        ma.pb({{score, j}, l});
        sort(all(ma), greater<>());
        ma.resize(2);
      }
    }
    vector<pair<int, pair<vector<int>, vector<int>>>> next;
    for (auto p : ma) {
      vector<int> v = last[p.se].se.fi;
      vector<int> u = last[p.se].se.se;
      v[p.fi.se] = i;
      u.pb(p.fi.se);
      next.pb({last[p.se].fi + p.fi.fi, {v, u}});
    }
    sort(all(next));
    last = next;
  }
  rep(i, d) {
    t[i] = last[0].se.se[i];
    st[t[i]].insert(i);
  }
  rep(i, 26) {
    st[i].insert(-1);
    st[i].insert(d);
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) {
    rep(j, 26) { cin >> s[i][j]; }
  }
  init();
  int score = 0, last[26];
  fill(last, last + 26, -1);
  rep(i, d) {
    score += s[i][t[i]];
    last[t[i]] = i;
    rep(j, 26) score -= c[j] * (i - last[j]);
  }
  rep(_, 200000) {
    int dd = Random(0, d - 1), q = Random(0, 24);
    if (t[dd] <= q)
      q++;
    int to = toscore(dd, q);
    if (to > 0) {
      score += to;
      int from = t[dd], to = q;
      t[dd] = to;
      st[from].erase(dd);
      st[to].insert(dd);
    }
  }
  rep(i, d) cout << t[i] + 1 << endl;
  return 0;
}