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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define endl '\\n'
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for (int i = (b); i >= (int)(e); --i)

#define sim template <class n
sim, class s > ostream &operator<<(ostream &p, pair<n, s> x) {
  return p << "<" << x.f << ", " << x.s << ">";
}
sim > auto operator<<(ostream &p, n y) ->
      typename enable_if<!is_same<n, string>::value,
                         decltype(y.begin(), p)>::type {
  int o = 0;
  p << "{";
  for (auto c : y) {
    if (o++)
      p << ", ";
    p << c;
  }
  return p << "}";
}
void dor() { cerr << endl; }
sim, class... s > void dor(n p, s... y) {
  cerr << p << " ";
  dor(y...);
}
sim, class s > void mini(n &p, s y) {
  if (p > y)
    p = y;
}
sim, class s > void maxi(n &p, s y) {
  if (p < y)
    p = y;
}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif

#define I(x) #x " =", (x), " "
#define A(a, i) #a "[" #i " =", i, "] =", a[i], " "

int R(int a, int b) { return rand() % (b - a + 1) + a; }

const int MXD = 400;
int res[MXD];
int ever_best = -1e9;
int contest[MXD];
int s[MXD][28];
int c[28];

int last[28];
set<int> when[28];
int D;
int sc;

void clear() {
  FOR(i, 1, 26) when[i].clear();
  FOR(i, 1, 26) when[i].insert(0);
  FOR(i, 1, 26) when[i].insert(D + 1);
  FOR(i, 1, 26) last[i] = 0;
  sc = 0;
}

void check() {
  if (sc > ever_best) {
    ever_best = sc;
    FOR(i, 1, D) res[i] = contest[i];
  }
}

void random_sol() { FOR(i, 1, D) contest[i] = R(1, 26); }
void get_score() {
  sc = 0;

  FOR(i, 1, D) {
    sc += s[i][contest[i]];
    last[contest[i]] = i;
    FOR(j, 1, 26)
    sc -= c[j] * (i - last[j]);

    when[contest[i]].insert(i);
  }
}
void zachlan() {
  sc = 0;

  FOR(i, 1, D) {
    int best = -1;
    int best_val = -1e9;

    int minus = 0;

    FOR(j, 1, 26) {
      int cand = s[i][j] + c[j] * (i - last[j]);
      minus += c[j] * (i - last[j]);
      if (cand > best_val) {
        best_val = cand;
        best = j;
      }
    }
    contest[i] = best;

    when[contest[i]].insert(i);
    sc += best_val - minus;
    last[contest[i]] = i;
  }
}

int cost(int day, int who) {
  int prev = *--when[who].upper_bound(day - 1);
  int nxt = *when[who].upper_bound(day);

  int ile = day - prev;
  int razy = nxt - day;
  return ile * razy * c[who];
}
// int cost_dodam(int day, int who)
//   {
//   int prev = *--when[who].upper_bound(day-1);
//   int nxt = *when[who].upper_bound(day);

//   int ile = day - prev;
//   int razy = nxt - day;
//   return ile * razy * c;
//   }

LD max_time = 1.9;

void change(int day, int who) {
  sc = sc - cost(day, contest[day]) - s[day][contest[day]];
  when[contest[day]].erase(day);

  contest[day] = who;

  sc = sc + cost(day, contest[day]) + s[day][contest[day]];
  when[contest[day]].insert(day);
}

double WallTime() { return (LD)clock() / CLOCKS_PER_SEC; }
mt19937 rng;

void poprawiaj(LD czas) {
  int cnt = 0;
  int iter = 0;
  int same_cnt = 0;

  double temp0 = 3.3e3;
  double temp1 = 2e-3;
  LD startTime = WallTime();
  LD endTime = czas + startTime;

  while (clock() < max_time * CLOCKS_PER_SEC) {
    double curTime = WallTime();
    if (curTime >= endTime)
      break;
    double frac = (curTime - startTime) / (endTime - startTime);
    double temp = exp(log(temp0) * (1 - frac) + log(temp1) * frac);
    int day = R(1, D);
    int who = contest[day];
    int przed = sc;

    sc = sc - cost(day, who) - s[day][who];
    when[who].erase(day);

    int best = 0;
    int best_val = -1e9;
    FOR(i, 1, 26) {
      //      debug(day, i, cost(day, i));

      int cand = cost(day, i) + s[day][i];
      if (cand > best_val) {
        best_val = cand;
        best = i;
      }
    }

    //    debug(who, best, best_val);

    when[best].insert(day);
    contest[day] = best;
    sc += best_val;

    //    if(iter % 1000 == 0)debug(I(sc), iter);
    //    break;
    iter++;
    check();

    if (sc == przed)
      same_cnt++;
    else
      same_cnt = 0;

    int res = sc;
    day = R(1, D);
    who = R(1, 26);
    int prev_who = contest[day];
    change(day, who);
    int new_res = sc;
    if (new_res > res ||
        bernoulli_distribution(exp((new_res - res) / temp))(rng)) {
      if (new_res < res)
        cnt++;
    } else {
      change(day, prev_who);
    }
  }
  debug(I(sc), iter, ever_best, I(cnt));
}

void modify(int res) {
#ifdef DEB
  FILE *f = fopen("result.txt", "r");
  int val = 0;
  fscanf(f, "%d", &val);
  fclose(f);

  val += res;

  f = fopen("result.txt", "w");
  fprintf(f, "\\n%d\\n\\n", val);
  fclose(f);
#endif
}

int main() {
  cin >> D;

  FOR(i, 1, 26) scanf("%d", &c[i]);
  FOR(i, 1, D) { FOR(j, 1, 26) scanf("%d", &s[i][j]); }

  int iters = 0;
  while (clock() < 1.5 * CLOCKS_PER_SEC) {
    clear();

    if (iters < 1000)
      zachlan();
    else {
      random_sol();
      get_score();
    }
    poprawiaj(0.2);
    iters++;
  }

  FOR(i, 1, D) contest[i] = res[i];
  clear();
  get_score();
  poprawiaj(0.5);

  debug(I(ever_best));
  modify(ever_best);
  FOR(i, 1, D) cout << res[i] << endl;
}


Error: WA

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define endl '\\n'
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for (int i = (b); i >= (int)(e); --i)

#define sim template <class n
sim, class s > ostream &operator<<(ostream &p, pair<n, s> x) {
  return p << "<" << x.f << ", " << x.s << ">";
}
sim > auto operator<<(ostream &p, n y) ->
      typename enable_if<!is_same<n, string>::value,
                         decltype(y.begin(), p)>::type {
  int o = 0;
  p << "{";
  for (auto c : y) {
    if (o++)
      p << ", ";
    p << c;
  }
  return p << "}";
}
void dor() { cerr << endl; }
sim, class... s > void dor(n p, s... y) {
  cerr << p << " ";
  dor(y...);
}
sim, class s > void mini(n &p, s y) {
  if (p > y)
    p = y;
}
sim, class s > void maxi(n &p, s y) {
  if (p < y)
    p = y;
}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif

#define I(x) #x " =", (x), " "
#define A(a, i) #a "[" #i " =", i, "] =", a[i], " "

int R(int a, int b) { return rand() % (b - a + 1) + a; }

const int MXD = 400;
int res[MXD];
int ever_best = -1e9;
int contest[MXD];
int s[MXD][28];
int c[28];

int last[28];
set<int> when[28];
int D;
int sc;

void clear() {
  FOR(i, 1, 26) when[i].clear();
  FOR(i, 1, 26) when[i].insert(0);
  FOR(i, 1, 26) when[i].insert(D + 1);
  FOR(i, 1, 26) last[i] = 0;
  sc = 0;
}

void check() {
  if (sc > ever_best) {
    ever_best = sc;
    FOR(i, 1, D) res[i] = contest[i];
  }
}

void random_sol() { FOR(i, 1, D) contest[i] = R(1, 26); }
void get_score() {
  sc = 0;

  FOR(i, 1, D) {
    sc += s[i][contest[i]];
    last[contest[i]] = i;
    FOR(j, 1, 26)
    sc -= c[j] * (i - last[j]);

    when[contest[i]].insert(i);
  }
}
void zachlan() {
  sc = 0;

  FOR(i, 1, D) {
    int best = -1;
    int best_val = -1e9;

    int minus = 0;

    FOR(j, 1, 26) {
      int cand = s[i][j] + c[j] * (i - last[j]);
      minus += c[j] * (i - last[j]);
      if (cand > best_val) {
        best_val = cand;
        best = j;
      }
    }
    contest[i] = best;

    when[contest[i]].insert(i);
    sc += best_val - minus;
    last[contest[i]] = i;
  }
}

int cost(int day, int who) {
  int prev = *--when[who].upper_bound(day - 1);
  int nxt = *when[who].upper_bound(day);

  int ile = day - prev;
  int razy = nxt - day;
  return ile * razy * c[who];
}
// int cost_dodam(int day, int who)
//   {
//   int prev = *--when[who].upper_bound(day-1);
//   int nxt = *when[who].upper_bound(day);

//   int ile = day - prev;
//   int razy = nxt - day;
//   return ile * razy * c;
//   }

LD max_time = 1.9;

void change(int day, int who) {
  sc = sc - cost(day, contest[day]) - s[day][contest[day]];
  when[contest[day]].erase(day);

  contest[day] = who;

  sc = sc + cost(day, contest[day]) + s[day][contest[day]];
  when[contest[day]].insert(day);
}

double WallTime() { return (LD)clock() / CLOCKS_PER_SEC; }
mt19937 rng;

void poprawiaj(LD czas) {
  int cnt = 0;
  int iter = 0;
  int same_cnt = 0;

  double temp0 = 3e3;
  double temp1 = 2e-3;
  LD startTime = WallTime();
  LD endTime = czas + startTime;

  while (clock() < max_time * CLOCKS_PER_SEC) {
    double curTime = WallTime();
    if (curTime >= endTime)
      break;
    double frac = (curTime - startTime) / (endTime - startTime);
    double temp = exp(log(temp0) * (1 - frac) + log(temp1) * frac);
    int day = R(1, D);
    int who = contest[day];
    int przed = sc;

    sc = sc - cost(day, who) - s[day][who];
    when[who].erase(day);

    int best = 0;
    int best_val = -1e9;
    FOR(i, 1, 26) {
      //      debug(day, i, cost(day, i));

      int cand = cost(day, i) + s[day][i];
      if (cand > best_val) {
        best_val = cand;
        best = i;
      }
    }

    //    debug(who, best, best_val);

    when[best].insert(day);
    contest[day] = best;
    sc += best_val;

    //    if(iter % 1000 == 0)debug(I(sc), iter);
    //    break;
    iter++;
    check();

    if (sc == przed)
      same_cnt++;
    else
      same_cnt = 0;

    int res = sc;
    day = R(1, D);
    who = R(1, 26);
    int prev_who = contest[day];
    change(day, who);
    int new_res = sc;
    if (new_res > res ||
        bernoulli_distribution(exp((new_res - res) / temp))(rng)) {
      if (new_res < res)
        cnt++;
    } else {
      change(day, prev_who);
    }
  }
  debug(I(sc), iter, ever_best, I(cnt));
}

void modify(int res) {
  FILE *f = fopen("result.txt", "r");
  int val = 0;
  fscanf(f, "%d", &val);
  fclose(f);

  val += res;

  f = fopen("result.txt", "w");
  fprintf(f, "\\n%d\\n\\n", val);
  fclose(f);
}

int main() {
  cin >> D;

  FOR(i, 1, 26) scanf("%d", &c[i]);
  FOR(i, 1, D) { FOR(j, 1, 26) scanf("%d", &s[i][j]); }

  int iters = 0;
  while (clock() < 1.5 * CLOCKS_PER_SEC) {
    clear();

    if (iters < 1000)
      zachlan();
    else {
      random_sol();
      get_score();
    }
    poprawiaj(0.2);
    iters++;
  }

  FOR(i, 1, D) contest[i] = res[i];
  clear();
  get_score();
  poprawiaj(0.5);

  debug(I(ever_best));
  modify(ever_best);
  FOR(i, 1, D) cout << res[i] << endl;
}