Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#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 2.10
#define SA_COEFF 10000

#define START_TEMP 1200
#define END_TEMP 1

#define INF 1000000007

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;
vector<int> mx;

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

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

ANSTYPE ans;

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

void init() {
  // ans=
  ans.t = vector<int>(d);
  for (int i = 0; i < d; i++)
    ans.t[i] = i % 26;
  vector<int> bef(26, 0);
  for (int i = 0; i < d; i++) {
    int nx = -1;
    int mx = (-1) * INF;
    for (int j = 0; j < n; j++) {
      int now = s[i][j];
      for (int k = 0; k < n; k++) {
        if (j == k)
          continue;
        now -= (c[k] * (i + 1 - bef[k]));
      }
      if (mx < now) {
        mx = now;
        nx = j;
      }
    }
    bef[nx] = i + 1;
    ans.t[i] = nx;
  }
}

long long value(ANSTYPE &in_ans) {
  long long 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;

  in_ans.t[randxor() % d] = randxor() % n;
  return value(in_ans);

  return bef_score + (aft - bef);
}

int next2(ANSTYPE &in_ans, int bef_score) {
  int x = randxor() % (d - 1);
  int xx = in_ans.t[x];
  in_ans.t[x] = in_ans.t[x + 1];
  in_ans.t[x + 1] = xx;

  return value(in_ans);
}

int next3(ANSTYPE &in_ans, int bef_score) {
  int x = randxor() % (d - 1);
  in_ans.t[x] = mx[x];
  return value(in_ans);
}

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;

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

  int cnt = 0;
  int bad_cnt = 0;

  //	cerr << "score \t: " << value(ans) << endl;
  while (elapsed(st) < TIMELIMIT) {
    cnt++;
    next_ans = now_ans;

    if (elapsed(st) / TIMELIMIT > 2.0 / 3.0 and randxor() % 2 == 0)
      next_score = next2(next_ans, now_score);
    else {
      if (randxor() % 4 == 0)
        next_score = next3(next_ans, now_score);
      else
        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;;
  }
  ANSTYPE bef_best_ans = best_ans;
  int bef_best_score = best_score;

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

    if (elapsed(st) / TIMELIMIT > 2.0 / 3.0 and randxor() % 2 == 0)
      next_score = next2(next_ans, now_score);
    else {
      if (randxor() % 4 == 0)
        next_score = next3(next_ans, now_score);
      else
        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;
  if (best_score < bef_best_score)
    ans = bef_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;
}


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.05
#define SA_COEFF 10000

#define START_TEMP 1200
#define END_TEMP 1

#define INF 1000000007

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;
vector<int> mx;

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

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

ANSTYPE ans;

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

void init() {
  // ans=
  ans.t = vector<int>(d);
  for (int i = 0; i < d; i++)
    ans.t[i] = i % 26;
  vector<int> bef(26, 0);
  for (int i = 0; i < d; i++) {
    int nx = -1;
    int mx = (-1) * INF;
    for (int j = 0; j < n; j++) {
      int now = s[i][j];
      for (int k = 0; k < n; k++) {
        if (j == k)
          continue;
        now -= (c[k] * (i + 1 - bef[k]));
      }
      if (mx < now) {
        mx = now;
        nx = j;
      }
    }
    bef[nx] = i + 1;
    ans.t[i] = nx;
  }
}

long long value(ANSTYPE &in_ans) {
  long long 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;

  in_ans.t[randxor() % d] = randxor() % n;
  return value(in_ans);

  return bef_score + (aft - bef);
}

int next2(ANSTYPE &in_ans, int bef_score) {
  int x = randxor() % (d - 1);
  int xx = in_ans.t[x];
  in_ans.t[x] = in_ans.t[x + 1];
  in_ans.t[x + 1] = xx;

  return value(in_ans);
}

int next3(ANSTYPE &in_ans, int bef_score) {
  int x = randxor() % (d - 1);
  in_ans.t[x] = mx[x];
  return value(in_ans);
}

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;

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

  int cnt = 0;
  int bad_cnt = 0;

  //	cerr << "score \t: " << value(ans) << endl;
  while (elapsed(st) < TIMELIMIT) {
    cnt++;
    next_ans = now_ans;

    if (elapsed(st) / TIMELIMIT > 2.0 / 3.0 and randxor() % 2 == 0)
      next_score = next2(next_ans, now_score);
    else {
      if (randxor() % 4 == 0)
        next_score = next3(next_ans, now_score);
      else
        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;;
  }
  ANSTYPE bef_best_ans = best_ans;
  int bef_best_score = best_score;

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

    if (elapsed(st) / TIMELIMIT > 2.0 / 3.0 and randxor() % 2 == 0)
      next_score = next2(next_ans, now_score);
    else {
      if (randxor() % 4 == 0)
        next_score = next3(next_ans, now_score);
      else
        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;
  if (best_score < bef_best_score)
    ans = bef_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;
}