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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf = 1e18;
unsigned long mix(unsigned long a, unsigned long b, unsigned long c) {
  a = a - b;
  a = a - c;
  a = a ^ (c >> 13);
  b = b - c;
  b = b - a;
  b = b ^ (a << 8);
  c = c - a;
  c = c - b;
  c = c ^ (b >> 13);
  a = a - b;
  a = a - c;
  a = a ^ (c >> 12);
  b = b - c;
  b = b - a;
  b = b ^ (a << 16);
  c = c - a;
  c = c - b;
  c = c ^ (b >> 5);
  a = a - b;
  a = a - c;
  a = a ^ (c >> 3);
  b = b - c;
  b = b - a;
  b = b ^ (a << 10);
  c = c - a;
  c = c - b;
  c = c ^ (b >> 15);
  return c;
}
unsigned long seed = mix(clock(), time(NULL), 22869420);
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, 2, 4, 8, 16, 32, 64, 96};
vector<int> best;
int main() {
  srand(seed);
  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 < 300; times++) {
    for (int k = 7; k > 0; k--) {
      vector<int> changeid, changeval;
      vector<int> nw = best;
      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 = nw;
        for (int i = 0; i < cv; i++)
          cur[changeid[i] - 1] = changeval[i];
        if (tester(best) < tester(cur))
          best = cur;
        else {
          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;
unsigned long mix(unsigned long a, unsigned long b, unsigned long c) {
  a = a - b;
  a = a - c;
  a = a ^ (c >> 13);
  b = b - c;
  b = b - a;
  b = b ^ (a << 8);
  c = c - a;
  c = c - b;
  c = c ^ (b >> 13);
  a = a - b;
  a = a - c;
  a = a ^ (c >> 12);
  b = b - c;
  b = b - a;
  b = b ^ (a << 16);
  c = c - a;
  c = c - b;
  c = c ^ (b >> 5);
  a = a - b;
  a = a - c;
  a = a ^ (c >> 3);
  b = b - c;
  b = b - a;
  b = b ^ (a << 10);
  c = c - a;
  c = c - b;
  c = c ^ (b >> 15);
  return c;
}
unsigned long seed = mix(clock(), time(NULL), 22869420);
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, 2, 4, 8, 16, 32, 64, 96, 128};
vector<int> best;
int main() {
  srand(seed);
  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 < 400; times++) {
    for (int k = 8; k > 0; k--) {
      vector<int> changeid, changeval;
      vector<int> nw = best;
      for (int tt = 0; tt < 5 * (12 - k); tt++) {
        int cv = randlf(lgg[k - 1], lgg[k]);
        changeid = gen(cv, 365);
        changeval = gen(cv, 26);
        vector<int> cur = nw;
        for (int i = 0; i < cv; i++)
          cur[changeid[i] - 1] = changeval[i];
        if (tester(best) < tester(cur))
          best = cur;
        else {
          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;
}