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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

std::mt19937 mt{std::random_device{}()};

int randint(int lower, int upper) {
  std::uniform_int_distribution<int> dist(lower, upper);
  return dist(mt);
}

int calc_score(int M, int D, vector<vector<int>> &S, vector<int> &T,
               vector<int> &C) {
  int ans = 0;
  vector<int> last(M, -1);
  for (int i = 0; i < D; i++) {
    ans += S[i][T[i]];
    last[T[i]] = i;
    for (int j = 0; j < M; j++) {
      ans -= C[j] * (i - last[j]);
    }
  }
  return ans;
}

int main() {
  int tstart = clock();
  ios::sync_with_stdio(false);
  cin.tie(0);
  int D;
  cin >> D;
  const int M = 26;
  vector<int> C(M);
  for (int i = 0; i < M; i++)
    cin >> C[i];
  vector<vector<int>> S(D, vector<int>(M));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < M; j++) {
      cin >> S[i][j];
    }
  }
  vector<int> T(D);
  for (int i = 0; i < D; i++) {
    int naxj = 0;
    int nax = S[i][0];
    for (int j = 1; j < M; j++) {
      if (chmax(nax, S[i][j])) {
        naxj = j;
      }
    }
    T[i] = naxj;
  }
  int score = calc_score(M, D, S, T, C);
  while (1.0 * (clock() - tstart) / CLOCKS_PER_SEC < 1.8) {
    // cout << 1.0 * (clock() - tstart) / CLOCKS_PER_SEC << endl;
    int H = randint(0, D - 1);
    int Q = randint(0, M - 1);
    int tmp = T[H];
    T[H] = Q;
    int nscore = calc_score(M, D, S, T, C);
    if (score < nscore) {
      score = nscore;
    } else {
      T[H] = tmp;
    }
  }
  for (int i = 0; i < D; i++) {
    cout << T[i] + 1 << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

std::mt19937 mt{std::random_device{}()};

int randint(int lower, int upper) {
  std::uniform_int_distribution<int> dist(lower, upper);
  return dist(mt);
}

int calc_score(int M, int D, vector<vector<int>> &S, vector<int> &T,
               vector<int> &C) {
  int ans = 0;
  vector<int> last(M, -1);
  for (int i = 0; i < D; i++) {
    ans += S[i][T[i]];
    last[T[i]] = i;
    for (int j = 0; j < M; j++) {
      ans -= C[j] * (i - last[j]);
    }
  }
  return ans;
}

int main() {
  int tstart = clock();
  ios::sync_with_stdio(false);
  cin.tie(0);
  int D;
  cin >> D;
  const int M = 26;
  vector<int> C(M);
  for (int i = 0; i < M; i++)
    cin >> C[i];
  vector<vector<int>> S(D, vector<int>(M));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < M; j++) {
      cin >> S[i][j];
    }
  }
  vector<int> T(D);
  for (int i = 0; i < D; i++) {
    int naxj = 0;
    int nax = S[i][0];
    for (int j = 1; j < M; j++) {
      if (chmax(nax, S[i][j])) {
        naxj = j;
      }
    }
    T[i] = naxj;
  }
  int score = calc_score(M, D, S, T, C);
  while ((clock() - tstart) / CLOCKS_PER_SEC < 1.8) {
    int H = randint(0, D - 1);
    int Q = randint(0, M - 1);
    int tmp = T[H];
    T[H] = Q;
    int nscore = calc_score(M, D, S, T, C);
    if (score < nscore) {
      score = nscore;
    } else {
      T[H] = tmp;
    }
  }
  for (int i = 0; i < D; i++) {
    cout << T[i] + 1 << '\\n';
  }
  return 0;
}