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

#include <bits/stdc++.h>
#define drep(i, cc, n) for (int i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define drrep(i, cc, n) for (int i = (cc); i >= (n); --i)
#define rrep(i, n) drrep(i, 0, n - 1)
#define sz(s) (int)(s.size())
#define vecprint(v)                                                            \\
  rep(i, v.size()) if (i == 0) cout << v[i];                                   \\
  else cout << " " << v[i];                                                    \\
  cout << endl;
#define arrprint(v, n)                                                         \\
  rep(i, n) if (i == 0) cout << v[i];                                          \\
  else cout << " " << v[i];                                                    \\
  cout << endl;
#define matprint(v, n, m)                                                      \\
  rep(i, n) {                                                                  \\
    rep(j, m) { cout << v[i][j] << " "; }                                      \\
    cout << endl;                                                              \\
  }
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
using ll = long long;
using P = pair<int, int>;
template <typename T>
using priority_queue_rev =
    priority_queue<T, vector<T>, greater<T>>; // 小さいものから取り出す

int d;
vector<int> c(26);
vector<vector<int>> s;
vector<int> t, now;
vector<int> last(26, -1);
ll best = 0;

ll score() {
  ll res = 0;
  rep(i, 26) last[i] = -1;
  rep(i, d) {
    res += s[i][now[i]];
    last[now[i]] = i;
    rep(j, 26) res -= c[j] * (i - last[j]);
  }
  return res;
}

int main() {
  random_device seed;
  mt19937 mt(seed());
  uniform_int_distribution<int> rnd1(0, 25);

  cin >> d;
  uniform_int_distribution<int> rnd2(0, d - 1);
  rep(i, 26) cin >> c[i];
  s.resize(d);
  rep(i, d) s[i].resize(26);
  rep(i, d) rep(j, 26) cin >> s[i][j];
  t.resize(d);
  now.resize(d);
  rep(i, d) {
    now[i] = i % 26;
    t[i] = now[i];
  }
  best = score();
  ll now_s;
  rep(__, 10) {
    rep(i, d) now[i] = rnd1(mt);
    now_s = score();
    if (best < now_s) {
      best = now_s;
      rep(j, d) t[j] = now[j];
    }
    rep(_, 500) {
      // cout << best << endl;
      // rep(i,d)now[i] = rnd1(mt);
      int choice = rnd2(mt);
      rep(i, 26) {
        now[choice] = i;
        now_s = score();
        if (best < now_s) {
          best = now_s;
          rep(j, d) t[j] = now[j];
        }
      }
    }
  }

  // cout << score() << endl;
  rep(i, d) cout << t[i] + 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define drep(i, cc, n) for (int i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define drrep(i, cc, n) for (int i = (cc); i >= (n); --i)
#define rrep(i, n) drrep(i, 0, n - 1)
#define sz(s) (int)(s.size())
#define vecprint(v)                                                            \\
  rep(i, v.size()) if (i == 0) cout << v[i];                                   \\
  else cout << " " << v[i];                                                    \\
  cout << endl;
#define arrprint(v, n)                                                         \\
  rep(i, n) if (i == 0) cout << v[i];                                          \\
  else cout << " " << v[i];                                                    \\
  cout << endl;
#define matprint(v, n, m)                                                      \\
  rep(i, n) {                                                                  \\
    rep(j, m) { cout << v[i][j] << " "; }                                      \\
    cout << endl;                                                              \\
  }
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
using ll = long long;
using P = pair<int, int>;
template <typename T>
using priority_queue_rev =
    priority_queue<T, vector<T>, greater<T>>; // 小さいものから取り出す

int d;
vector<int> c(26);
vector<vector<int>> s;
vector<int> t, now;
vector<int> last(26, -1);
ll best = 0;

ll score() {
  ll res = 0;
  rep(i, 26) last[i] = -1;
  rep(i, d) {
    res += s[i][now[i]];
    last[now[i]] = i;
    rep(j, 26) res -= c[j] * (i - last[j]);
  }
  return res;
}

int main() {
  random_device seed;
  mt19937 mt(seed());
  uniform_int_distribution<int> rnd1(0, 25);

  cin >> d;
  uniform_int_distribution<int> rnd2(0, d - 1);
  rep(i, 26) cin >> c[i];
  s.resize(d);
  rep(i, d) s[i].resize(26);
  rep(i, d) rep(j, 26) cin >> s[i][j];
  t.resize(d);
  now.resize(d);
  rep(i, d) {
    now[i] = i % 26;
    t[i] = now[i];
  }
  best = score();
  ll now_s;
  rep(__, 20) {
    rep(i, d) now[i] = rnd1(mt);
    now_s = score();
    if (best < now_s) {
      best = now_s;
      rep(j, d) t[j] = now[j];
    }
    rep(_, 500) {
      // cout << best << endl;
      // rep(i,d)now[i] = rnd1(mt);
      int choice = rnd2(mt);
      rep(i, 26) {
        now[choice] = i;
        now_s = score();
        if (best < now_s) {
          best = now_s;
          rep(j, d) t[j] = now[j];
        }
      }
    }
  }

  // cout << score() << endl;
  rep(i, d) cout << t[i] + 1 << endl;

  return 0;
}