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

#pragma GCC optimize("O3")
// #pragma GCC target ("tune=native")
#pragma GCC target("avx2")
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

struct chtime {
  chrono::system_clock::time_point stm;
  auto nget() { return chrono::system_clock::now(); }
  chtime() { stm = nget(); }
  uint64_t gtm() {
    return chrono::duration_cast<chrono::milliseconds>(nget() - stm).count();
  }
  void disp() { cerr << gtm() << "\\n"; }
};
chtime tmq;

ll CTM;
const ll TLMT = 2 * 1000 - 50;
mt19937 mran((unsigned)time(nullptr)); //(20200528);
int D;
VI C(26), USD(26);
vector<VI> S(366, VI(26));

inline int xran(int su) { return mran() % su; }
inline int xran2(int su) { return mran() & su; } // su = (2^n)-1
inline ll xran9(ll su) { return mran() & su; }

vector<double> mkfq(int n = 100) {
  if (n == 0)
    n = 1;
  vector<double> zzx(n + 1);
  rep(i, n + 1) zzx[i] = 1 - (1 - i / (1.0 * n)) * (1 - i / (1.0 * n));
  return zzx;
}

inline ll clpt(const VI &t) {
  VI usd(27, 0);
  // VL rtn(D);
  ll ans = 0;
  rep(i, D) {
    ans += S[i][t[i]];
    usd[t[i]] = i + 1;
    rep(j, 26) { ans -= (ll)C[j] * (i + 1 - usd[j]); }
    // dbg(i, ans);
  }
  return ans;
}

inline ll dpt2(const VI &t, int dy, int rt) {
  ll ans = 0;
  ans += S[dy][rt];
  int q1 = dy, q2 = dy;
  for (q1 = dy; q1 >= 0 && t[q1] != rt; q1--)
    ;
  for (q2 = dy; q2 < D && t[q2] != rt; q2++)
    ;
  ans += 1LL * C[rt] * (dy - q1) * (q2 - dy);
  return ans;
}

int main() {
  typedef pair<ll, VI> LVI;
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> D;
  rep(i, 26) cin >> C[i];
  rep(i, D) rep(j, 26) cin >> S[i][j];
  auto fqd = mkfq(100);
  for (auto &z : fqd)
    z /= 100;
  int tct = 0, chcnt = 0, upcnt = 0;
  rep(i, 99) xran2(1023);

  VI idt(D);
  rep(i, D) idt[i] = xran(26); //(i+3)%26;
  auto mypt = clpt(idt);
  dbg("Init", mypt);
  LVI BST = LVI(mypt, idt);
  auto ALBST = BST;
  for (; (tct & 511) != 509 || (CTM = tmq.gtm()) < TLMT; tct++) {

    int d1 = xran(D), p2 = xran(26);
    while (idt[d1] == p2)
      p2 = xran(26);
    int op1 = idt[d1];
    ll df = dpt2(idt, d1, p2);
    idt[d1] = p2;
    df -= dpt2(idt, d1, op1);
    if (df >= 0) {
      mypt += df;
      BST = LVI(mypt, idt);
      upcnt++;
    } else {
      double zan = 1.0 - 1.0 * CTM / TLMT;
      int qw = zan * 100;
      qw = max(0, min(99, qw));
      if (xran2(1023) < fqd[qw] * 1023 && BST.first > 1 &&
          abs((BST.first - mypt) / BST.first) < fqd[qw]) {
        mypt += df;
        BST = LVI(mypt, idt);
        chcnt++;
      } else {
        idt[d1] = op1;
      }
    }
    ALBST = max(ALBST, BST);
  }
  dbg("res", ALBST.first, BST.first, tmq.gtm(), "cnt=", tct, upcnt, chcnt);
  rep(i, D) cout << ALBST.second[i] + 1 << "\\n";
  dbg(clpt(ALBST.second));
  return 0;
}
// gpp mar.cpp && a < tester\\sample2.in.txt


Error: WA

Fixed submission:

#pragma GCC optimize("O3")
// #pragma GCC target ("tune=native")
#pragma GCC target("avx2")
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

struct chtime {
  chrono::system_clock::time_point stm;
  auto nget() { return chrono::system_clock::now(); }
  chtime() { stm = nget(); }
  uint64_t gtm() {
    return chrono::duration_cast<chrono::milliseconds>(nget() - stm).count();
  }
  void disp() { cerr << gtm() << "\\n"; }
};
chtime tmq;

ll CTM;
const ll TLMT = 2 * 1000 - 50;
const ll ZGZ = 1000000;
mt19937 mran(202005282);
int D;
VI C(26), USD(26);
vector<VI> S(366, VI(26));

inline int xran(int su) { return mran() % su; }
inline int xran2(int su) { return mran() & su; } // su = (2^n)-1
inline ll xran9(ll su) { return mran() & su; }

vector<double> mkfq(int n = 100) {
  vector<double> zzx(n + 1);
  rep(i, n + 1) zzx[i] = 1 - (1 - i / (1.0 * n)) * (1 - i / (1.0 * n));
  return zzx;
}

inline ll clpt(const VI &t) {
  VI usd(27, 0);
  // VL rtn(D);
  ll ans = 0;
  rep(i, D) {
    ans += S[i][t[i]];
    usd[t[i]] = i + 1;
    rep(j, 26) { ans -= (ll)C[j] * (i + 1 - usd[j]); }
    // dbg(i, ans);
  }
  return ans;
}

inline ll dpt2(const VI &t, int dy, int rt) {
  ll ans = 0;
  ans += S[dy][rt];
  int q1 = dy, q2 = dy;
  for (q1 = dy; q1 >= 0 && t[q1] != rt; q1--)
    ;
  for (q2 = dy; q2 < D && t[q2] != rt; q2++)
    ;
  ans += 1LL * C[rt] * (dy - q1) * (q2 - dy);
  return ans;
}

int main() {
  typedef pair<ll, VI> LVI;
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> D;
  rep(i, 26) cin >> C[i];
  rep(i, D) rep(j, 26) cin >> S[i][j];
  auto fqd = mkfq(100);
  for (auto &z : fqd)
    z /= 100;
  int tct = 0, chcnt = 0, upcnt = 0;
  rep(i, 99) xran2(1023);

  VI idt(D);
  rep(i, D) idt[i] = i % 26;
  auto mypt = clpt(idt);
  dbg("Init", mypt);
  LVI BST = LVI(mypt, idt);
  auto ALBST = BST;
  for (; (tct & 511) != 509 || (CTM = tmq.gtm()) < TLMT; tct++) {

    int d1 = xran(D), p2 = xran(26);
    while (idt[d1] == p2)
      p2 = xran(26);
    int op1 = idt[d1];
    ll df = dpt2(idt, d1, p2);
    idt[d1] = p2;
    df -= dpt2(idt, d1, op1);
    if (df >= 0) {
      mypt += df;
      BST = LVI(mypt, idt);
      upcnt++;
    } else {
      double zan = 1.0 - 1.0 * CTM / TLMT;
      int qw = zan * 100;
      qw = max(0, min(99, qw));
      if (xran2(1023) < fqd[qw] * 1023 &&
          abs((BST.first - mypt) / BST.first) < fqd[qw]) {
        mypt += df;
        BST = LVI(mypt, idt);
        chcnt++;
      } else {
        idt[d1] = op1;
      }
    }
    ALBST = max(ALBST, BST);
  }
  dbg("res", ALBST.first, BST.first, tmq.gtm(), "cnt=", tct, upcnt, chcnt);
  rep(i, D) cout << ALBST.second[i] + 1 << "\\n";
  dbg(clpt(ALBST.second));
  return 0;
}
// gpp mar.cpp && a < tester\\sample2.in.txt