Luzhiled's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ei1333/library

:heavy_check_mark: Knapsack Limitations(個数制限つきナップサック問題) $O(NW)$
(dp/knapsack-limitations.hpp)

概要

個数制限つきナップサック問題を次に示す.

重さ $w_i$, 価値 $v_i$ であるような $N$ 種類の品物がある. $i$ 番目の品物は $m_i$ 個まで選ぶことができる. 重さの和が $W$ 以下となるように選ぶとき, 価値の最大値を求めよ.

スライド最大値を用いた動的計画法により効率的に計算可能.

計算量

Required by

Verified with

Code

template< typename T, typename Compare = greater< T > >
vector< T > knapsack_limitations(const vector< int > &w, const vector< int > &m, const vector< T > &v,
                                 const int &W, const T &NG, const Compare &comp = Compare()) {
  const int N = (int) w.size();
  vector< T > dp(W + 1, NG), deqv(W + 1);
  dp[0] = T();
  vector< int > deq(W + 1);
  for(int i = 0; i < N; i++) {
    if(w[i] == 0) {
      for(int j = 0; j <= W; j++) {
        if(dp[j] != NG && comp(dp[j] + v[i] * m[i], dp[j])) {
          dp[j] = dp[j] + v[i] * m[i];
        }
      }
    } else {
      for(int a = 0; a < w[i]; a++) {
        int s = 0, t = 0;
        for(int j = 0; w[i] * j + a <= W; j++) {
          if(dp[w[i] * j + a] != NG) {
            auto val = dp[w[i] * j + a] - j * v[i];
            while(s < t && comp(val, deqv[t - 1])) --t;
            deq[t] = j;
            deqv[t++] = val;
          }
          if(s < t) {
            dp[j * w[i] + a] = deqv[s] + j * v[i];
            if(deq[s] == j - m[i]) ++s;
          }
        }
      }
    }
  }
  return dp;
}
#line 1 "dp/knapsack-limitations.hpp"
template< typename T, typename Compare = greater< T > >
vector< T > knapsack_limitations(const vector< int > &w, const vector< int > &m, const vector< T > &v,
                                 const int &W, const T &NG, const Compare &comp = Compare()) {
  const int N = (int) w.size();
  vector< T > dp(W + 1, NG), deqv(W + 1);
  dp[0] = T();
  vector< int > deq(W + 1);
  for(int i = 0; i < N; i++) {
    if(w[i] == 0) {
      for(int j = 0; j <= W; j++) {
        if(dp[j] != NG && comp(dp[j] + v[i] * m[i], dp[j])) {
          dp[j] = dp[j] + v[i] * m[i];
        }
      }
    } else {
      for(int a = 0; a < w[i]; a++) {
        int s = 0, t = 0;
        for(int j = 0; w[i] * j + a <= W; j++) {
          if(dp[w[i] * j + a] != NG) {
            auto val = dp[w[i] * j + a] - j * v[i];
            while(s < t && comp(val, deqv[t - 1])) --t;
            deq[t] = j;
            deqv[t++] = val;
          }
          if(s < t) {
            dp[j * w[i] + a] = deqv[s] + j * v[i];
            if(deq[s] == j - m[i]) ++s;
          }
        }
      }
    }
  }
  return dp;
}
Back to top page