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

#include <bits/stdc++.h>

#include <utility>
using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
const int INF = 1e9;
using Pii = pair<int, int>;

const int con_type = 26;
int D = 365;
int C[con_type];
int S[365][con_type];
vector<int> ans;
int last[con_type];

struct State {
  int cost;
  vector<int> ans;
  vector<int> last;

  State() {
    cost = 0;
    last = vector<int>(26, -1);
  }

  State(int cost, vector<int> ans, vector<int> last)
      : cost(cost), ans(ans), last(last){};

  bool operator<(const State &s) const { return cost < s.cost; }

  bool operator>(const State &s) const { return cost > s.cost; }
};

void solve() {
  priority_queue<State> Q;
  Q.push(State());
  REP(d, D) {
    priority_queue<State> nQ;
    while (!Q.empty()) {
      State now = Q.top();
      Q.pop();
      vector<Pii> add_cost;
      int sum = 0;
      int type_cost[26];
      REP(i, con_type) {
        sum += (d - now.last[i]) * C[i];
        type_cost[i] = (d - now.last[i]) * C[i];
      }
      REP(i, con_type) {
        add_cost.emplace_back(Pii(S[d][i] + (d - now.last[i]) * C[i] - sum, i));
      }
      sort(add_cost.begin(), add_cost.end(), greater<Pii>());
      REP(i, 12) {
        int id = add_cost[i].second;
        State next = now;
        next.cost += S[d][id];
        next.cost -= sum;
        next.cost += type_cost[id];
        next.ans.emplace_back(id);
        next.last[id] = d;
        nQ.push(next);
      }
    }
    Q = priority_queue<State>();
    REP(i, min(800, (int)nQ.size())) {
      Q.push(nQ.top());
      nQ.pop();
    }
  }

  ans = Q.top().ans;
}

void input() {
  cin >> D;
  REP(i, con_type) {
    cin >> C[i];
    last[i] = -1;
  }
  REP(i, D) REP(j, con_type) cin >> S[i][j];
}

void output() { REP(i, D) cout << ans[i] + 1 << endl; }

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  input();
  solve();
  output();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <utility>
using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
const int INF = 1e9;
using Pii = pair<int, int>;

const int con_type = 26;
int D = 365;
int C[con_type];
int S[365][con_type];
vector<int> ans;
int last[con_type];

struct State {
  int cost;
  vector<int> ans;
  vector<int> last;

  State() {
    cost = 0;
    last = vector<int>(26, -1);
  }

  State(int cost, vector<int> ans, vector<int> last)
      : cost(cost), ans(ans), last(last){};

  bool operator<(const State &s) const { return cost < s.cost; }

  bool operator>(const State &s) const { return cost > s.cost; }
};

void solve() {
  priority_queue<State> Q;
  Q.push(State());
  REP(d, D) {
    priority_queue<State> nQ;
    while (!Q.empty()) {
      State now = Q.top();
      Q.pop();
      vector<Pii> add_cost;
      int sum = 0;
      int type_cost[26];
      REP(i, con_type) {
        sum += (d - now.last[i]) * C[i];
        type_cost[i] = (d - now.last[i]) * C[i];
      }
      REP(i, con_type) {
        add_cost.emplace_back(Pii(S[d][i] + (d - now.last[i]) * C[i] - sum, i));
      }
      sort(add_cost.begin(), add_cost.end(), greater<Pii>());
      REP(i, 12) {
        int id = add_cost[i].second;
        State next = now;
        next.cost += S[d][id];
        next.cost -= sum;
        next.cost += type_cost[id];
        next.ans.emplace_back(id);
        next.last[id] = d;
        nQ.push(next);
      }
    }
    Q = priority_queue<State>();
    REP(i, min(1000, (int)nQ.size())) {
      Q.push(nQ.top());
      nQ.pop();
    }
  }

  ans = Q.top().ans;
}

void input() {
  cin >> D;
  REP(i, con_type) {
    cin >> C[i];
    last[i] = -1;
  }
  REP(i, D) REP(j, con_type) cin >> S[i][j];
}

void output() { REP(i, D) cout << ans[i] + 1 << endl; }

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  input();
  solve();
  output();

  return 0;
}