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

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

int d;                 // 日数
const int z = 26;      // コンテスト数
vector<int> c(z);      // コンテストiが開催されないペナルティ
vector<vector<int>> s; // i日目にコンテストjが開催された時の満足度

int score(vector<int> &t) {
  int sum = 0;
  vector<int> last(z, -1); // コンテストが最後に開催された日
  for (int i = 0; i < t.size(); i++) {
    sum += s[i][t[i]];
    last[t[i]] = i;
    for (int j = 0; j < z; j++) {
      sum -= c[j] * ((i + 1) - (last[j] + 1));
    }
  }
  return sum;
}

void mix(vector<int> &a, vector<int> &b, int index) {
  assert(index < d);
  assert(index >= 1);
  for (int i = 0; i < index; i++) {
    swap(a[i], b[i]);
  }
}

void Main() {
  cin >> d;
  s = vector<vector<int>>(d, vector<int>(z));
  for (int i = 0; i < z; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < z; j++) {
      cin >> s[i][j];
    }
  }

  const int debug = 0;
  const int n_change = 256 + 128;
  const int best_n = 512;
  const int mix_n = 256;
  const int change_n = 128;
  const int total_n = best_n + mix_n * 2 + change_n;

  std::mt19937 mt(rand());
  std::uniform_int_distribution<> rand_z(0, z - 1);
  std::uniform_int_distribution<> rand_mix(1, d - 1);
  std::uniform_int_distribution<> rand_d(0, d - 1);
  std::uniform_int_distribution<> rand_sample(0, best_n - 1);

  vector<vector<vector<int>>> ts(2,
                                 vector<vector<int>>(total_n, vector<int>(d)));
  for (int i = 0; i < ts[0].size(); i++) {
    for (int j = 0; j < d; j++) {
      ts[0][i][j] = rand_z(mt);
    }
  }

  vector<pair<int, int>> scores;
  scores.reserve(ts[0].size());
  int w0 = 0, w1 = 1;
  for (int i = 0; i < n_change; i++) {
    int ss = 0;
    if (i > 0) {
      scores.resize(best_n);
      ss = best_n;
    }
    for (int j = ss; j < ts[w0].size(); j++) {
      scores.push_back(make_pair(score(ts[w0][j]), j));
    }
    sort(scores.rbegin(), scores.rend());
    if (debug)
      cerr << scores[0].first << endl;

    for (int j = 0; j < best_n; j++) {
      for (int k = 0; k < d; k++) {
        ts[w1][j][k] = ts[w0][scores[j].second][k];
      }
    }
    for (int j = 0; j < mix_n; j++) {
      int rand_a = rand_sample(mt);
      int rand_b = rand_sample(mt);
      for (int k = 0; k < d; k++) {
        ts[w1][best_n + 2 * j][k] = ts[w1][rand_a][k];
        ts[w1][best_n + 2 * j + 1][k] = ts[w1][rand_b][k];
      }
      mix(ts[w1][best_n + 2 * j], ts[w1][best_n + 2 * j + 1], rand_mix(mt));
    }
    for (int j = 0; j < change_n; j++) {
      int rand_a = rand_sample(mt);
      for (int k = 0; k < d; k++) {
        ts[w1][best_n + 2 * mix_n + j][k] = ts[w1][rand_a][k];
      }
      ts[w1][best_n + 2 * mix_n + j][rand_d(mt)] = rand_z(mt);
    }

    swap(w0, w1);
  }

  if (!debug) {
    for (int i = 0; i < d; i++) {
      cout << ts[w0][scores[0].second][i] + 1 << endl;
    }
  }
}

int main(int argc, char **argv) {
  Main();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int d;                 // 日数
const int z = 26;      // コンテスト数
vector<int> c(z);      // コンテストiが開催されないペナルティ
vector<vector<int>> s; // i日目にコンテストjが開催された時の満足度

int score(vector<int> &t) {
  int sum = 0;
  vector<int> last(z, -1); // コンテストが最後に開催された日
  for (int i = 0; i < t.size(); i++) {
    sum += s[i][t[i]];
    last[t[i]] = i;
    for (int j = 0; j < z; j++) {
      sum -= c[j] * ((i + 1) - (last[j] + 1));
    }
  }
  return sum;
}

void mix(vector<int> &a, vector<int> &b, int index) {
  assert(index < d);
  assert(index >= 1);
  for (int i = 0; i < index; i++) {
    swap(a[i], b[i]);
  }
}

void Main() {
  cin >> d;
  s = vector<vector<int>>(d, vector<int>(z));
  for (int i = 0; i < z; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < z; j++) {
      cin >> s[i][j];
    }
  }

  const int n_change = 512;
  const int best_n = 256 + 128;
  const int mix_n = 256;
  const int change_n = 128;
  const int total_n = best_n + mix_n * 2 + change_n;

  std::mt19937 mt(rand());
  std::uniform_int_distribution<> rand_z(0, z - 1);
  std::uniform_int_distribution<> rand_mix(1, d - 1);
  std::uniform_int_distribution<> rand_d(0, d - 1);
  std::uniform_int_distribution<> rand_sample(0, best_n - 1);

  vector<vector<vector<int>>> ts(2,
                                 vector<vector<int>>(total_n, vector<int>(d)));
  for (int i = 0; i < ts[0].size(); i++) {
    for (int j = 0; j < d; j++) {
      ts[0][i][j] = rand_z(mt);
    }
  }

  vector<pair<int, int>> scores;
  scores.reserve(ts[0].size());
  int w0 = 0, w1 = 1;
  for (int i = 0; i < n_change; i++) {
    int ss = 0;
    if (i > 0) {
      scores.resize(best_n);
      ss = best_n;
    }
    for (int j = ss; j < ts[w0].size(); j++) {
      scores.push_back(make_pair(score(ts[w0][j]), j));
    }
    sort(scores.rbegin(), scores.rend());
    cerr << scores[0].first << endl;

    for (int j = 0; j < best_n; j++) {
      for (int k = 0; k < d; k++) {
        ts[w1][j][k] = ts[w0][scores[j].second][k];
      }
    }
    for (int j = 0; j < mix_n; j++) {
      int rand_a = rand_sample(mt);
      int rand_b = rand_sample(mt);
      for (int k = 0; k < d; k++) {
        ts[w1][best_n + 2 * j][k] = ts[w1][rand_a][k];
        ts[w1][best_n + 2 * j + 1][k] = ts[w1][rand_b][k];
      }
      mix(ts[w1][best_n + 2 * j], ts[w1][best_n + 2 * j + 1], rand_mix(mt));
    }
    for (int j = 0; j < change_n; j++) {
      int rand_a = rand_sample(mt);
      for (int k = 0; k < d; k++) {
        ts[w1][best_n + 2 * mix_n + j][k] = ts[w1][rand_a][k];
      }
      ts[w1][best_n + 2 * mix_n + j][rand_d(mt)] = rand_z(mt);
    }

    swap(w0, w1);
  }

  for (int i = 0; i < d; i++) {
    cout << ts[w0][scores[0].second][i] + 1 << endl;
  }
}

int main(int argc, char **argv) {
  Main();
  return 0;
}