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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf = 1e18;
int n;
ll cst[26], sat[366][26], last[26], tmp[26];
ll tester(vector<int> v) {
  ll res = 0ll;
  memset(last, 0ll, sizeof(last));
  for (int nw, i = 0; i < n; i++) {
    nw = v[i];
    res += sat[i][nw - 1];
    last[nw - 1] = i + 1;
    for (int j = 0; j < 26; j++)
      res -= cst[j] * (i + 1 - last[j]);
  }
  return res;
}
int rand(int x) { return rand() % x + 1; }
vector<int> gen(int hm, int x) {
  vector<int> res;
  for (int cr = rand() % x + 1, times = 0; times < hm;
       cr = rand(x) % x + 1, times++)
    res.push_back(cr);
  return res;
}
int randlf(int l, int r) { return rand() % (r - l + 1) + l; }
vector<int> lgg = {1, 18, 36, 54, 72, 90, 108, 126, 144, 162, 180};
vector<int> best;
int main() {
  scanf("%d", &n);
  memset(last, 0ll, sizeof(last));
  for (int i = 0; i < 26; i++)
    scanf("%lld", &cst[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 26; j++)
      scanf("%lld", &sat[i][j]);
  ll res = 0ll;
  vector<int> base;
  for (int nw, i = 0; i < n; i++) {
    memset(tmp, 0ll, sizeof(tmp));
    for (nw = 0; nw < 26; nw++) {
      tmp[nw] += sat[i][nw];
      for (int j = 0; j < 26; j++)
        if (j != nw)
          tmp[nw] -= cst[j] * (i + 1 - last[j]);
    }
    ll get = -inf;
    int id = -1;
    for (nw = 0; nw < 26; nw++)
      if (get < tmp[nw]) {
        get = tmp[nw];
        id = nw;
      } else if (get == tmp[nw] && cst[nw] > cst[id]) {
        get = tmp[nw];
        id = nw;
      }
    base.push_back(id + 1);
    last[id] = i + 1;
    res += tmp[nw];
  }
  best = base;
  clock_t t = clock();
  for (; clock() - t < 1500;) {
    for (int k = 10; k > 0; k--) {
      vector<int> changeid, changeval;
      for (int tt = 0; tt < 5 * (11 - k); tt++) {
        int cv = randlf(lgg[k - 1], lgg[k]);
        changeid = gen(cv, 365);
        changeval = gen(cv, 26);
        vector<int> cur = best;
        for (int i = 0; i < cv; i++)
          cur[changeid[i] - 1] = changeval[i];
        if (tester(best) < tester(cur))
          best = cur;
      }
    }
  }
  for (int i = 0; i < n; i++)
    printf("%d\\n", best[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf = 1e18;
int n;
ll cst[26], sat[366][26], last[26], tmp[26];
ll tester(vector<int> v) {
  ll res = 0ll;
  memset(last, 0ll, sizeof(last));
  for (int nw, i = 0; i < n; i++) {
    nw = v[i];
    res += sat[i][nw - 1];
    last[nw - 1] = i + 1;
    for (int j = 0; j < 26; j++)
      res -= cst[j] * (i + 1 - last[j]);
  }
  return res;
}
int rand(int x) { return rand() % x + 1; }
vector<int> gen(int hm, int x) {
  vector<int> res;
  for (int cr = rand() % x + 1, times = 0; times < hm;
       cr = rand(x) % x + 1, times++)
    res.push_back(cr);
  return res;
}
int randlf(int l, int r) { return rand() % (r - l + 1) + l; }
vector<int> lgg = {1, 18, 36, 54, 72, 90, 108, 126, 144, 162, 180};
vector<int> best;
int main() {
  scanf("%d", &n);
  memset(last, 0ll, sizeof(last));
  for (int i = 0; i < 26; i++)
    scanf("%lld", &cst[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 26; j++)
      scanf("%lld", &sat[i][j]);
  ll res = 0ll;
  vector<int> base;
  for (int nw, i = 0; i < n; i++) {
    memset(tmp, 0ll, sizeof(tmp));
    for (nw = 0; nw < 26; nw++) {
      tmp[nw] += sat[i][nw];
      for (int j = 0; j < 26; j++)
        if (j != nw)
          tmp[nw] -= cst[j] * (i + 1 - last[j]);
    }
    ll get = -inf;
    int id = -1;
    for (nw = 0; nw < 26; nw++)
      if (get < tmp[nw]) {
        get = tmp[nw];
        id = nw;
      } else if (get == tmp[nw] && cst[nw] > cst[id]) {
        get = tmp[nw];
        id = nw;
      }
    base.push_back(id + 1);
    last[id] = i + 1;
    res += tmp[nw];
  }
  best = base;
  for (int times = 0; times < 1000; times++) {
    for (int k = 10; k > 0; k--) {
      vector<int> changeid, changeval;
      for (int tt = 0; tt < 20; tt++) {
        int cv = randlf(lgg[k - 1], lgg[k]);
        changeid = gen(cv, 365);
        changeval = gen(cv, 26);
        vector<int> cur = best;
        for (int i = 0; i < cv; i++)
          cur[changeid[i] - 1] = changeval[i];
        if (tester(best) < tester(cur))
          best = cur;
      }
    }
  }
  for (int i = 0; i < n; i++)
    printf("%d\\n", best[i]);
  return 0;
}