Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VP vector<pair<int, int>>
#define VPP vector<pair<int, pair<int, int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = (1 << 30) - 1;
const ll INFL = 1LL << 60;
const ll mod = 10000000007;

std::chrono::system_clock::time_point start;
std::chrono::system_clock::time_point owari;

double calc_time() {
  owari = std::chrono::system_clock::now();
  return std::chrono::duration_cast<std::chrono::milliseconds>(owari - start)
      .count();
}

/*
inline unsigned long long xor128() {
        static unsigned long long rx = 411111789, ry = 9471111169, rz =
5212488629, rw = 886575123; unsigned long long rt = (rx ^ (rx << 11)); rx = ry;
        ry = rz;
        rz = rw;
        return (rw = (rw ^ (rw >> 19)) ^ (rt ^ (rt >> 8)));
}
*/
inline uint32_t xor128() {

  static uint32_t seed = 2;
  seed = seed ^ (seed << 7);
  seed = seed ^ (seed >> 12);
  return seed = seed & (~1);
}

double rand128() { return (double)(xor128() & 0xFFFF) / 0xFFFF; }

int D;

int pena[26];
int s[365][26];
int ans[365];
int pe[26];
int score = 0;

inline int calc() {
  int cur = 0;
  REP(i, 365) { cur += s[i][ans[i]]; }
  VI a(26, -1);
  REP(i, 365) {
    cur -= (i - a[ans[i]]) * (i - a[ans[i]] - 1) / 2 * pena[ans[i]];
    a[ans[i]] = i;
  }
  REP(i, 26) { cur -= (365 - a[i]) * (364 - a[i]) / 2 * pena[i]; }
  return cur;
}

int main() {
  cin >> D;

  start = std::chrono::system_clock::now();
  REP(i, 26) cin >> pena[i];
  REP(i, 365) REP(j, 26) { cin >> s[i][j]; }

  REP(i, D) { ans[i] = i % 26; }

  score = calc();

  REP(_, 2) {
    REP(i, 365) {
      REP(j, 26) {
        int tmp = ans[i];
        ans[i] = j;
        int cur = calc();
        if (cur < score) {
          ans[i] = tmp;
        } else
          score = cur;
      }
    }
  }

  FOR(j, 1, 8) {
    REP(i, 365) {
      if (calc_time() > 1900)
        break;
      if (j > 364)
        continue;
      REP(k, 26) REP(l, 26) {
        int tmp1 = ans[i];
        int tmp2 = ans[i + j];
        ans[i] = k;
        ans[i + j] = l;
        int cur = calc();
        if (cur < score) {
          ans[i] = tmp1;
          ans[i + j] = tmp2;
        } else
          score = cur;
      }
    }
  }
  cerr << score << endl;

  int loop = 0;

  while (true) {

    if (loop % 100 == 0) {
      if (calc_time() > 1950)
        break;
    }
    loop++;
    int a = xor128() % 363;
    int b = xor128() % 7;
    int c = xor128() % 7;
    b++;
    c++;
    if (a + b + c >= 365)
      continue;
    int tmp1 = ans[a];
    int tmp2 = ans[a + b];
    int tmp3 = ans[a + b + c];
    REP(i, 26) REP(j, 26) REP(k, 26) {
      ans[a] = i;
      ans[a + b] = j;
      ans[a + b + c] = k;
      int cur = calc();
      if (cur > score) {
        tmp1 = i;
        tmp2 = j;
        tmp3 = k;
        score = cur;
        cerr << score << endl;
      }
    }
    ans[a] = tmp1;
    ans[a + b] = tmp2;
    ans[a + b + c] = tmp3;
  }

  cerr << score << endl;

  REP(i, 365) { cout << ans[i] + 1 << endl; }
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VP vector<pair<int, int>>
#define VPP vector<pair<int, pair<int, int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = (1 << 30) - 1;
const ll INFL = 1LL << 60;
const ll mod = 10000000007;

std::chrono::system_clock::time_point start;
std::chrono::system_clock::time_point owari;

double calc_time() {
  owari = std::chrono::system_clock::now();
  return std::chrono::duration_cast<std::chrono::milliseconds>(owari - start)
      .count();
}

/*
inline unsigned long long xor128() {
        static unsigned long long rx = 411111789, ry = 9471111169, rz =
5212488629, rw = 886575123; unsigned long long rt = (rx ^ (rx << 11)); rx = ry;
        ry = rz;
        rz = rw;
        return (rw = (rw ^ (rw >> 19)) ^ (rt ^ (rt >> 8)));
}
*/
inline uint32_t xor128() {

  static uint32_t seed = 2;
  seed = seed ^ (seed << 7);
  seed = seed ^ (seed >> 12);
  return seed = seed & (~1);
}

double rand128() { return (double)(xor128() & 0xFFFF) / 0xFFFF; }

int D;

int pena[26];
int s[365][26];
int ans[365];
int pe[26];
int score = 0;

inline int calc() {
  int cur = 0;
  REP(i, 365) { cur += s[i][ans[i]]; }
  VI a(26, -1);
  REP(i, 365) {
    cur -= (i - a[ans[i]]) * (i - a[ans[i]] - 1) / 2 * pena[ans[i]];
    a[ans[i]] = i;
  }
  REP(i, 26) { cur -= (365 - a[i]) * (364 - a[i]) / 2 * pena[i]; }
  return cur;
}

int main() {
  cin >> D;

  start = std::chrono::system_clock::now();
  REP(i, 26) cin >> pena[i];
  REP(i, 365) REP(j, 26) { cin >> s[i][j]; }

  REP(i, D) { ans[i] = i % 26; }

  score = calc();

  REP(_, 2) {
    REP(i, 365) {
      REP(j, 26) {
        int tmp = ans[i];
        ans[i] = j;
        int cur = calc();
        if (cur < score) {
          ans[i] = tmp;
        } else
          score = cur;
      }
    }
  }

  FOR(j, 1, 8) {
    REP(i, 365) {
      if (calc_time() > 1900)
        break;
      if (j > 364)
        continue;
      REP(k, 26) REP(l, 26) {
        int tmp1 = ans[i];
        int tmp2 = ans[i + j];
        ans[i] = k;
        ans[i + j] = l;
        int cur = calc();
        if (cur < score) {
          ans[i] = tmp1;
          ans[i + j] = tmp2;
        } else
          score = cur;
      }
    }
  }
  cerr << score << endl;

  int loop = 0;

  while (calc_time() < 1900) {
    int a = xor128() % 363;
    int b = xor128() % 7;
    int c = xor128() % 7;
    b++;
    c++;
    if (a + b + c >= 365)
      continue;
    int tmp1 = ans[a];
    int tmp2 = ans[a + b];
    int tmp3 = ans[a + b + c];
    REP(i, 26) REP(j, 26) REP(k, 26) {
      ans[a] = i;
      ans[a + b] = j;
      ans[a + b + c] = k;
      int cur = calc();
      if (cur > score) {
        tmp1 = i;
        tmp2 = j;
        tmp3 = k;
        score = cur;
        cerr << score << endl;
      }
    }
    ans[a] = tmp1;
    ans[a + b] = tmp2;
    ans[a + b + c] = tmp3;
  }

  cerr << score << endl;

  REP(i, 365) { cout << ans[i] + 1 << endl; }
}