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

#include <bits/stdc++.h>

/*==========type============*/
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::pair<std::string, ll> psl;

/*==========constant============*/
#define INF INT_MAX
#define INF_L LONG_MAX
#define INF_LL LLONG_MAX
#define MOD 1000000007

/*==========function============*/
#define REP(ii, jj, nn) for (ll ii = jj; ii < (nn); ii++)
#define RREP(ii, nn, jj) for (ll ii = nn; jj < ii; ii--)
#define each(i, ...) for (auto &&i : __VA_ARGS__)
#define ALL(vec) (vec).begin(), (vec).end()
#define sum(...) accumulate(ALL(__VA_ARGS__), 0LL)
#define dsum(...) accumulate(ALL(__VA_ARGS__), 0.0)
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
template <class T> inline auto max(const T &a) { return *max_element(ALL(a)); }
template <class T> inline auto min(const T &a) { return *min_element(ALL(a)); }
inline ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
inline ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

using namespace std;

// ルートクラス
class Root {
public:
  ll cost;
  vector<ll> scd;
  ll day;

  Root(ll day, ll cost, vector<ll> scd) {
    this->day = day, this->cost = cost, this->scd = scd;
  }
  Root(const Root &r) {
    this->scd = r.scd;
    this->cost = r.cost;
    this->day = r.day;
  }
  Root(){};

  Root &operator=(const Root &container) {
    this->scd = container.scd;
    this->cost = container.cost;
    this->day = container.day;
    return *this;
  }
  bool operator>(const Root &container) const {
    return (cost < container.cost);
  }
};

ll Range = 100;

ll reduce(vector<ll> &c, vector<ll> &scd, ll d) {
  ll ret = 0;
  vector<ll> last(26, 0);
  REP(i, 0, scd.size()) last[scd[i] - 1] = i + 1;
  REP(i, 0, 26) ret += c[i] * (d - last[i]);
  return ret;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll d;
  cin >> d;
  vector<ll> c(d);
  REP(i, 0, d) cin >> c[i];
  vector<vector<ll>> s(d, vector<ll>(26, 0));
  REP(j, 0, d) REP(i, 0, 26) cin >> s[j][i];

  priority_queue<Root, vector<Root>, greater<Root>> que;

  REP(i, 0, 26) {
    vector<ll> scd;
    scd.push_back(i + 1);
    que.push(Root(1, s[0][i], scd));
  }

  ll day = 1;
  while (que.size() > 0) {
    // cout << "day:" << day << endl;
    if (day >= d)
      break;
    ll rng = que.size() <= Range ? que.size() : Range;
    vector<Root> rr(rng);
    // cout << "size:" << que.size() << endl;
    REP(i, 0, rng) {
      rr[i] = que.top();
      que.pop();
      // cout << "rr:" << rr[i].cost << endl;
    }
    while (!que.empty())
      que.pop();
    REP(i, 0, rr.size()) {
      // cout << rr.size() << " " << rr[i].scd[0] << endl;
      REP(j, 0, 26) {
        ll dday = rr[i].day;
        vector<ll> sscd = rr[i].scd;
        sscd.emplace_back(j + 1);
        ll ccost = rr[i].cost + s[day][j] - reduce(c, sscd, d);
        // cout << "ad" << endl;
        // cout << "dday:" << dday << " cost:" << ccost << " size:" <<
        // sscd.size() << endl; cout << "rr.size(): " << rr.size() << endl; cout
        // << "que.size(): " << que.size() << endl;
        que.push(Root(day, ccost, sscd));
        // cout << "aa" << endl;
      }
    }
    day++;
  }

  Root ans = que.top();
  REP(i, 0, d) cout << ans.scd[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

/*==========type============*/
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::pair<std::string, ll> psl;

/*==========constant============*/
#define INF INT_MAX
#define INF_L LONG_MAX
#define INF_LL LLONG_MAX
#define MOD 1000000007

/*==========function============*/
#define REP(ii, jj, nn) for (ll ii = jj; ii < (nn); ii++)
#define RREP(ii, nn, jj) for (ll ii = nn; jj < ii; ii--)
#define each(i, ...) for (auto &&i : __VA_ARGS__)
#define ALL(vec) (vec).begin(), (vec).end()
#define sum(...) accumulate(ALL(__VA_ARGS__), 0LL)
#define dsum(...) accumulate(ALL(__VA_ARGS__), 0.0)
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
template <class T> inline auto max(const T &a) { return *max_element(ALL(a)); }
template <class T> inline auto min(const T &a) { return *min_element(ALL(a)); }
inline ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
inline ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

using namespace std;

// ルートクラス
class Root {
public:
  ll cost;
  vector<ll> scd;
  ll day;

  Root(ll day, ll cost, vector<ll> scd) {
    this->day = day, this->cost = cost, this->scd = scd;
  }
  Root(const Root &r) {
    this->scd = r.scd;
    this->cost = r.cost;
    this->day = r.day;
  }
  Root(){};

  Root &operator=(const Root &container) {
    this->scd = container.scd;
    this->cost = container.cost;
    this->day = container.day;
    return *this;
  }
  bool operator>(const Root &container) const {
    return (cost < container.cost);
  }
};

ll Range = 10000;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll d;
  cin >> d;
  vector<ll> c(d);
  REP(i, 0, d) cin >> c[i];
  vector<vector<ll>> s(d, vector<ll>(26, 0));
  REP(j, 0, d) REP(i, 0, 26) cin >> s[j][i];

  priority_queue<Root, vector<Root>, greater<Root>> que;

  REP(i, 0, 26) {
    vector<ll> scd;
    scd.push_back(i + 1);
    que.push(Root(1, s[0][i], scd));
  }

  ll day = 0;
  while (que.size() > 0) {
    // cout << "day:" << day << endl;
    if (day >= d)
      break;
    ll rng = que.size() <= Range ? que.size() : Range;
    vector<Root> rr(rng);
    // cout << "size:" << que.size() << endl;
    REP(i, 0, rng) {
      rr[i] = que.top();
      que.pop();
      // cout << "rr:" << rr[i].cost << endl;
    }
    while (!que.empty())
      que.pop();
    REP(i, 0, rr.size()) {
      // cout << rr.size() << " " << rr[i].scd[0] << endl;
      REP(j, 0, 26) {
        ll dday = rr[i].day;
        ll ccost = rr[i].cost + s[day][j];
        vector<ll> sscd = rr[i].scd;
        sscd.emplace_back(j + 1);
        // cout << "ad" << endl;
        // cout << "dday:" << dday << " cost:" << ccost << " size:" <<
        // sscd.size() << endl; cout << "rr.size(): " << rr.size() << endl; cout
        // << "que.size(): " << que.size() << endl;
        que.push(Root(day + 1, ccost, sscd));
        // cout << "aa" << endl;
      }
    }
    day++;
  }

  Root ans = que.top();
  REP(i, 0, d) cout << ans.scd[i] << endl;

  return 0;
}