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 ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

struct XORShift {
  unsigned int x, y, z, w, t;

  XORShift(int seed) {
    mt19937 rnd(seed);
    x = rnd();
    y = rnd();
    z = rnd();
    w = rnd();
    t = 1;
  }

  int rand() {
    t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w & 0x7fffffff;
  }
} rnd(rand());

const int N = 26;
const int D = 365;
const double TIMELIMIT = 1.95;
int d;
int c[N];
int s[D][N];

struct STATE {
  int contests[D];
  /*
  int pre[D][N];
  int nxt[D][N];
  */
  int score;
};

STATE state;

void init(STATE &state) {
  state.score = 0;
  int last[N] = {};
  rep(i, D) {
    int mx = -INF;
    int select;
    rep(j, N) {
      if (s[i][j] > mx) {
        mx = s[i][j];
        select = j;
      }
    }
    state.contests[i] = select;
    state.score += mx;
    last[select] = i + 1;
    rep(j, N) { state.score -= c[j] * (i + 1 - last[j]); }
  }
  /*
  fill(state.pre, state.pre + D, 0);
  fill(state.nxt, state.nxt + D, D + 1);
  fill(last, last + N, 0);
  rep(i, D) {
      rep(j, N) {
          state.pre[i][j] = last[j];
      }
      last[state.contests[i]] = i + 1;
  }
  fill(last, last + N, D + 1);
  rep(i, D) {
      rep(j, N) {
          state.pre[D - 1 - i][j] = last[j];
      }
      last[state.contests[D - 1 - i]] = D - i;
  }
  */
}

int cnt = 0;
void SA() {
  init(state);
  double starttemp = 9000.0, endtemp = 1.0;
  int startclock = clock();
  while (true) {
    cnt++;
    int nowclock = clock();
    if (nowclock - startclock > TIMELIMIT * CLOCKS_PER_SEC)
      break;
    STATE newstate = state;
    int d = rnd.rand() % D, cc = rnd.rand() % N;
    newstate.score = 0;
    newstate.contests[d] = cc;
    int last[N] = {};
    rep(i, D) {
      newstate.score += s[i][newstate.contests[i]];
      last[newstate.contests[i]] = i + 1;
      rep(j, N) { newstate.score -= c[j] * (i + 1 - last[j]); }
    }
    double temp = starttemp + (endtemp - starttemp) * (nowclock - startclock) /
                                  (TIMELIMIT * CLOCKS_PER_SEC);
    if (exp((newstate.score - state.score) / temp) >
        (rnd.rand() % INF) / (double)INF) {
      state = newstate;
    }
  }
}

signed main() {
  int t;
  cin >> t;
  rep(i, N) { cin >> c[i]; }
  rep(i, D) {
    rep(j, N) { cin >> s[i][j]; }
  }
  SA();
  rep(i, D) { cout << state.contests[i] + 1 << endl; }
  /*
  cout << "score : " << state.score << endl;
  cout << "cnt : " << cnt << endl;
  */
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

struct XORShift {
  unsigned int x, y, z, w, t;

  XORShift(int seed) {
    mt19937 rnd(seed);
    x = rnd();
    y = rnd();
    z = rnd();
    w = rnd();
    t = 1;
  }

  int rand() {
    t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w & 0x7fffffff;
  }
} rnd(rand());

const int N = 26;
const int D = 365;
const double TIMELIMIT = 1.99;
int d;
int c[N];
int s[D][N];

struct STATE {
  int contests[D];
  /*
  int pre[D][N];
  int nxt[D][N];
  */
  int score;
};

STATE state;

void init(STATE &state) {
  state.score = 0;
  int last[N] = {};
  rep(i, D) {
    int mx = -INF;
    int select;
    rep(j, N) {
      if (s[i][j] > mx) {
        mx = s[i][j];
        select = j;
      }
    }
    state.contests[i] = select;
    state.score += mx;
    last[select] = i + 1;
    rep(j, N) { state.score -= c[j] * (i + 1 - last[j]); }
  }
  /*
  fill(state.pre, state.pre + D, 0);
  fill(state.nxt, state.nxt + D, D + 1);
  fill(last, last + N, 0);
  rep(i, D) {
      rep(j, N) {
          state.pre[i][j] = last[j];
      }
      last[state.contests[i]] = i + 1;
  }
  fill(last, last + N, D + 1);
  rep(i, D) {
      rep(j, N) {
          state.pre[D - 1 - i][j] = last[j];
      }
      last[state.contests[D - 1 - i]] = D - i;
  }
  */
}

int cnt = 0;
void SA() {
  init(state);
  double starttemp = 9000.0, endtemp = 1.0;
  int startclock = clock();
  while (true) {
    cnt++;
    int nowclock = clock();
    if (nowclock - startclock > TIMELIMIT * CLOCKS_PER_SEC)
      break;
    STATE newstate = state;
    int d = rnd.rand() % D, cc = rnd.rand() % N;
    newstate.score = 0;
    newstate.contests[d] = cc;
    int last[N] = {};
    rep(i, D) {
      newstate.score += s[i][newstate.contests[i]];
      last[newstate.contests[i]] = i + 1;
      rep(j, N) { newstate.score -= c[j] * (i + 1 - last[j]); }
    }
    double temp = starttemp + (endtemp - starttemp) * (nowclock - startclock) /
                                  (TIMELIMIT * CLOCKS_PER_SEC);
    if (exp((newstate.score - state.score) / temp) >
        (rnd.rand() % INF) / (double)INF) {
      state = newstate;
    }
  }
}

signed main() {
  cin >> d;
  rep(i, N) { cin >> c[i]; }
  rep(i, D) {
    rep(j, N) { cin >> s[i][j]; }
  }
  SA();
  rep(i, D) { cout << state.contests[i] + 1 << endl; }
  /*
  cout << "score : " << state.score << endl;
  cout << "cnt : " << cnt << endl;
  */
  return 0;
}