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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int, int>;
const int kind = 26;
const int INF = 1e9;
const int max_queries = 5e4;

int main() {
  int D;
  cin >> D;
  vector<int> c(kind);
  rep(i, kind) cin >> c[i];
  int c_sum = 0;
  rep(i, kind) c_sum += c[i];
  vector<vector<int>> s(D, vector<int>(kind));
  rep(i, D) { rep(j, kind) cin >> s[i][j]; }
  vector<int> t(D), max_score(D, -INF);
  vector<int> last(kind);
  int dec = 0;
  for (int day = 1; day <= D; day++) {
    int k = -1;
    dec += c_sum;
    rep(i, kind) {
      int res = s[day - 1][i];
      res -= dec;
      res += c[i] * (day - last[i]);
      if (res > max_score[day - 1]) {
        max_score[day - 1] = res;
        k = i;
      }
    }
    dec -= c[k] * last[k];
    last[k] = day;
    t[day - 1] = k;
  }
  /*
  vector<set<int>> hold(kind);
  rep(i,kind) hold[i].insert(0);
  rep(i,D) hold[t[i]].insert(i+1);
  */
  int M = max_queries;
  vector<int> d(M), q(M);
  q[0] = 1;
  vector<int> t_new(D);
  rep(i, D) t_new[i] = t[i];
  int fin_score = 0;
  rep(i, M) {
    int ch_ans = 0, ch_dec = 0;
    vector<int> ch_last(kind);
    set<P> change;
    t_new[d[i]] = q[i];
    for (int day = 1; day <= D; day++) {
      int k = t_new[day - 1];
      ch_ans += s[day - 1][k];
      ch_dec += c_sum;
      ch_ans -= ch_dec;
      ch_ans += c[k] * (day - ch_last[k]);
      ch_dec -= c[k] * (day - ch_last[k]);
      int tot = s[day - 1][k] - ch_dec + c[k] * (day - ch_last[k]);
      change.insert(P(tot, day));
      ch_last[k] = day;
    }
    if (fin_score >= ch_ans) {
      t_new[d[i]] = t[d[i]];
    } else {
      fin_score = ch_ans;
      /*
      hold[t[d[i]]].erase(d[i]+1);
      hold[q[i]].insert(d[i]+1);
      */
    }
    if (i != M - 1) {
      auto itr = change.begin();
      int r = rand() % 20, plus = 0;
      if (r <= 10)
        plus = 0;
      else if (r <= 15)
        plus = 1;
      else if (r <= 17)
        plus = 2;
      else if (r <= 18)
        plus = 3;
      else
        plus = 4;
      rep(j, plus) itr++;
      d[i + 1] = (itr->second) - 1;
      /*
      int res=-INF, n=-1;
      rep(j,kind){
              auto itr=hold[j].upper_bound(d[i+1]);
              itr--;
              int prev=*itr;
              int dif=c[j]*((d[i+1]+1)-prev);
              if(res<dif){
                      res=dif;
                      n=j;
              }
      }
      q[i+1]=n;
      */
      q[i + 1] = rand() % 26;
    }
  }
  rep(i, D) cout << t_new[i] + 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int, int>;
const int kind = 26;
const int INF = 1e9;
const int max_queries = 1e5;

int main() {
  int D;
  cin >> D;
  vector<int> c(kind);
  rep(i, kind) cin >> c[i];
  int c_sum = 0;
  rep(i, kind) c_sum += c[i];
  vector<vector<int>> s(D, vector<int>(kind));
  rep(i, D) { rep(j, kind) cin >> s[i][j]; }
  vector<int> t(D), max_score(D, -INF);
  vector<int> last(kind);
  int ans = 0, dec = 0;
  for (int day = 1; day <= D; day++) {
    int k = -1;
    dec += c_sum;
    rep(i, kind) {
      int res = s[day - 1][i];
      res -= dec;
      res += c[i] * (day - last[i]);
      if (res > max_score[day - 1]) {
        max_score[day - 1] = res;
        k = i;
      }
    }
    dec -= c[k] * last[k];
    last[k] = day;
    t[day - 1] = k;
  }
  vector<set<int>> hold(kind);
  rep(i, kind) hold[i].insert(0);
  rep(i, D) hold[t[i]].insert(i + 1);
  int M = max_queries;
  vector<int> d(M), q(M);
  q[0] = 1;
  vector<int> t_new(D);
  rep(i, D) t_new[i] = t[i];
  int fin_score = 0;
  rep(i, M) {
    int ch_ans = 0, ch_dec = 0;
    vector<int> ch_last(kind);
    set<P> change;
    t_new[d[i]] = q[i];
    for (int day = 1; day <= D; day++) {
      int k = t_new[day - 1];
      ch_ans += s[day - 1][k];
      ch_dec += c_sum;
      ch_ans -= ch_dec;
      ch_ans += c[k] * (day - ch_last[k]);
      ch_dec -= c[k] * (day - ch_last[k]);
      int tot = s[day - 1][k] - ch_dec + c[k] * (day - ch_last[k]);
      change.insert(P(tot, day));
      ch_last[k] = day;
    }
    if (fin_score >= ch_ans) {
      t_new[d[i]] = t[d[i]];
    } else {
      fin_score = ch_ans;
      hold[t[d[i]]].erase(d[i] + 1);
      hold[q[i]].insert(d[i] + 1);
    }
    if (i != M - 1) {
      auto itr = change.begin();
      int r = rand() % 20, plus = 0;
      if (r <= 10)
        plus = 0;
      else if (r <= 15)
        plus = 1;
      else if (r <= 17)
        plus = 2;
      else if (r <= 18)
        plus = 3;
      else
        plus = 4;
      rep(j, plus) itr++;
      d[i + 1] = (itr->second) - 1;
      /*
      int res=-INF, n=-1;
      rep(j,kind){
              auto itr=hold[j].upper_bound(d[i+1]);
              itr--;
              int prev=*itr;
              int dif=c[j]*((d[i+1]+1)-prev);
              if(res<dif){
                      res=dif;
                      n=j;
              }
      }
      q[i+1]=n;
      */
      q[i + 1] = rand() % 26;
    }
  }
  rep(i, D) cout << t_new[i] + 1 << endl;

  return 0;
}