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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
using P = pair<int, int>;
using lint = long long;
using ll = long long;
const lint inf = 1e18 + 7;
const int MOD = 1000000007;
int d;
int c[30] = {};
int s[370][30];
int num = 100000;
int calc(vector<int> const &t, vector<vector<int>> &last_list) {
  int res = 0;
  vector<int> last(30, 0);
  rep1(i, d) {
    res += s[i][t[i]];
    last[t[i]] = i;
    last_list[i] = last;
    int buf = 0;
    rep1(j, 26) { buf += c[j] * (i - last[j]); }
    res -= buf;
    // cout<<res<<"\\n";
  }
  return res;
}
int change(int nd, int nq, int &res, vector<int> &t,
           vector<vector<int>> &last_list) {
  int flag_t = false;
  int flag_q = false;
  int x = t[nd];
  t[nd] = 0;
  res += s[nd][nq];
  res -= s[nd][x];
  for (int i = nd; i <= d; ++i) {
    if (flag_t && flag_q)
      break;
    if (t[i] == x || flag_t)
      flag_t = true;
    else {
      res += c[x] * (i - last_list[i][x]);
      last_list[i][x] = last_list[i - 1][x];
      res -= c[x] * (i - last_list[i][x]);
    }
    if (t[i] == nq || flag_q)
      flag_q = true;
    else {
      res += c[nq] * (i - last_list[i][nq]);
      last_list[i][nq] = nd;
      res -= c[nq] * (i - last_list[i][nq]);
    }
  }
  t[nd] = nq;
  return res;
}
vector<int> greedy(int d) {
  vector<int> res(d + 1, 0);
  vector<int> last(30, 0);
  rep1(i, d) {
    int mx = -inf;
    int mx_ind = 0;
    rep1(j, 26) {
      int tmp = s[i][j];
      rep1(k, 26) {
        if (k == j)
          continue;
        tmp -= c[k] * (i - last[k]);
      }
      if (mx < tmp) {
        mx = tmp;
        mx_ind = j;
      }
    }
    last[mx_ind] = i;
    res[i] = mx_ind;
  }
  return res;
}
signed main() {
  cin >> d;
  rep1(i, 26) cin >> c[i];
  rep1(i, d) { rep1(j, 26) cin >> s[i][j]; }
  vector<int> t = greedy(d);
  vector<vector<int>> last_list(d + 1, vector<int>(30, 0));
  int res = calc(t, last_list);
  // cout<<res<<"\\n";
  int param = 2;
  rep(xx, num) {
    random_device rand;
    mt19937 mt(rand());
    vector<int> nd(param);
    rep(i, param) nd[i] = mt() % d + 1;
    vector<int> nq(param);
    rep(i, param) nq[i] = mt() % 26 + 1;
    vector<int> bq(param);
    rep(i, param) bq[i] = t[nd[i]];
    int bval = res;
    rep(i, param) { change(nd[i], nq[i], res, t, last_list); }
    int yakinamasi = mt() % 100;
    // if(yakinamasi<10)continue;
    if (bval > res) {
      rep(i, param) { change(nd[i], bq[i], res, t, last_list); }
    }
  }
  rep(xx, num) {
    random_device rnd;
    mt19937 mt(rnd());
    int nd = mt() % d + 1;
    int nq = mt() % 26 + 1;
    int bq = t[nd];
    int bval = res;
    int hoge = change(nd, nq, res, t, last_list);
    if (hoge < bval) {
      change(nd, bq, res, t, last_list);
    }
  }
  rep1(i, d) { cout << t[i] << "\\n"; }
  // cout<<res<<"\\n";
  return 0;
}
/*
int m;cin>>m;
  vector<int> bres(m);
  rep(i,m){
    int d,q;cin>>d>>q;
    bres[i]=change(d,q,res,t,last_list);
  }
  rep(i,m){
    cout<<bres[i]<<"\\n";
  }
  */

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
using P = pair<int, int>;
using lint = long long;
using ll = long long;
const lint inf = 1e18 + 7;
const int MOD = 1000000007;
int d;
int c[30] = {};
int s[370][30];
int num = 170000;
int calc(vector<int> const &t, vector<vector<int>> &last_list) {
  int res = 0;
  vector<int> last(30, 0);
  rep1(i, d) {
    res += s[i][t[i]];
    last[t[i]] = i;
    last_list[i] = last;
    int buf = 0;
    rep1(j, 26) { buf += c[j] * (i - last[j]); }
    res -= buf;
    // cout<<res<<"\\n";
  }
  return res;
}
int change(int nd, int nq, int &res, vector<int> &t,
           vector<vector<int>> &last_list) {
  int flag_t = false;
  int flag_q = false;
  int x = t[nd];
  t[nd] = 0;
  res += s[nd][nq];
  res -= s[nd][x];
  for (int i = nd; i <= d; ++i) {
    if (flag_t && flag_q)
      break;
    if (t[i] == x || flag_t)
      flag_t = true;
    else {
      res += c[x] * (i - last_list[i][x]);
      last_list[i][x] = last_list[i - 1][x];
      res -= c[x] * (i - last_list[i][x]);
    }
    if (t[i] == nq || flag_q)
      flag_q = true;
    else {
      res += c[nq] * (i - last_list[i][nq]);
      last_list[i][nq] = nd;
      res -= c[nq] * (i - last_list[i][nq]);
    }
  }
  t[nd] = nq;
  return res;
}
vector<int> greedy(int d) {
  vector<int> res(d + 1, 0);
  vector<int> last(30, 0);
  rep1(i, d) {
    int mx = -inf;
    int mx_ind = 0;
    rep1(j, 26) {
      int tmp = s[i][j];
      rep1(k, 26) {
        if (k == j)
          continue;
        tmp -= c[k] * (i - last[k]);
      }
      if (mx < tmp) {
        mx = tmp;
        mx_ind = j;
      }
    }
    last[mx_ind] = i;
    res[i] = mx_ind;
  }
  return res;
}
signed main() {
  cin >> d;
  rep1(i, 26) cin >> c[i];
  rep1(i, d) { rep1(j, 26) cin >> s[i][j]; }
  vector<int> t = greedy(d);
  vector<vector<int>> last_list(d + 1, vector<int>(30, 0));
  int res = calc(t, last_list);
  // cout<<res<<"\\n";
  int param = 3;
  rep(xx, num) {
    random_device rand;
    mt19937 mt(rand());
    vector<int> nd(param);
    rep(i, param) nd[i] = mt() % d + 1;
    vector<int> nq(param);
    rep(i, param) nq[i] = mt() % 26 + 1;
    vector<int> bq(param);
    rep(i, param) bq[i] = t[nd[i]];
    int bval = res;
    rep(i, param) { change(nd[i], nq[i], res, t, last_list); }
    int yakinamasi = mt() % 100;
    // if(yakinamasi<10)continue;
    if (bval > res) {
      rep(i, param) { change(nd[i], bq[i], res, t, last_list); }
    }
  }
  rep(xx, num) {
    random_device rnd;
    mt19937 mt(rnd());
    int nd = mt() % d + 1;
    int nq = mt() % 26 + 1;
    int bq = t[nd];
    int bval = res;
    int hoge = change(nd, nq, res, t, last_list);
    if (hoge < bval) {
      change(nd, bq, res, t, last_list);
    }
  }
  rep1(i, d) { cout << t[i] << "\\n"; }
  // cout<<res<<"\\n";
  return 0;
}
/*
int m;cin>>m;
  vector<int> bres(m);
  rep(i,m){
    int d,q;cin>>d>>q;
    bres[i]=change(d,q,res,t,last_list);
  }
  rep(i,m){
    cout<<bres[i]<<"\\n";
  }
  */