Problem p02618 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <ctime> //時間を測ります
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random> //乱択します
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;

#define TIMELIMIT 1.95
#define SA_COEFF 10000

#define START_TEMP 50
#define END_TEMP 1

inline unsigned long long rdtsc() {
#ifdef __amd64
  unsigned long long a, d;
  __asm__ volatile("rdtsc" : "=a"(a), "=d"(d));
  return (d << 32) | a;
#else
  unsigned long long x;
  __asm__ volatile("rdtsc" : "=A"(x));
  return x;
#endif
}
inline double gettime() { return rdtsc() / 2793344000.0; }
inline double elapsed(double st) { return gettime() - st; }

static unsigned long rand_x = 123456789, rand_y = 362436069, rand_z = 521288629,
                     rand_w = 88675123;
std::random_device rnd{};
void init_randxor() {
  unsigned long s = rnd();
  rand_w ^= s;
  rand_w ^= rand_w >> 21;
  rand_w ^= rand_w << 35;
  rand_w ^= rand_w >> 4;
  rand_w *= 2685821657736338717LL;
}
inline unsigned int randxor() {
  unsigned int t;
  t = (rand_x ^ (rand_x << 11));
  rand_x = rand_y;
  rand_y = rand_z;
  rand_z = rand_w;
  return (rand_w = (rand_w ^ (rand_w >> 19)) ^ (t ^ (t >> 8)));
}

struct ANSTYPE {
  vector<int> t;
};

// **** write variable here ****
int d;
int n = 26;
vector<int> c(26);
vector<vector<int>> s;

// *****************************

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

ANSTYPE ans;

void output() {
  for (int i = 0; i < d; i++)
    cout << ans.t[i] << endl;
}

void init() {
  // ans=
  ans.t = vector<int>(d);
  for (int i = 0; i < d; i++)
    ans.t[i] = i % 26 + 1;
}

int value(ANSTYPE &in_ans) {
  int point = 0;

  return point;
}

int next(ANSTYPE &in_ans, int bef_score) {
  int bef = 0;
  int aft = 0;

  return bef_score + (aft - bef);
}

void SA() {
  double st = gettime();
  double now_time;
  bool FORCE_NEXT;
  double probability;
  double temp;

  init();

  ANSTYPE now_ans = ans;
  ANSTYPE best_ans = now_ans;
  ANSTYPE next_ans;

  int best_score = value(ans);
  int next_score = 0;
  int now_score = best_score;

  int cnt = 0;
  int bad_cnt = 0;

  while (elapsed(st) < TIMELIMIT) {
    cnt++;
    next_ans = now_ans;

    next_score = next(next_ans, now_score);

    now_time = elapsed(st);

    temp = START_TEMP +
           (END_TEMP - START_TEMP) * now_time / (TIMELIMIT); // 現在温度
    probability = exp((next_score - now_score) / temp);

    FORCE_NEXT = (probability > (double(randxor() % 100100) / 100100));
    //		FORCE_NEXT = false;

    if (next_score > now_score or (FORCE_NEXT and next_score > 0)) {
      if (next_score < now_score)
        bad_cnt++;
      now_score = next_score;
      now_ans = next_ans;
    }
    if (now_score > best_score) {
      best_score = now_score;
      best_ans = now_ans;
    }
    //		cerr << best_score << " : " << next_score << endl;;
  }
  ans = best_ans;
  //	cerr << "loop \\t: " << cnt << endl;
  //	cerr << "bad use\\t: " << bad_cnt << endl;
  //	cerr << "score \\t: " << value(ans) << endl;
}

void solve() { SA(); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  init_randxor();
  input();
  solve();
  output();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <ctime> //時間を測ります
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random> //乱択します
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;

#define TIMELIMIT 1.95
#define SA_COEFF 10000

#define START_TEMP 50
#define END_TEMP 1

inline unsigned long long rdtsc() {
#ifdef __amd64
  unsigned long long a, d;
  __asm__ volatile("rdtsc" : "=a"(a), "=d"(d));
  return (d << 32) | a;
#else
  unsigned long long x;
  __asm__ volatile("rdtsc" : "=A"(x));
  return x;
#endif
}
inline double gettime() { return rdtsc() / 2793344000.0; }
inline double elapsed(double st) { return gettime() - st; }

static unsigned long rand_x = 123456789, rand_y = 362436069, rand_z = 521288629,
                     rand_w = 88675123;
std::random_device rnd{};
void init_randxor() {
  unsigned long s = rnd();
  rand_w ^= s;
  rand_w ^= rand_w >> 21;
  rand_w ^= rand_w << 35;
  rand_w ^= rand_w >> 4;
  rand_w *= 2685821657736338717LL;
}
inline unsigned int randxor() {
  unsigned int t;
  t = (rand_x ^ (rand_x << 11));
  rand_x = rand_y;
  rand_y = rand_z;
  rand_z = rand_w;
  return (rand_w = (rand_w ^ (rand_w >> 19)) ^ (t ^ (t >> 8)));
}

struct ANSTYPE {
  vector<int> t;
};

// **** write variable here ****
int d;
int n = 26;
vector<int> c(26);
vector<vector<int>> s;

// *****************************

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

ANSTYPE ans;

void output() {
  for (int i = 0; i < d; i++)
    cout << ans.t[i] << endl;
}

void init() {
  // ans=
  ans.t = vector<int>(d);
  for (int i = 0; i < d; i++)
    ans.t[i] = i % 26 + 1;
}

int value(ANSTYPE &in_ans) {
  int point = 0;
  vector<int> bef(26, 0);
  for (int i = 0; i < d; i++) {
    int now = in_ans.t[i];
    point += s[i][now];
    bef[now] = i + 1;
    for (int j = 0; j < n; j++)
      point -= (c[j] * (i + 1 - bef[j]));
  }

  return point;
}

int next(ANSTYPE &in_ans, int bef_score) {
  int bef = 0;
  int aft = 0;

  return bef_score + (aft - bef);
}

void SA() {
  double st = gettime();
  double now_time;
  bool FORCE_NEXT;
  double probability;
  double temp;

  init();

  ANSTYPE now_ans = ans;
  ANSTYPE best_ans = now_ans;
  ANSTYPE next_ans;

  int best_score = value(ans);
  int next_score = 0;
  int now_score = best_score;

  int cnt = 0;
  int bad_cnt = 0;

  while (elapsed(st) < TIMELIMIT) {
    cnt++;
    next_ans = now_ans;

    next_score = next(next_ans, now_score);

    now_time = elapsed(st);

    temp = START_TEMP +
           (END_TEMP - START_TEMP) * now_time / (TIMELIMIT); // 現在温度
    probability = exp((next_score - now_score) / temp);

    FORCE_NEXT = (probability > (double(randxor() % 100100) / 100100));
    //		FORCE_NEXT = false;

    if (next_score > now_score or (FORCE_NEXT and next_score > 0)) {
      if (next_score < now_score)
        bad_cnt++;
      now_score = next_score;
      now_ans = next_ans;
    }
    if (now_score > best_score) {
      best_score = now_score;
      best_ans = now_ans;
    }
    //		cerr << best_score << " : " << next_score << endl;;
  }
  ans = best_ans;
  //	cerr << "loop \\t: " << cnt << endl;
  //	cerr << "bad use\\t: " << bad_cnt << endl;
  //	cerr << "score \\t: " << value(ans) << endl;
}

void solve() { SA(); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  init_randxor();
  input();
  solve();
  output();
  return 0;
}