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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
void printvec(vec &v) {
  Rep(i, (ll)v.size()) { cout << v[i] << " \\n"[i + 1 == (ll)v.size()]; }
}

ll D = 365;
vec c(26);
mat s(D, vec(26));
vec last(26, 0);

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  clock_t start = clock();

  cin >> D;
  Rep(i, 26) { cin >> c[i]; }
  Rep(i, D) {
    Rep(j, 26) { cin >> s[i][j]; }
  }

  vec t(D);

  ll tmp = 0;
  Rep(i, D) {
    ll ans = 0;
    ll best = -inf;
    Rep(j, 26) {
      ll score = 0;
      score += s[i][j];
      Rep(k, 26) {
        if (k != j)
          score -= c[k] * (i + 1 - last[k]);
      }
      if (score > best) {
        ans = j;
        best = score;
      }
    }

    last[ans] = i + 1;
    tmp += s[i][ans];
    Rep(j, 26) { tmp -= c[j] * (i + 1 - last[j]); }

    t[i] = ans;
  }

  vector<set<ll>> days(26);
  Rep(i, 26) {
    days[i].insert(-1);
    days[i].insert(D);
  }
  Rep(i, D) { days[t[i]].insert(i); }

  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<> randd(0, 364), randq(0, 25);

  ll cnt = 0;

  while (1.0 * (clock() - start) / CLOCKS_PER_SEC < 1.0) {
    cnt++;
    ll d = randd(mt);
    ll q = randq(mt);

    ll oldscore = tmp;

    tmp -= s[d][t[d]];
    tmp += s[d][q];
    auto old2 = days[t[d]].upper_bound(d);
    old2--;
    old2--;
    auto old1 = old2;
    old2++;
    old2++;
    auto new2 = days[q].lower_bound(d);
    auto new1 = --new2;
    new2++;
    // cout << *old1 << " " << *old2 << " " << *new1 << " " << *new2 << "\\n";
    tmp += c[q] * (d - *new1) * (*new2 - d);
    tmp -= c[t[d]] * (d - *old1) * (*old2 - d);

    if (tmp > oldscore) {
      days[t[d]].erase(d);
      days[q].insert(d);
      t[d] = q;
    }
  }

  Rep(i, D) { cout << t[i] + 1 << "\\n"; }

  // cout << (double)(clock() - start) / CLOCKS_PER_SEC << "\\n";
  // cout << cnt << "\\n";
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
void printvec(vec &v) {
  Rep(i, (ll)v.size()) { cout << v[i] << " \\n"[i + 1 == (ll)v.size()]; }
}

ll D = 365;
vec c(26);
mat s(D, vec(26));
vec last(26, 0);

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  clock_t start = clock();

  cin >> D;
  Rep(i, 26) { cin >> c[i]; }
  Rep(i, D) {
    Rep(j, 26) { cin >> s[i][j]; }
  }

  vec t(D);

  ll tmp = 0;
  Rep(i, D) {
    ll ans = 0;
    ll best = -inf;
    Rep(j, 26) {
      ll score = 0;
      score += s[i][j];
      Rep(k, 26) {
        if (k != j)
          score -= c[k] * (i + 1 - last[k]);
      }
      if (score > best) {
        ans = j;
        best = score;
      }
    }

    last[ans] = i + 1;
    tmp += s[i][ans];
    Rep(j, 26) { tmp -= c[j] * (i + 1 - last[j]); }

    t[i] = ans;
  }

  vector<set<ll>> days(26);
  Rep(i, 26) {
    days[i].insert(-1);
    days[i].insert(D);
  }
  Rep(i, D) { days[t[i]].insert(i); }

  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<> randd(0, 364), randq(0, 25);

  while ((clock() - start) / CLOCKS_PER_SEC < 1.9) {
    ll d = randd(mt);
    ll q = randq(mt);

    ll oldscore = tmp;

    tmp -= s[d][t[d]];
    tmp += s[d][q];
    auto old2 = days[t[d]].upper_bound(d);
    old2--;
    old2--;
    auto old1 = old2;
    old2++;
    old2++;
    auto new2 = days[q].lower_bound(d);
    auto new1 = --new2;
    new2++;
    // cout << *old1 << " " << *old2 << " " << *new1 << " " << *new2 << "\\n";
    tmp += c[q] * (d - *new1) * (*new2 - d);
    tmp -= c[t[d]] * (d - *old1) * (*old2 - d);

    if (tmp > oldscore) {
      days[t[d]].erase(d);
      days[q].insert(d);
      t[d] = q;
    }
  }

  Rep(i, D) { cout << t[i] + 1 << "\\n"; }
}