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

#include <bits/stdc++.h>

#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF (1 << 29)

using namespace std;
typedef long long ll;

const int nc = 26;
int D;
ll c[40], t[400];
ll s[400][40];

ll scoring() {
  vector<int> last(nc + 10, -1);

  long long sat = 0;

  for (ll d = 0; d < D; d++) {
    last[t[d]] = d;

    for (int i = 0; i < nc; i++) {
      sat -= c[i] * (d - last[i]);
    }
    sat += s[d][t[d]];
    // cout << sat << endl;
  }

  return sat;
}

double elapsed_time(chrono::system_clock::time_point start) {
  auto now_time = chrono::system_clock::now();
  auto elapsed =
      chrono::duration_cast<std::chrono::milliseconds>(now_time - start)
          .count();
  // cout << elapsed << endl;
  return elapsed;
}

int main() {
  random_device rnd;
  mt19937 mt(rnd());

  cin >> D;

  for (int i = 0; i < nc; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < D; i++) {
    for (int j = 0; j < nc; j++) {
      cin >> s[i][j];
    }
  }

  ll score = scoring();

  for (int i = 0; i < D; i++) {
    t[i] = mt() % nc; // 初期解をrandで生成
  }

  auto start = chrono::system_clock::now();
  int cnt = 0;
  while (cnt < 100000) {
    cnt++;
    // if(elapsed_time(start) > 1.9) break;
    int d, q, old;

    d = mt() % D;
    q = mt() % nc;
    old = t[d];
    t[d] = q;

    ll new_score = scoring();
    // cout << new_score << endl;
    if (score > new_score) {
      t[d] = old;
    } else {
      score = new_score;
    }
  }

  // cout << score << endl;
  for (int i = 0; i < D; i++) {
    cout << ++t[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF (1 << 29)

using namespace std;
typedef long long ll;

const int nc = 26;
int D;
ll c[40], t[400];
ll s[400][40];

ll scoring() {
  vector<int> last(nc + 10, -1);

  long long sat = 0;

  for (ll d = 0; d < D; d++) {
    last[t[d]] = d;

    for (int i = 0; i < nc; i++) {
      sat -= c[i] * (d - last[i]);
    }
    sat += s[d][t[d]];
    // cout << sat << endl;
  }

  return sat;
}

double elapsed_time(chrono::system_clock::time_point start) {
  auto now_time = chrono::system_clock::now();
  auto elapsed =
      chrono::duration_cast<std::chrono::milliseconds>(now_time - start)
          .count();
  // cout << elapsed << endl;
  return elapsed;
}

int main() {
  random_device rnd;
  mt19937 mt(rnd());

  cin >> D;

  for (int i = 0; i < nc; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < D; i++) {
    for (int j = 0; j < nc; j++) {
      cin >> s[i][j];
    }
  }

  ll score = scoring();

  for (int i = 0; i < D; i++) {
    t[i] = mt() % nc; // 初期解をrandで生成
  }

  auto start = chrono::system_clock::now();
  int cnt = 0;
  while (cnt < 1000000) {
    cnt++;
    // if(elapsed_time(start) > 1.9) break;
    int d, q, old;

    d = mt() % D;
    q = mt() % nc;
    old = t[d];
    t[d] = q;

    ll new_score = scoring();
    // cout << new_score << endl;
    if (score > new_score) {
      t[d] = old;
    } else {
      score = new_score;
    }
  }

  // cout << score << endl;
  for (int i = 0; i < D; i++) {
    cout << ++t[i] << endl;
  }
  return 0;
}