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

#define _GLIBCXX_DEBUG
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
#define rep0(TMS) for (int CNT = 0; CNT < (int)(TMS); CNT++)
#define rep(CNT, GOAL) for (int CNT = 0; CNT < (int)(GOAL); CNT++)
#define rep2(CNT, START, GOAL)                                                 \\
  for (int CNT = (int)(START); CNT < (int)(GOAL); CNT++)
#define rep3(CNT, START, GOAL)                                                 \\
  for (int CNT = (int)(START); CNT > (int)(GOAL); CNT--)
#define all(CONT) begin(CONT), end(CONT)
#define itrep(ITR, CONT) for (auto ITR = begin(CONT); ITR != end(CONT); ITR++)
#define itrep1(ITR, CONT)                                                      \\
  for (auto ITR = next(begin(CONT)); ITR != end(CONT); ITR++)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void prec(const int &DIG) {
  cerr << fixed << setprecision(DIG);
  cout << fixed << setprecision(DIG);
}
template <typename T> void CERR(const T &ELEM) { cerr << ELEM; }
template <typename T, typename... Ts>
void CERR(const T &FIRST, const Ts &...REST) {
  CERR(FIRST);
  cerr << ", ";
  CERR(REST...);
}
template <typename T1, typename T2> void CERR(const pair<T1, T2> &PAIR) {
  cerr << "(";
  CERR(PAIR.first);
  cerr << ", ";
  CERR(PAIR.second);
  cerr << ")";
}
template <typename T> void CERR(const vector<T> &VEC) {
  cerr << "{ ";
  itrep(ITR, VEC) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
template <typename T> void CERR1(const vector<T> &VEC) {
  cerr << "{ ";
  itrep1(ITR, VEC) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
template <typename T> void CERR(const set<T> &SET) {
  cerr << "{ ";
  itrep(ITR, SET) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
template <typename T> void CERR(const multiset<T> &MULTISET) {
  cerr << "{ ";
  itrep(ITR, MULTISET) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
template <typename T1, typename T2> void CERR(const map<T1, T2> &MAP) {
  cerr << "{ ";
  itrep(ITR, MAP) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
#define db(OBJ)                                                                \\
  cerr << #OBJ << ": ";                                                        \\
  CERR(OBJ);                                                                   \\
  cerr << ", "
#define dbl(OBJ)                                                               \\
  cerr << #OBJ << ": ";                                                        \\
  CERR(OBJ);                                                                   \\
  cerr << "\\n"
#define db1(OBJ)                                                               \\
  cerr << #OBJ << ": ";                                                        \\
  CERR1(OBJ);                                                                  \\
  cerr << "\\n"
#define dbs(...)                                                               \\
  cerr << "(" << #__VA_ARGS__ << "): (";                                       \\
  CERR(__VA_ARGS__);                                                           \\
  cerr << ")\\n"
#define dbvv(VV)                                                               \\
  cerr << #VV << ": {\\n";                                                      \\
  rep(CNT, VV.size()) {                                                        \\
    cerr << #VV << "[" << CNT << "]: ";                                        \\
    CERR(VV[CNT]);                                                             \\
    cerr << ",\\n";                                                             \\
  }                                                                            \\
  cerr << "}\\n"
#define db01(VV)                                                               \\
  cerr << #VV << ": {\\n";                                                      \\
  rep(CNT, VV.size()) {                                                        \\
    cerr << #VV << "[" << CNT << "]: ";                                        \\
    CERR1(VV[CNT]);                                                            \\
    cerr << ",\\n";                                                             \\
  }                                                                            \\
  cerr << "}\\n"
#define db10(VV)                                                               \\
  cerr << #VV << ": {\\n";                                                      \\
  rep2(CNT, 1, VV.size()) {                                                    \\
    cerr << #VV << "[" << CNT << "]: ";                                        \\
    CERR(VV[CNT]);                                                             \\
    cerr << ",\\n";                                                             \\
  }                                                                            \\
  cerr << "}\\n"
#define db11(VV)                                                               \\
  cerr << #VV << ": {\\n";                                                      \\
  rep2(CNT, 1, VV.size()) {                                                    \\
    cerr << #VV << "[" << CNT << "]: ";                                        \\
    CERR1(VV[CNT]);                                                            \\
    cerr << ",\\n";                                                             \\
  }                                                                            \\
  cerr << "}\\n"
#define YN(FLG) cout << (FLG ? "YES" : "NO") << "\\n"
#define Yn(FLG) cout << (FLG ? "Yes" : "No") << "\\n"
#define yn(FLG) cout << (FLG ? "yes" : "no") << "\\n"
// const ll INF = 1'000'000'000'000'000'007;
const int INF = 1'000'000'007;
const ll MOD = 1'000'000'007; // 998'244'353;

int T1;
// int T = 150000;
int D;
const int N = 26; // コンテストのタイプの数
vi c(N + 1);
vvi s;

int score_max = -INF;
vi t;

int score(vi &t) {
  int score = 0;
  vi last = vi(N + 1);
  rep2(d, 1, D + 1) {
    last[t[d]] = d;
    score += s[d][t[d]];
    rep2(i, 1, N + 1) score -= c[i] * (d - last[i]);
  }
  return score;
}

void trial1() {
  int d = rand() % D + 1, q = rand() % N + 1;
  // dbs(d, q);
  vi u = t;
  u[d] = q;
  if (chmax(score_max, score(u))) {
    t[d] = q;
    // dbl(t);
    // dbl(score_max);
  }
  // dbl(t); dbl(score_max);
}

void trial2() {
  int d = rand() % (D - 1) + 1;
  vi u = t;
  swap(u[d], u[d + 1]);
  if (chmax(score_max, score(u))) {
    swap(t[d], t[d + 1]);
  }
}

void trial() {
  if (rand() % 2 == 0)
    trial1();
  else
    trial2();
}

void solve() {
  // rep0(T) trial();
  while (true) {
    trial();
    // cout << clock() << endl;
    if (clock() - T1 > 1.98 * CLOCKS_PER_SEC)
      break;
  }
  rep2(d, 1, D + 1) cout << t[d] << endl;
}

void init() {
  t = vi(D + 1, 0);
  chmax(score_max, score(t));
  rep2(d, 1, D + 1) {
    vi u = t;
    rep2(i, 1, N + 1) {
      u[d] = i;
      if (chmax(score_max, score(u))) {
        t[d] = i;
      }
    }
  }
  // dbl(t);
  // dbl(score_max);
  // cerr << "init end\\n";
}

int main() {
  T1 = clock();
  // 日数dは1-ind, コンテストのタイプiは1-ind
  cin >> D;
  rep2(i, 1, N + 1) cin >> c[i];
  s = vvi(D + 1, vi(N + 1));
  rep2(d, 1, D + 1) rep2(i, 1, N + 1) cin >> s[d][i];
  init();
  // db1(c); db01(s);
  solve();
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
#define rep0(TMS) for (int CNT = 0; CNT < (int)(TMS); CNT++)
#define rep(CNT, GOAL) for (int CNT = 0; CNT < (int)(GOAL); CNT++)
#define rep2(CNT, START, GOAL)                                                 \\
  for (int CNT = (int)(START); CNT < (int)(GOAL); CNT++)
#define rep3(CNT, START, GOAL)                                                 \\
  for (int CNT = (int)(START); CNT > (int)(GOAL); CNT--)
#define all(CONT) begin(CONT), end(CONT)
#define itrep(ITR, CONT) for (auto ITR = begin(CONT); ITR != end(CONT); ITR++)
#define itrep1(ITR, CONT)                                                      \\
  for (auto ITR = next(begin(CONT)); ITR != end(CONT); ITR++)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void prec(const int &DIG) {
  cerr << fixed << setprecision(DIG);
  cout << fixed << setprecision(DIG);
}
template <typename T> void CERR(const T &ELEM) { cerr << ELEM; }
template <typename T, typename... Ts>
void CERR(const T &FIRST, const Ts &...REST) {
  CERR(FIRST);
  cerr << ", ";
  CERR(REST...);
}
template <typename T1, typename T2> void CERR(const pair<T1, T2> &PAIR) {
  cerr << "(";
  CERR(PAIR.first);
  cerr << ", ";
  CERR(PAIR.second);
  cerr << ")";
}
template <typename T> void CERR(const vector<T> &VEC) {
  cerr << "{ ";
  itrep(ITR, VEC) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
template <typename T> void CERR1(const vector<T> &VEC) {
  cerr << "{ ";
  itrep1(ITR, VEC) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
template <typename T> void CERR(const set<T> &SET) {
  cerr << "{ ";
  itrep(ITR, SET) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
template <typename T> void CERR(const multiset<T> &MULTISET) {
  cerr << "{ ";
  itrep(ITR, MULTISET) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
template <typename T1, typename T2> void CERR(const map<T1, T2> &MAP) {
  cerr << "{ ";
  itrep(ITR, MAP) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
#define db(OBJ)                                                                \\
  cerr << #OBJ << ": ";                                                        \\
  CERR(OBJ);                                                                   \\
  cerr << ", "
#define dbl(OBJ)                                                               \\
  cerr << #OBJ << ": ";                                                        \\
  CERR(OBJ);                                                                   \\
  cerr << "\\n"
#define db1(OBJ)                                                               \\
  cerr << #OBJ << ": ";                                                        \\
  CERR1(OBJ);                                                                  \\
  cerr << "\\n"
#define dbs(...)                                                               \\
  cerr << "(" << #__VA_ARGS__ << "): (";                                       \\
  CERR(__VA_ARGS__);                                                           \\
  cerr << ")\\n"
#define dbvv(VV)                                                               \\
  cerr << #VV << ": {\\n";                                                      \\
  rep(CNT, VV.size()) {                                                        \\
    cerr << #VV << "[" << CNT << "]: ";                                        \\
    CERR(VV[CNT]);                                                             \\
    cerr << ",\\n";                                                             \\
  }                                                                            \\
  cerr << "}\\n"
#define db01(VV)                                                               \\
  cerr << #VV << ": {\\n";                                                      \\
  rep(CNT, VV.size()) {                                                        \\
    cerr << #VV << "[" << CNT << "]: ";                                        \\
    CERR1(VV[CNT]);                                                            \\
    cerr << ",\\n";                                                             \\
  }                                                                            \\
  cerr << "}\\n"
#define db10(VV)                                                               \\
  cerr << #VV << ": {\\n";                                                      \\
  rep2(CNT, 1, VV.size()) {                                                    \\
    cerr << #VV << "[" << CNT << "]: ";                                        \\
    CERR(VV[CNT]);                                                             \\
    cerr << ",\\n";                                                             \\
  }                                                                            \\
  cerr << "}\\n"
#define db11(VV)                                                               \\
  cerr << #VV << ": {\\n";                                                      \\
  rep2(CNT, 1, VV.size()) {                                                    \\
    cerr << #VV << "[" << CNT << "]: ";                                        \\
    CERR1(VV[CNT]);                                                            \\
    cerr << ",\\n";                                                             \\
  }                                                                            \\
  cerr << "}\\n"
#define YN(FLG) cout << (FLG ? "YES" : "NO") << "\\n"
#define Yn(FLG) cout << (FLG ? "Yes" : "No") << "\\n"
#define yn(FLG) cout << (FLG ? "yes" : "no") << "\\n"
// const ll INF = 1'000'000'000'000'000'007;
const int INF = 1'000'000'007;
const ll MOD = 1'000'000'007; // 998'244'353;

int T1;
// int T = 150000;
int D;
const int N = 26; // コンテストのタイプの数
vi c(N + 1);
vvi s;

int score_max = -INF;
vi t;

int score(vi &t) {
  int score = 0;
  vi last = vi(N + 1);
  rep2(d, 1, D + 1) {
    last[t[d]] = d;
    score += s[d][t[d]];
    rep2(i, 1, N + 1) score -= c[i] * (d - last[i]);
  }
  return score;
}

int score(vi &t, int k) {
  int score = 0;
  vi last = vi(N + 1);
  rep2(d, 1, D + 1) {
    last[t[d]] = d;
    score += s[d][t[d]];
    rep2(i, 1, N + 1) score -= c[i] * (d - last[i] + k);
  }
  return score;
}

void trial() {
  int d = rand() % D + 1, q = rand() % N + 1;
  // dbs(d, q);
  vi u = t;
  u[d] = q;
  if (chmax(score_max, score(u))) {
    t[d] = q;
    // dbl(t);
    // dbl(score_max);
  }
  // dbl(t); dbl(score_max);
}

void solve() {
  // rep0(T) trial();
  while (false) {
    trial();
    // cout << clock() << endl;
    if (clock() - T1 > 1.95 * CLOCKS_PER_SEC)
      break;
  }
  rep2(d, 1, D + 1) cout << t[d] << endl;
}

void init() {
  t = vi(D + 1, 0);
  chmax(score_max, score(t));
  rep2(d, 1, D + 1) {
    vi u = t;
    rep2(i, 1, N + 1) {
      u[d] = i;
      if (chmax(score_max, score(u))) {
        t[d] = i;
      }
    }
  }
  // dbl(t);
  // dbl(score_max);
  // cerr << "init end\\n";
}

void greedies() {
  rep(k, N + 1) {
    vi v(D + 1, 0);
    int score_max_k = score(v, k);
    rep2(d, 1, D + 1) {
      vi u = v;
      rep2(i, 1, N + 1) {
        u[d] = i;
        if (chmax(score_max_k, score(u, k)))
          v[d] = i;
      }
    }
    if (chmax(score_max, score_max_k))
      t = v;
  }
}

int main() {
  T1 = clock();
  // 日数dは1-ind, コンテストのタイプiは1-ind
  cin >> D;
  rep2(i, 1, N + 1) cin >> c[i];
  s = vvi(D + 1, vi(N + 1));
  rep2(d, 1, D + 1) rep2(i, 1, N + 1) cin >> s[d][i];
  // init();
  greedies();
  // db1(c); db01(s);
  solve();
}