Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

const int kind = 26;
const int D = 365;
vector<int> c(kind);
vector<vector<int>> s(D, vector<int>(kind));
vector<int> state(D);
vector<int> last(kind);
vector<vector<pair<int, int>>> ranking(D, vector<pair<int, int>>(kind));

double start;
const double TLE = 1.98;

constexpr double ticks_per_sec = 3000000000;
constexpr double ticks_per_sec_inv = 1.0 / ticks_per_sec;
inline double rdtsc() { // in seconds
  uint32_t lo, hi;
  asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
  return (((uint64_t)hi << 32) | lo) * ticks_per_sec_inv;
}

inline uint64_t xor64(void) {
  static uint64_t x = 88172645463325252ULL;
  x = x ^ (x << 7);
  return x = x ^ (x >> 9);
}
inline int intRand(unsigned long long n) { return xor64() % n; }

int score() {
  rep(i, kind) { last[i] = 0; }

  int result = 0;

  rep(i, D) {
    result += s[i][ranking[i][state[i]].second];
    last[ranking[i][state[i]].second] = i + 1;

    rep(j, kind) { result -= ((i + 1) - last[j]) * c[j]; }
  }

  return result;
}

void init() {

  rep(i, D) {
    rep(j, kind) { ranking[i][j] = make_pair(s[i][j], j); }
    sort(ranking[i].rbegin(), ranking[i].rend());

    state[i] = i;
  }
}

void finish() {

  rep(i, D) { state[i] = ranking[i][state[i]].second; }
}

// 日付、変更前選択順位、変更後選択順位
pair<int, pair<int, int>> m;

void move() {
  int tmpd = intRand(D);
  if (state[tmpd] == 0 || state[tmpd] == 25) {
    if (state[tmpd] == 0) {
      m = make_pair(tmpd, make_pair(state[tmpd], state[tmpd] + 1));
      state[tmpd] = state[tmpd] + 1;
    } else {
      m = make_pair(tmpd, make_pair(state[tmpd], state[tmpd] - 1));
      state[tmpd] = state[tmpd] - 1;
    }
  } else {
    int r = intRand(2);
    if (r) {
      m = make_pair(tmpd, make_pair(state[tmpd], state[tmpd] - 1));
      state[tmpd] = state[tmpd] - 1;
    } else {
      m = make_pair(tmpd, make_pair(state[tmpd], state[tmpd] + 1));
      state[tmpd] = state[tmpd] + 1;
    }
  }
}

void undo() { state[m.first] = m.second.first; }

void sa() {

  vector<int> bestState = vector<int>(state);
  int bestScore = score();
  int preScore = bestScore;

  int loopCount = 0;
  int updateCount = 0;
  int undoCount = 0;

  while (rdtsc() - start < TLE) {
    move();
    int tmpScore = score();

    if (tmpScore >= bestScore) {
      bestScore = tmpScore;
      bestState = vector<int>(state);
      updateCount++;
    }

    double temp = 1000 * (TLE - (rdtsc() - start)) / TLE;
    double prob = exp((tmpScore - preScore) / temp);

    if (prob > 1.0 * intRand(1000000000) / 1000000000) {
      preScore = tmpScore;
    } else {
      undo();
      undoCount++;
    }

    loopCount++;
  }

  state = vector<int>(bestState);

  cerr << "  loop count: " << loopCount << endl;
  cerr << "  undo count: " << undoCount << endl;
  cerr << "update count: " << updateCount << endl;
}

void solve() {
  init();

  cerr << " start score: " << score() << endl;
  sa();

  cerr << "finish score: " << score() << endl;
  finish();
}

int main() {

  start = rdtsc();

  int tmp;
  cin >> tmp;
  rep(i, kind) { cin >> c[i]; }
  rep(i, D) {
    rep(j, kind) { cin >> s[i][j]; }
  }

  solve();

  rep(i, D) { printf("%d\n", state[i] + 1); }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

const int kind = 26;
const int D = 365;
vector<int> c(kind);
vector<vector<int>> s(D, vector<int>(kind));
vector<int> state(D);
vector<int> last(kind);
vector<vector<pair<int, int>>> ranking(D, vector<pair<int, int>>(kind));

double start;
const double TLE = 1.98;

constexpr double ticks_per_sec = 3000000000;
constexpr double ticks_per_sec_inv = 1.0 / ticks_per_sec;
inline double rdtsc() { // in seconds
  uint32_t lo, hi;
  asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
  return (((uint64_t)hi << 32) | lo) * ticks_per_sec_inv;
}

inline uint64_t xor64(void) {
  static uint64_t x = 88172645463325252ULL;
  x = x ^ (x << 7);
  return x = x ^ (x >> 9);
}
inline int intRand(unsigned long long n) { return xor64() % n; }

int score() {
  rep(i, kind) { last[i] = 0; }

  int result = 0;

  rep(i, D) {
    result += s[i][ranking[i][state[i]].second];
    last[ranking[i][state[i]].second] = i + 1;

    rep(j, kind) { result -= ((i + 1) - last[j]) * c[j]; }
  }

  return result;
}

void init() {

  rep(i, D) {
    rep(j, kind) { ranking[i][j] = make_pair(s[i][j], j); }
    sort(ranking[i].rbegin(), ranking[i].rend());

    state[i] = 0;
  }
}

void finish() {

  rep(i, D) { state[i] = ranking[i][state[i]].second; }
}

// 日付、変更前選択順位、変更後選択順位
pair<int, pair<int, int>> m;

void move() {
  int tmpd = intRand(D);
  if (state[tmpd] == 0 || state[tmpd] == 25) {
    if (state[tmpd] == 0) {
      m = make_pair(tmpd, make_pair(state[tmpd], state[tmpd] + 1));
      state[tmpd] = state[tmpd] + 1;
    } else {
      m = make_pair(tmpd, make_pair(state[tmpd], state[tmpd] - 1));
      state[tmpd] = state[tmpd] - 1;
    }
  } else {
    int r = intRand(2);
    if (r) {
      m = make_pair(tmpd, make_pair(state[tmpd], state[tmpd] - 1));
      state[tmpd] = state[tmpd] - 1;
    } else {
      m = make_pair(tmpd, make_pair(state[tmpd], state[tmpd] + 1));
      state[tmpd] = state[tmpd] + 1;
    }
  }
}

void undo() { state[m.first] = m.second.first; }

void sa() {

  vector<int> bestState = vector<int>(state);
  int bestScore = score();
  int preScore = bestScore;

  int loopCount = 0;
  int updateCount = 0;
  int undoCount = 0;

  while (rdtsc() - start < TLE) {
    move();
    int tmpScore = score();

    if (tmpScore >= bestScore) {
      bestScore = tmpScore;
      bestState = vector<int>(state);
      updateCount++;
    }

    double temp = 1000 * (TLE - (rdtsc() - start)) / TLE;
    double prob = exp((tmpScore - preScore) / temp);

    if (prob > 1.0 * intRand(1000000000) / 1000000000) {
      preScore = tmpScore;
    } else {
      undo();
      undoCount++;
    }

    loopCount++;
  }

  state = vector<int>(bestState);

  cerr << "  loop count: " << loopCount << endl;
  cerr << "  undo count: " << undoCount << endl;
  cerr << "update count: " << updateCount << endl;
}

void solve() {
  init();

  cerr << " start score: " << score() << endl;
  sa();

  cerr << "finish score: " << score() << endl;
  finish();
}

int main() {

  start = rdtsc();

  int tmp;
  cin >> tmp;
  rep(i, kind) { cin >> c[i]; }
  rep(i, D) {
    rep(j, kind) { cin >> s[i][j]; }
  }

  solve();

  rep(i, D) { printf("%d\n", state[i] + 1); }

  return 0;
}