多重背包

单调队列优化:

#include <bits/stdc++.h>
using namespace std;
int n,vv,h,t,dp[100000],id[100000];
int v[100000],w[100000],q[100000],num[100000];
void insert(int x,int y)
{
  int i;
  for (i=t;i>=h;i--)
  {
      if (q[i]>y) break;
  }
  q[++i]=y; id[i]=x;t=i;
}
int main()
{
  freopen("noip.in","r",stdin);
  freopen("noip.out","w",stdout);
  cin>>n>>vv;
  for (int i=1;i<=n;i++)
  {
      cin>>v[i]>>w[i]>>num[i];
  }
  for (int i=1;i<=n;i++)
  {
      for (int j=0;j<=v[i]-1;j++)
      {
        h=1,t=0;
        for (int k=0;k<vv/v[i]+(vv%v[i]>=j);k++)
        {
            if (h<=t&&id[h]+num[i]<k) h++;
            int tmp=dp[k*v[i]+j];
            if (h<=t)
            {
              dp[k*v[i]+j]=max(dp[k*v[i]+j],q[h]+k*w[i]);
        }
        insert(k,tmp-k*w[i]);
      }
    }
  }
  cout<<dp[vv]; 
} 

 

posted @ 2018-01-31 11:20  尹吴潇  阅读(118)  评论(0编辑  收藏  举报