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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using PL = pair<ll, ll>;
using P = pair<int, int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007; // = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;
constexpr double TL = 1.85;

clock_t start, fin;
int D;
int c[26];
int s[365][26];
int last[26];
int answer[365];
ll before_max;

ll compute_res() {
  ll ans = 0;
  vector<int> before2(26, -1);
  rep(i, D) {
    int t = answer[i];
    before2[t] = i;
    ll ret = s[i][t];
    rep(j, 26) { ret -= (i - before2[j]) * c[j]; }
    ans += ret;
  }
  return ans;
}

int calc_score(int d, int i) {
  int bf = last[i];
  last[i] = d;
  int res = s[d][i];
  rep(j, 26) { res -= (d - last[j]) * c[j]; }
  last[i] = bf;
  return res;
}

void optimization() {
  rep(d, D) {
    int max_score = -INF;
    int best_i = -1;
    rep(i, 26) {
      int x = calc_score(d, i);
      if (x > max_score) {
        max_score = x;
        best_i = i;
      }
    }
    last[best_i] = d;
    answer[d] = best_i;
  }
}

void change_day() {
  int d = rand() % D;
  int q = rand() % 26;

  if (answer[d] == q)
    return;
  int old = answer[d];
  answer[d] = q;
  ll score = compute_res();
  if (before_max < score)
    return;
  else {
    answer[d] = old;
    return;
  }
}

int main() {
  clock_t start = clock();
  cin >> D;
  rep(i, 26) cin >> c[i];
  rep(i, D) rep(j, 26) cin >> s[i][j];
  fill(last, last + 26, -1);

  /*
  //答え確認用
  printf("%d\\n",D);
  rep(i,26) printf("%d ",c[i]);
  cout << '\\n';
  rep(i,D){
      rep(j,26) printf("%d ",s[i][j]);
      cout << '\\n';
  }
  */

  optimization();
  before_max = compute_res();
  fin = clock();
  while ((double)(fin - start) / CLOCKS_PER_SEC < TL) {
    change_day();
    fin = clock();
  }
  rep(i, D) printf("%d\\n", answer[i] + 1);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using PL = pair<ll, ll>;
using P = pair<int, int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007; // = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;
constexpr double TL = 1.75;

clock_t start, fin;
int D;
int c[26];
int s[365][26];
int last[26];
int answer[365];
ll before_max;

ll compute_res() {
  ll ans = 0;
  vector<int> before2(26, -1);
  rep(i, D) {
    int t = answer[i];
    before2[t] = i;
    ll ret = s[i][t];
    rep(j, 26) { ret -= (i - before2[j]) * c[j]; }
    ans += ret;
  }
  return ans;
}

int calc_score(int d, int i) {
  int bf = last[i];
  last[i] = d;
  int res = s[d][i];
  rep(j, 26) { res -= (d - last[j]) * c[j]; }
  last[i] = bf;
  return res;
}

void optimization() {
  rep(d, D) {
    int max_score = -INF;
    int best_i = -1;
    rep(i, 26) {
      int x = calc_score(d, i);
      if (x > max_score) {
        max_score = x;
        best_i = i;
      }
    }
    last[best_i] = d;
    answer[d] = best_i;
  }
}

void change_day() {
  int d = rand() % D;
  int q = rand() % 26;

  if (answer[d] == q)
    return;
  int old = answer[d];
  answer[d] = q;
  ll score = compute_res();
  if (before_max < score)
    return;
  else {
    answer[d] = old;
    return;
  }
}

int main() {
  clock_t start = clock();
  cin >> D;
  rep(i, 26) cin >> c[i];
  rep(i, D) rep(j, 26) cin >> s[i][j];
  fill(last, last + 26, -1);

  /*
  //答え確認用
  printf("%d\\n",D);
  rep(i,26) printf("%d ",c[i]);
  cout << '\\n';
  rep(i,D){
      rep(j,26) printf("%d ",s[i][j]);
      cout << '\\n';
  }
  */

  optimization();
  before_max = compute_res();
  fin = clock();
  while ((fin - start) / CLOCKS_PER_SEC < TL) {
    change_day();
    fin = clock();
  }
  rep(i, D) printf("%d\\n", answer[i] + 1);
  return 0;
}