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

#include <bits/stdc++.h> // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sun Jun 28 20:59:50 JST 2020
 **/

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
  return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

Int D;
std::vector<Int> c;
std::vector<std::vector<Int>> s;
constexpr Int K = 26;

Int calc_score(const std::vector<Int> &a) {
  assert(a.size() == D);
  Int ret = 0;
  Int sum = std::accumulate(c.begin(), c.end(), 0LL);
  Int cur_minus = 0;
  std::vector<Int> last(K, -1);
  REP(d, D) {
    cur_minus += sum - c[a[d]] * (d - last[a[d]]);
    ret += s[d][a[d]] - cur_minus;
    last[a[d]] = d;
  }
  return ret;
}

std::chrono::system_clock::time_point start_time;
std::vector<Int> ans(D);
Int best_score;
std::vector<Int> local_search(Int t) {
  std::vector<Int> nxt = ans;
  if (t % 10 == 0) {
    Int idx = rnd() % (D - 1);
    std::swap(nxt[idx], nxt[idx + 1]);
  } else {
    Int idx = rnd() % D;
    nxt[idx] = rnd() % K;
    return nxt;
  }
  return nxt;
}
void hill_climb() {
  std::vector<Int> cnt(2);
  REP(t, 1e9) {
    std::chrono::system_clock::time_point now =
        std::chrono::system_clock::now();
    if (std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time)
            .count() > 1700)
      break;
    // change
    auto nxt = local_search(t);
    Int nxt_score = calc_score(nxt);
    if (nxt_score >= best_score) {
      dump(t, best_score, nxt_score);
      best_score = nxt_score;
      ans = std::move(nxt);
      cnt[t % 2]++;
    }
  }
  dump(cnt);
}

signed main() {
  start_time = std::chrono::system_clock::now();
  // input
  std::cin >> D;
  c.resize(K);
  s = make_vec<Int>(D, K, 0);
  REP(i, K) std::cin >> c[i];
  REP(i, D) REP(j, K) std::cin >> s[i][j];

  // hoge
  ans.resize(D);
  REP(i, D) ans[i] = i % K;
  best_score = calc_score(ans);
  hill_climb();
  // dump(calc_score(ans));
  // REP(i, D) { ans[i] = std::max_element(s[i].begin(), s[i].end()) -
  // s[i].begin(); } dump(calc_score(ans));

  // output
  for (Int x : ans)
    std::cout << x + 1 << "\\n";
  std::cout << std::endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h> // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sun Jun 28 20:59:50 JST 2020
 **/

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());

template <class T, class S> T make_vec(S x) { return x; }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) {
  return std::vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...));
}

Int D;
std::vector<Int> c;
std::vector<std::vector<Int>> s;
constexpr Int K = 26;

Int calc_score(const std::vector<Int> &a) {
  assert(a.size() == D);
  Int ret = 0;
  Int sum = std::accumulate(c.begin(), c.end(), 0LL);
  Int cur_minus = 0;
  std::vector<Int> last(K, -1);
  REP(d, D) {
    cur_minus += sum - c[a[d]] * (d - last[a[d]]);
    ret += s[d][a[d]] - cur_minus;
    last[a[d]] = d;
  }
  return ret;
}

std::chrono::system_clock::time_point start_time;
std::vector<Int> ans(D);
Int best_score;
void hill_climb() {
  std::vector<Int> cnt(2);
  REP(t, 1e9) {
    std::chrono::system_clock::time_point now =
        std::chrono::system_clock::now();
    if (std::chrono::duration_cast<std::chrono::seconds>(now - start_time)
            .count() > 1.9)
      break;
    std::vector<Int> nxt = ans;
    // change
    if (t % 2) {
      Int idx = rnd() % (D - 1);
      std::swap(nxt[idx], nxt[idx + 1]);
    } else {
      Int idx = rnd() % D;
      nxt[idx] = rnd() % K;
    }

    Int nxt_score = calc_score(nxt);
    if (nxt_score >= best_score) {
      dump(t, best_score, nxt_score);
      best_score = nxt_score;
      ans = std::move(nxt);
      cnt[t % 2]++;
    }
  }
  dump(cnt);
}

signed main() {
  start_time = std::chrono::system_clock::now();
  // input
  std::cin >> D;
  c.resize(K);
  s = make_vec<Int>(D, K, 0);
  REP(i, K) std::cin >> c[i];
  REP(i, D) REP(j, K) std::cin >> s[i][j];

  // hoge
  ans.resize(D);
  REP(i, D) ans[i] = i % K;
  best_score = calc_score(ans);
  hill_climb();
  // dump(calc_score(ans));
  // REP(i, D) { ans[i] = std::max_element(s[i].begin(), s[i].end()) -
  // s[i].begin(); } dump(calc_score(ans));

  // output
  for (Int x : ans)
    std::cout << x + 1 << "\\n";
  std::cout << std::endl;
}