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

#include <algorithm>
#include <chrono>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, a, b) for (ll i = a; i > b; i--)
#define CST(x) cout << fixed << setprecision(x) // 小数点以下の桁数指定
#define ct(a) cout << a << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (1); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
static const double pi = 3.141592653589793;
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;

using namespace std::chrono;
inline ll get_time_millisec(void) {
  return static_cast<ll>(
      duration_cast<milliseconds>(steady_clock::now().time_since_epoch())
          .count());
}

unsigned long xor128() {
  static unsigned long x = 123456789, y = 362436069, z = 521288629,
                       w = 88675123;
  unsigned long t = (x ^ (x << 11));
  x = y;
  y = z;
  z = w;
  return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

inline int getRange(int l, int r) { return xor128() % (r + 1 - l) + l; }

class Solver {

private:
  // 要素
  ll D;
  ll C[26];
  ll S[365][26];
  ll date[365];

public:
  void input() {
    cin >> D;
    FOR(i, 0, 26) cin >> C[i];
    FOR(i, 0, D) {
      FOR(j, 0, 26) { cin >> S[i][j]; }
    }
  }

  int calcScore() {
    int score = 0;

    return score;
  }
  void init() {}
  void rndchange() {}

  void output() {
    FOR(i, 0, D) { cout << xor128() % 26 + 1 << endl; }
  }
};

Solver solve(void) {

  Solver currentSeq;

  currentSeq.input();
  currentSeq.init();

  Solver bestSeq = currentSeq;
  int bestScore = currentSeq.calcScore();
  int currentScore = bestScore;

  const ll saTimeStart = get_time_millisec();

  const ll saTimeEnd = saTimeStart + 5; // milli
  double saTimeCurrent = saTimeStart;

  while (saTimeCurrent < saTimeEnd) {

    Solver nextSeq = currentSeq;

    nextSeq.rndchange();

    const int nextScore = nextSeq.calcScore();

    const ll T = saTimeEnd - saTimeStart;
    const ll t = saTimeCurrent - saTimeStart;
    const ll R = 10000;

    // const bool FORCE_NEXT = R * (T - t) > T*((xor128()) % (R));
    const bool FORCE_NEXT = false;
    /*
    double startTemp = 20;
    double endTemp = 10;
    double temp = startTemp + (endTemp - startTemp)*t / T;
    double probability = exp((nextScore - currentScore) / temp);
    const bool FORCE_NEXT = probability > (double)(xor128() % R) / (double)R;
    */
    // const bool FORCE_NEXT = (xor128() % R) >998;

    if (nextScore > currentScore || FORCE_NEXT) {
      currentScore = nextScore;
      currentSeq = nextSeq;
    }

    if (currentScore > bestScore) {

      bestScore = currentScore;

      // cout << bestScore << endl;
      bestSeq = currentSeq;
    }
    saTimeCurrent = get_time_millisec();
  }

  return bestSeq;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  Solver ans = solve();
  ans.output();

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, a, b) for (ll i = a; i > b; i--)
#define CST(x) cout << fixed << setprecision(x) // 小数点以下の桁数指定
#define ct(a) cout << a << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (1); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
static const double pi = 3.141592653589793;
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;

using namespace std::chrono;
inline ll get_time_millisec(void) {
  return static_cast<ll>(
      duration_cast<milliseconds>(steady_clock::now().time_since_epoch())
          .count());
}

unsigned long xor128() {
  static unsigned long x = 123456789, y = 362436069, z = 521288629,
                       w = 88675123;
  unsigned long t = (x ^ (x << 11));
  x = y;
  y = z;
  z = w;
  return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

inline int getRange(int l, int r) { return xor128() % (r + 1 - l) + l; }

class Solver {

private:
  // 要素
  ll D;
  ll C[26];
  ll S[365][26];
  ll date[365];

public:
  void input() {
    cin >> D;
    FOR(i, 0, 26) cin >> C[i];
    FOR(i, 0, D) {
      FOR(j, 0, 26) { cin >> S[i][j]; }
    }
  }

  int calcScore() {
    int score = 0;

    return score;
  }
  void init() {}
  void rndchange() {}

  void output() {
    FOR(i, 0, D) { cout << xor128() % 26 + 1 << endl; }
  }
};

Solver solve(void) {

  Solver currentSeq;

  currentSeq.input();
  currentSeq.init();

  Solver bestSeq = currentSeq;
  int bestScore = currentSeq.calcScore();
  int currentScore = bestScore;

  const ll saTimeStart = get_time_millisec();

  const ll saTimeEnd = saTimeStart + 2995; // milli
  double saTimeCurrent = saTimeStart;

  while (saTimeCurrent < saTimeEnd) {

    Solver nextSeq = currentSeq;

    nextSeq.rndchange();

    const int nextScore = nextSeq.calcScore();

    const ll T = saTimeEnd - saTimeStart;
    const ll t = saTimeCurrent - saTimeStart;
    const ll R = 10000;

    // const bool FORCE_NEXT = R * (T - t) > T*((xor128()) % (R));
    const bool FORCE_NEXT = false;
    /*
    double startTemp = 20;
    double endTemp = 10;
    double temp = startTemp + (endTemp - startTemp)*t / T;
    double probability = exp((nextScore - currentScore) / temp);
    const bool FORCE_NEXT = probability > (double)(xor128() % R) / (double)R;
    */
    // const bool FORCE_NEXT = (xor128() % R) >998;

    if (nextScore > currentScore || FORCE_NEXT) {
      currentScore = nextScore;
      currentSeq = nextSeq;
    }

    if (currentScore > bestScore) {

      bestScore = currentScore;

      // cout << bestScore << endl;
      bestSeq = currentSeq;
    }
    saTimeCurrent = get_time_millisec();
  }

  return bestSeq;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  Solver ans = solve();
  ans.output();

  return 0;
}