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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define per1(i, n) for (int i = n; i > 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
random_device rnd;
mt19937 mt(rnd());
uniform_int_distribution<> rand365(1, 365);
uniform_int_distribution<> rand26(1, 26), rand100(0, 100);
ll D, c[30];
vec s[400];
vec ans(400);
double start_temp = 0, end_temp = 0;
double start_time, TIME_LIMIT = 1.95;
ll score(vec A) {
  ll ans = 0, last[30] = {0};
  rep1(i, D) {
    ans += s[i][A[i]];
    last[A[i]] = i;
    rep1(j, 26) ans -= c[j] * (i - last[j]);
  }
  return ans;
}

double getTime() { return (double)clock() / CLOCKS_PER_SEC; }

void input() {
  cin >> D;
  rep1(i, 26) cin >> c[i];
  int S;
  rep1(i, D) {
    s[i].push_back(0);
    rep(j, 26) cin >> S, s[i].push_back(S);
  }
}

void solve() {
  rep1(i, D) { ans[i] = max_element(all(s[i])) - s[i].begin(); }
  start_time = getTime();
  ll now = score(ans);
  while (true) {
    double now_time = getTime();
    if (now_time - start_time > TIME_LIMIT)
      break;
    double temp = start_temp + (end_temp - start_temp) *
                                   (now_time - start_time) / TIME_LIMIT;
    int d = rand365(mt), q = rand26(mt), preq;
    preq = ans[d];
    ans[d] = q;
    ll a = score(ans);
    if (a > now)
      now = a;
    else {
      if (temp > rand100(mt))
        now = a;
      ans[d] = preq;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  input();
  solve();
  rep1(i, D) cout << ans[i] << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define per1(i, n) for (int i = n; i > 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
random_device rnd;
mt19937 mt(rnd());
uniform_int_distribution<> rand365(1, 365);
uniform_int_distribution<> rand26(1, 26), rand100(0, 100);
ll D, c[30];
vec s[400];
vec ans(400);
double start_temp = 0, end_temp = 0;
double start_time, TIME_LIMIT = 3;
ll score(vec A) {
  ll ans = 0, last[30] = {0};
  rep1(i, D) {
    ans += s[i][A[i]];
    last[A[i]] = i;
    rep1(j, 26) ans -= c[j] * (i - last[j]);
  }
  return ans;
}

double getTime() { return (double)clock() / CLOCKS_PER_SEC; }

void input() {
  cin >> D;
  rep1(i, 26) cin >> c[i];
  int S;
  rep1(i, D) {
    s[i].push_back(0);
    rep(j, 26) cin >> S, s[i].push_back(S);
  }
}

void solve() {
  rep1(i, D) { ans[i] = max_element(all(s[i])) - s[i].begin(); }
  start_time = getTime();
  ll now = score(ans);
  while (true) {
    double now_time = getTime();
    if (now_time - start_time > TIME_LIMIT)
      break;
    double temp = start_temp + (end_temp - start_temp) *
                                   (now_time - start_time) / TIME_LIMIT;
    int d = rand365(mt), q = rand26(mt), preq;
    preq = ans[d];
    ans[d] = q;
    ll a = score(ans);
    if (a > now)
      now = a;
    else {
      if (temp > rand100(mt))
        now = a;
      ans[d] = preq;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  input();
  solve();
  rep1(i, D) cout << ans[i] << "\\n";
  cout << score(ans);
}