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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  chrono::system_clock::time_point start;
  start = chrono::system_clock::now();

  ios::sync_with_stdio(false);
  cin.tie(0);

  const int INF = 1000000000;

  int d;
  cin >> d;
  vector<int> c(26);
  vector<vector<int>> s(d, vector<int>(26));
  for (int i = 0; i < 26; ++i)
    cin >> c[i];
  for (int j = 0; j < d; ++j)
    for (int i = 0; i < 26; ++i)
      cin >> s[j][i];

  int m;
  vector<int> t(d);
  vector<vector<int>> blank(d, vector<int>(26, 1));
  for (int j = 0; j < d; ++j) {
    for (int i = 0; i < 26; ++i)
      if (j != 0)
        blank[j][i] = blank[j - 1][i] + 1;
    m = -INF;
    for (int i = 0; i < 26; ++i) {
      if (s[j][i] + c[i] * blank[j][i] > m) {
        t[j] = i;
        m = s[j][i] + c[i] * blank[j][i];
      }
    }
    blank[j][t[j]] = 0;
  }

  int d_, q, dt, diff = 0, m2;
  vector<int> t_ = t;
  vector<vector<int>> blank_ = blank;
  srand((unsigned int)time(NULL));
  while (chrono::duration_cast<chrono::seconds>(chrono::system_clock::now() -
                                                start)
             .count() < 0.1) {
    d_ = rand() % d;
    m2 = -INF;
    for (int i = 0; i < 26; ++i) {
      if (s[d_][i] > m2) {
        m2 = s[d_][i];
        q = i;
      }
    }
    // q = rand() % 26;

    diff = 0;

    diff -= s[d_][t_[d_]];
    diff += s[d_][q];

    if (d_ != 0)
      blank_[d_][t_[d_]] = blank_[d_ - 1][t_[d_]] + 1;
    else
      blank_[d_][t_[d_]] = 1;
    diff -= c[t_[d_]] * blank_[d_][t_[d_]];
    dt = d_ + 1;
    while (t_[dt] != t_[d_] && dt < d) {
      diff += c[t_[d_]] * blank_[dt][t_[d_]];
      blank_[dt][t_[d_]] = blank_[dt - 1][t_[d_]] + 1;
      diff -= c[t_[d_]] * blank_[dt][t_[d_]];
      dt++;
    }

    diff += c[q] * blank_[d_][q];
    blank_[d_][q] = 0;
    dt = d_ + 1;
    while (t_[dt] != q && dt < d) {
      diff += c[q] * blank_[dt][q];
      blank_[dt][q] = blank_[dt - 1][q] + 1;
      diff -= c[q] * blank_[dt][q];
      dt++;
    }

    t_[d_] = q;

    // printf("%d\\n", diff);

    if (diff > 0) {
      t = t_;
      blank = blank_;
    } else {
      t_ = t;
      blank_ = blank;
    }
  }

  for (int j = 0; j < d; ++j) {
    printf("%d\\n", t[j] + 1);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  chrono::system_clock::time_point start;
  start = chrono::system_clock::now();

  ios::sync_with_stdio(false);
  cin.tie(0);

  const int INF = 1000000000;

  int d;
  cin >> d;
  vector<int> c(26);
  vector<vector<int>> s(d, vector<int>(26));
  for (int i = 0; i < 26; ++i)
    cin >> c[i];
  for (int j = 0; j < d; ++j)
    for (int i = 0; i < 26; ++i)
      cin >> s[j][i];

  int m;
  vector<int> t(d);
  vector<vector<int>> blank(d, vector<int>(26, 1));
  for (int j = 0; j < d; ++j) {
    for (int i = 0; i < 26; ++i)
      if (j != 0)
        blank[j][i] = blank[j - 1][i] + 1;
    m = -INF;
    for (int i = 0; i < 26; ++i) {
      if (s[j][i] + c[i] * blank[j][i] > m) {
        t[j] = i;
        m = s[j][i] + c[i] * blank[j][i];
      }
    }
    blank[j][t[j]] = 0;
  }

  int d_, q, dt, diff = 0, m2;
  vector<int> t_ = t;
  vector<vector<int>> blank_ = blank;
  srand((unsigned int)time(NULL));
  while (chrono::duration_cast<chrono::seconds>(chrono::system_clock::now() -
                                                start)
             .count() < 1.9) {
    d_ = rand() % d;
    m2 = -INF;
    for (int i = 0; i < 26; ++i) {
      if (s[d_][i] > m2) {
        m2 = s[d_][i];
        q = i;
      }
    }
    // q = rand() % 26;

    diff = 0;

    diff -= s[d_][t_[d_]];
    diff += s[d_][q];

    if (d_ != 0)
      blank_[d_][t_[d_]] = blank_[d_ - 1][t_[d_]] + 1;
    else
      blank_[d_][t_[d_]] = 1;
    diff -= c[t_[d_]] * blank_[d_][t_[d_]];
    dt = d_ + 1;
    while (t_[dt] != t_[d_] && dt < d) {
      diff += c[t_[d_]] * blank_[dt][t_[d_]];
      blank_[dt][t_[d_]] = blank_[dt - 1][t_[d_]] + 1;
      diff -= c[t_[d_]] * blank_[dt][t_[d_]];
      dt++;
    }

    diff += c[q] * blank_[d_][q];
    blank_[d_][q] = 0;
    dt = d_ + 1;
    while (t_[dt] != q && dt < d) {
      diff += c[q] * blank_[dt][q];
      blank_[dt][q] = blank_[dt - 1][q] + 1;
      diff -= c[q] * blank_[dt][q];
      dt++;
    }

    t_[d_] = q;

    printf("%d\\n", diff);

    if (diff > 0) {
      t = t_;
      blank = blank_;
    } else {
      t_ = t;
      blank_ = blank;
    }
  }

  for (int j = 0; j < d; ++j) {
    printf("%d\\n", t[j] + 1);
  }

  return 0;
}