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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int MAX_D = 365;
const int MAX_type = 26;
const int BeamWidth = 50;

int D;
int c[MAX_D + 5];
int s[MAX_D + 5][MAX_type + 5];

struct State {
  vector<int> contest;
  vector<int> last;
  int day;
  int score;

  State() {
    // cout<<"hoge"<<endl;
    day = 0;
    score = 0;
    for (int i = 0; i < MAX_type + 5; ++i)
      last.push_back(0);
    // fill(last.begin(),last.end(),0);
    // cout<<last[0]<<endl;
  }

  void update(int type, int d) {
    contest.push_back(type);
    // last[type]=d;
    day++;
    score += s[d][type];
    for (int i = 0; i < MAX_type; ++i) {
      score -= c[i] * (day - last[i]);
    }
  }

  State nextState(State now, int type) {
    State next;
    next = now;
    next.update(type, now.day + 1);
    return next;
  }
};

bool operator<(const State &state1, const State &state2) {
  return state1.score < state2.score;
};

bool operator>(const State &state1, const State &state2) {
  return state1.score > state2.score;
};

State beamsearch() {
  int D_ = D;
  priority_queue<State, vector<State>, greater<State>> nowpq;
  for (int type = 0; type < MAX_type; ++type) {
    State s;
    // cout<<s.score<<endl;
    // cout<<s.nextState(s,type).contest[0]<<endl;
    nowpq.push(s.nextState(s, type));
  }
  while (nowpq.size() > BeamWidth)
    nowpq.pop();
  D_--;
  while (D_--) {
    // cout<<D<<endl;
    priority_queue<State, vector<State>, greater<State>> nextpq;
    while (nowpq.size()) {
      State now = nowpq.top();
      nowpq.pop();
      for (int type = 0; type < MAX_type; ++type) {
        nextpq.push(now.nextState(now, type));
        // cout<<now.nextState(now,type).score<<endl;
      }
    }
    while (nextpq.size() > BeamWidth)
      nextpq.pop();
    nowpq = nextpq;
  }
  while (nowpq.size() > 1)
    nowpq.pop();
  State ret = nowpq.top();
  return ret;
}

void input() {
  cin >> D;
  for (int i = 0; i < MAX_type; ++i)
    cin >> c[i];
  for (int i = 0; i < D; ++i) {
    // cout<<D<<endl;
    for (int j = 0; j < MAX_type; ++j) {
      cin >> s[i][j];
    }
  }
}

signed main() {
  input();
  auto res = beamsearch();
  if (res.contest.size() != D)
    cout << 0 / 0 << endl;
  for (auto u : res.contest) {
    cout << u + 1 << endl;
  }
  // cout<<"score is "<<res.score<<endl;
  // for(int i=0;i<MAX_type;++i)cout<<res.last[i]<<endl;
  // int score=0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))

typedef pair<int, int> PI;
typedef pair<int, pair<int, int>> PII;
static const int INF = 1010000000000000017LL;
static const double eps = 1e-12;
static const double pi = 3.14159265358979323846;
static const int dx[4] = {1, -1, 0, 0};
static const int dy[4] = {0, 0, 1, -1};
static const int ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
static const int ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int MAX_D = 365;
const int MAX_type = 26;
const int BeamWidth = 30;

int D;
int c[MAX_D + 5];
int s[MAX_D + 5][MAX_type + 5];

struct State {
  vector<int> contest;
  int last[MAX_type + 5] = {0};
  int day = 0;
  int score;

  void update(int type, int d) {
    contest.push_back(type);
    last[d] = type;
    day++;
    score += s[d][type];
    for (int i = 0; i < MAX_type; ++i)
      score -= c[i] * (day - last[i]);
  }

  State nextState(State now, int type) {
    State next = now;
    next.update(type, now.day + 1);
    return next;
  }
};

bool operator<(const State &state1, const State &state2) {
  return state1.score < state2.score;
};

bool operator>(const State &state1, const State &state2) {
  return state1.score > state2.score;
};

vector<int> beamsearch() {
  priority_queue<State> nowpq;
  for (int type = 0; type < MAX_type; ++type) {
    State s;
    // cout<<s.nextState(s,type).contest[0]<<endl;
    nowpq.push(s.nextState(s, type));
  }
  while (nowpq.size() >= BeamWidth)
    nowpq.pop();
  D--;
  while (D--) {
    // cout<<D<<endl;
    priority_queue<State> nextpq;
    while (nowpq.size()) {
      State now = nowpq.top();
      nowpq.pop();
      for (int type = 1; type <= MAX_type; ++type) {
        nextpq.push(now.nextState(now, type));
      }
    }
    while (nextpq.size() >= BeamWidth)
      nextpq.pop();
    nowpq = nextpq;
  }
  while (nowpq.size() > 1)
    nowpq.pop();
  State ret = nowpq.top();
  return ret.contest;
}

void input() {
  cin >> D;
  for (int i = 0; i < D; ++i)
    cin >> c[i];
  for (int i = 0; i < D; ++i) {
    for (int j = 0; j < MAX_type; ++j) {
      cin >> s[i][j];
    }
  }
}

signed main() {
  input();
  for (auto u : beamsearch()) {
    cout << u << endl;
  }
}