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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

// constexpr int INF = 1e9;
constexpr ll INF = 1LL << 61;
constexpr ll mod = 1e9 + 7;

class xorShift {
  uint32_t x, y, z, w;
  static const double TO_DOUBLE;

public:
  xorShift() {
    x = 123456789;
    y = 362436069;
    z = 521288629;
    w = 88675123;
  }
  xorShift(uint32_t seed) {
    x = seed = 1812433253u * (seed ^ (seed >> 30));
    y = seed = 1812433253u * (seed ^ (seed >> 30)) + 1;
    z = seed = 1812433253u * (seed ^ (seed >> 30)) + 2;
    w = seed = 1812433253u * (seed ^ (seed >> 30)) + 3;
  }
  uint32_t nextInt(uint32_t n) {
    uint32_t t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w % n;
  }
  uint32_t nextInt() {
    uint32_t t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }
  double nextDouble() { return nextInt() * TO_DOUBLE; }
};
const double xorShift::TO_DOUBLE = 1.0 / (1LL << 32);

class Timer {
  chrono::time_point<chrono::system_clock> start;

public:
  Timer() { start = chrono::system_clock::now(); }
  double duration() {
    return chrono::duration_cast<chrono::milliseconds>(
               chrono::system_clock::now() - start)
        .count();
    // return
    // chrono::duration<chrono::milliseconds>(chrono::system_clock::now()-
    // start).count();
  }
};

Timer T;
xorShift rnd;
constexpr double TIME_LIMIT = 1950;

int D = 365;
vector<int> c(26);
vector<vector<int>> s(D, vector<int>(26));

struct Ans {
  vector<int> ans;
  int score = 0;
  Ans() : ans(D) {}
};

struct State {
  int score = 0, down = 0;
  vector<int> ans, last;
  State() : last(D, 0){};
  int update(int t) {
    ans.push_back(t);
    down = 0;
    int d = ans.size();
    last[t] = d;
    score += s[d - 1][t];
    int maxd = 0;
    REP(i, 26) {
      score -= c[i] * (d - last[i]);
      down -= c[i] * (d + 1 - last[i]);
      maxd = max(maxd, c[i] * (d + 1 - last[i]));
    }
    return score;
  }
  bool operator<(const State right) const {
    return score + down < right.score + right.down;
  }
  bool operator>(const State left) const {
    return score + down > left.score + left.down;
  }
};

int cal_score(Ans &ans) {
  int manzoku = 0;
  vector<int> last(26, 0);
  REP(i, D) {
    manzoku += s[i][ans.ans[i]];
    last[ans.ans[i]] = i + 1;
    REP(j, 26) { manzoku -= c[j] * (i + 1 - last[j]); }
  }
  return manzoku;
}
struct Solver {
  Ans bestAns;
  Solver() {
    // init_ans();
    // init_ans2();
    // bestAns.score = cal_score(bestAns);
  }
  void init_ans() {
    REP(i, D) { bestAns.ans[i] = i % 26; }
  }
  void init_ans2() {
    vector<int> last(26, 0);
    REP(i, D) {
      int mint = 0, maxd = 0;
      REP(j, 26) {
        int loss = c[j] * ((i + 1) - last[j]);
        if (loss > maxd) {
          maxd = loss;
          mint = j;
        }
      }
      bestAns.ans[i] = mint;
      last[mint] = i + 1;
    }
  }
  void solve() {
    State bestState = beam_search();
    bestAns.ans = bestState.ans;
    bestAns.score = bestState.score;
    yama();
    print_ans();
  }
  State beam_search() {
    priority_queue<State> NowStates;
    NowStates.push(State());
    int k = 100; // ビーム幅
    REP(t, D) {
      priority_queue<State> NextStates;
      REP(i, k) {
        if (NowStates.empty())
          break;
        State NowState = NowStates.top();
        NowStates.pop();
        REP(j, 26) {
          State NextState = NowState;
          NextState.update(j);
          NextStates.push(NextState);
        }
      }
      NowStates = NextStates;
    }

    State BestState = NowStates.top();
    NowStates.pop();
    REP(i, 100) {
      if (NowStates.empty())
        break;
      if (BestState.score < NowStates.top().score) {
        BestState = NowStates.top();
      }
      NowStates.pop();
    }
    return BestState;
  }
  void yama() {
    Ans currentAns = bestAns;
    while (true) {
      if (T.duration() > TIME_LIMIT)
        break;
      int a = rnd.nextInt(D), b = rnd.nextInt(D);
      while (a == b) {
        b = rnd.nextInt(D);
      }
      swap(currentAns.ans[a], currentAns.ans[b]);
      int score = cal_score(currentAns);
      if (score > currentAns.score) {
        currentAns.score = score;
      } else {
        swap(currentAns.ans[a], currentAns.ans[b]);
      }
    }
    if (bestAns.score < currentAns.score) {
      bestAns = currentAns;
    }
  }
  void print_ans() {
    REP(i, D) { cout << bestAns.ans[i] + 1 << endl; }
  }
};
int main() {
  cin >> D;
  REP(i, 26) { cin >> c[i]; }
  REP(i, D) {
    REP(j, 26) { cin >> s[i][j]; }
  }
  Solver solver;
  solver.solve();
  return 0;
}


Error: -6
free(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

// constexpr int INF = 1e9;
constexpr ll INF = 1LL << 61;
constexpr ll mod = 1e9 + 7;

class xorShift {
  uint32_t x, y, z, w;
  static const double TO_DOUBLE;

public:
  xorShift() {
    x = 123456789;
    y = 362436069;
    z = 521288629;
    w = 88675123;
  }
  xorShift(uint32_t seed) {
    x = seed = 1812433253u * (seed ^ (seed >> 30));
    y = seed = 1812433253u * (seed ^ (seed >> 30)) + 1;
    z = seed = 1812433253u * (seed ^ (seed >> 30)) + 2;
    w = seed = 1812433253u * (seed ^ (seed >> 30)) + 3;
  }
  uint32_t nextInt(uint32_t n) {
    uint32_t t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w % n;
  }
  uint32_t nextInt() {
    uint32_t t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }
  double nextDouble() { return nextInt() * TO_DOUBLE; }
};
const double xorShift::TO_DOUBLE = 1.0 / (1LL << 32);

class Timer {
  chrono::time_point<chrono::system_clock> start;

public:
  Timer() { start = chrono::system_clock::now(); }
  double duration() {
    return chrono::duration_cast<chrono::milliseconds>(
               chrono::system_clock::now() - start)
        .count();
    // return
    // chrono::duration<chrono::milliseconds>(chrono::system_clock::now()-
    // start).count();
  }
};

Timer T;
xorShift rnd;
constexpr double TIME_LIMIT = 1950;

int D = 365;
vector<int> c(26);
vector<vector<int>> s(D, vector<int>(26));

struct Ans {
  vector<int> ans;
  int score = 0;
  Ans() : ans(D) {}
};

struct State {
  int score = 0, down = 0;
  vector<int> ans, last;
  State() : last(D, 0){};
  int update(int t) {
    ans.push_back(t);
    down = 0;
    int d = ans.size();
    last[t] = d;
    score += s[d - 1][t];
    int maxd = 0;
    REP(i, 26) {
      score -= c[i] * (d - last[i]);
      down -= c[i] * (d + 1 - last[i]);
      maxd = max(maxd, c[i] * (d + 1 - last[i]));
    }
    return score;
  }
  bool operator<(const State right) const {
    return score + down < right.score + right.down;
  }
  bool operator>(const State left) const {
    return score + down > left.score + left.down;
  }
};

int cal_score(Ans &ans) {
  int manzoku = 0;
  vector<int> last(26, 0);
  REP(i, D) {
    manzoku += s[i][ans.ans[i]];
    last[ans.ans[i]] = i + 1;
    REP(j, 26) { manzoku -= c[j] * (i + 1 - last[j]); }
  }
  return manzoku;
}
struct Solver {
  Ans bestAns;
  Solver() {
    // init_ans();
    // init_ans2();
    // bestAns.score = cal_score(bestAns);
  }
  void init_ans() {
    REP(i, D) { bestAns.ans[i] = i % 26; }
  }
  void init_ans2() {
    vector<int> last(26, 0);
    REP(i, D) {
      int mint = 0, maxd = 0;
      REP(j, 26) {
        int loss = c[j] * ((i + 1) - last[j]);
        if (loss > maxd) {
          maxd = loss;
          mint = j;
        }
      }
      bestAns.ans[i] = mint;
      last[mint] = i + 1;
    }
  }
  void solve() {
    State bestState = beam_search();
    bestAns.ans = bestState.ans;
    bestAns.score = bestState.score;
    yama();
    print_ans();
  }
  State beam_search() {
    priority_queue<State> NowStates;
    NowStates.push(State());
    int k = 100; // ビーム幅
    REP(t, D) {
      priority_queue<State> NextStates;
      REP(i, k) {
        if (NowStates.empty())
          break;
        State NowState = NowStates.top();
        NowStates.pop();
        REP(j, 26) {
          State NextState = NowState;
          NextState.update(j);
          NextStates.push(NextState);
        }
      }
      NowStates = NextStates;
    }

    State BestState;
    REP(i, 100) {
      if (NowStates.empty())
        break;
      if (BestState.score < NowStates.top().score) {
        BestState = NowStates.top();
      }
      NowStates.pop();
    }
    return BestState;
  }
  void yama() {
    Ans currentAns = bestAns;
    while (true) {
      if (T.duration() > TIME_LIMIT)
        break;
      int a = rnd.nextInt(D), b = rnd.nextInt(D);
      while (a == b) {
        b = rnd.nextInt(D);
      }
      swap(currentAns.ans[a], currentAns.ans[b]);
      int score = cal_score(currentAns);
      if (score > currentAns.score) {
        currentAns.score = score;
      } else {
        swap(currentAns.ans[a], currentAns.ans[b]);
      }
    }
    if (bestAns.score < currentAns.score) {
      bestAns = currentAns;
    }
  }
  void print_ans() {
    REP(i, D) { cout << bestAns.ans[i] + 1 << endl; }
  }
};
int main() {
  cin >> D;
  REP(i, 26) { cin >> c[i]; }
  REP(i, D) {
    REP(j, 26) { cin >> s[i][j]; }
  }
  Solver solver;
  solver.solve();
  return 0;
}