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

#include <ctime>
#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
ll C[32];
ll S[366][32];
ll T[366];
ll P[32];
ll L[32];
set<int> H[32];

uint32_t xor128(void) {
  static uint32_t x = 123456789;
  static uint32_t y = 362436069;
  static uint32_t z = 521288629;
  static uint32_t w = 88675123;
  uint32_t t;

  t = x ^ (x << 11);
  x = y;
  y = z;
  z = w;
  return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}

ll calcPenalty(int con, int D) {
  ll ret = 0;
  ll prev = 0;
  for (auto &it : H[con]) {
    ll dif = it - prev;
    ret += (dif - 1) * dif / 2;
    prev = it;
  }
  ll dif = D + 1 - prev;
  ret += (dif - 1) * dif / 2;
  ret *= C[con];
  return ret;
}

ll update(int day, int pcon, int ncon, int D) {
  ll pen = -P[pcon] - P[ncon];
  H[pcon].erase(day);
  H[ncon].insert(day);
  T[day] = ncon;
  ll vP = calcPenalty(pcon, D);
  ll vN = calcPenalty(ncon, D);
  pen += P[pcon] + P[ncon];
  ll ret = -pen;
  ret -= S[day][pcon];
  ret += S[day][ncon];
  if (ret > 0) {
    P[pcon] = vP;
    P[ncon] = vN;
    T[day] = ncon;
  } else {
    H[pcon].insert(day);
    H[ncon].erase(day);
  }
  return ret;
}

int main() {
  clock_t time_start = clock();
  ios::sync_with_stdio(false);
  int D;
  cin >> D;
  for (int i = 1; i <= 26; i++) {
    cin >> C[i];
  }
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> S[i][j];
    }
  }
  for (int day = 1; day <= D; day++) {
    ll ocon = 1;
    ll oval = S[day][ocon] + (day - L[ocon]) * C[ocon];
    for (int con = 2; con <= 26; con++) {
      ll val = S[day][con] + (day - L[con]) * C[con];
      if (val > oval) {
        ocon = con;
        oval = val;
      }
    }
    T[day] = ocon;
    L[ocon] = day;
    H[ocon].insert(day);
  }
  for (int con = 1; con <= 26; con++) {
    P[con] = calcPenalty(con, D);
  }
  while (1) {
    clock_t time_now = clock();
    if ((double)(time_now - time_start) > (double)CLOCKS_PER_SEC * 1.9) {
      break;
    }
    int day = xor128() % D + 1;
    int ncon = xor128() % 26 + 1;
    int pcon = T[day];
    update(day, pcon, ncon, D);
  }
  for (int day = 1; day <= D; day++) {
    cout << T[day] << '\\n';
  }
  cout << flush;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
ll C[32];
ll S[366][32];
ll T[366];
ll P[32];
ll L[32];
set<int> H[32];

uint32_t xor128(void) {
  static uint32_t x = 123456789;
  static uint32_t y = 362436069;
  static uint32_t z = 521288629;
  static uint32_t w = 88675123;
  uint32_t t;

  t = x ^ (x << 11);
  x = y;
  y = z;
  z = w;
  return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}

ll calcPenalty(int con, int D) {
  ll ret = 0;
  ll prev = 0;
  for (auto &it : H[con]) {
    ll dif = it - prev;
    ret += (dif - 1) * dif / 2;
    prev = it;
  }
  ll dif = D + 1 - prev;
  ret += (dif - 1) * dif / 2;
  ret *= C[con];
  return ret;
}

ll update(int day, int pcon, int ncon, int D) {
  ll pen = -P[pcon] - P[ncon];
  H[pcon].erase(day);
  H[ncon].insert(day);
  T[day] = ncon;
  ll vP = calcPenalty(pcon, D);
  ll vN = calcPenalty(ncon, D);
  pen += P[pcon] + P[ncon];
  ll ret = -pen;
  ret -= S[day][pcon];
  ret += S[day][ncon];
  if (ret > 0) {
    P[pcon] = vP;
    P[ncon] = vN;
    T[day] = ncon;
  } else {
    H[pcon].insert(day);
    H[ncon].erase(day);
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  int D;
  cin >> D;
  for (int i = 1; i <= 26; i++) {
    cin >> C[i];
  }
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> S[i][j];
    }
  }
  for (int day = 1; day <= D; day++) {
    ll ocon = 1;
    ll oval = S[day][ocon] + (day - L[ocon]) * C[ocon];
    for (int con = 2; con <= 26; con++) {
      ll val = S[day][con] + (day - L[con]) * C[con];
      if (val > oval) {
        ocon = con;
        oval = val;
      }
    }
    T[day] = ocon;
    L[ocon] = day;
    H[ocon].insert(day);
  }
  for (int con = 1; con <= 26; con++) {
    P[con] = calcPenalty(con, D);
  }
  for (int opt = 0; opt < 1000000; opt++) {
    int day = xor128() % D + 1;
    int ncon = xor128() % 26 + 1;
    int pcon = T[day];
    update(day, pcon, ncon, D);
  }
  for (int day = 1; day <= D; day++) {
    cout << T[day] << endl;
  }
}