poj1742 单调队列优化多重背包

31 篇文章 0 订阅
27 篇文章 0 订阅

 

如题:http://poj.org/problem?id=1742

Coins
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 31258 Accepted: 10640

Description

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

Input

The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The last test case is followed by two zeros.

Output

For each test case output the answer on a single line.

Sample Input

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output

8
4

Source

 

 

 

由这一题的M,N太大,使用二进制拆分的O(MN∑logni)直接wa,于是想起以前见过的单调队列去均摊复杂度到O(MN),就是将容量拆分成余数+k*v[i],找出单调性,入队,每次在窗口大小为n,n=min(num[i],V/v[i])里取出最大值更新。又去查了资料。终于基本搞清楚了。

这篇文章讲得很好:http://blog.csdn.net/flyinghearts/article/details/5898183,大家点进去看吧,贴上我借鉴作者部分代码后的AC代码。

还有就是一点,这一题只能用f[i]表示当前价值i能不能装出来,而使用平常的单调队列去算价值==背包容量也会超。

 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXV 100005
#define MAXN 105
#define max(a,b)(a>b?a:b)

bool a[MAXV];
int w[MAXN];
int num[MAXN];
bool f[MAXV];


void multipack(int V,int v,int n)
{
 int i,j,k;;
 if(n==1)
 {
  for(i=V;i>=v;i--)
   f[i]=f[i]||f[i-v];
  return;
 }
 if(n*v>=V)
 {
  for(i=v;i<=V;i++)
   f[i]=f[i]|f[i-v];
  return;
 }
 for(j=0;j<v;j++)
 {
  int sum=0;
  bool *p1=a,*p2=a-1;
  for(k=j;k<=V;k+=v)
  {
   if(p2==p1+n)
    sum-=*p1++;
   *++p2=f[k];
   sum+=f[k];
   if(sum!=0)
    f[k]=1;
  }
 }
}

int main()
{
// freopen("C:\\1.txt","r",stdin);
 int n,m;
 while(~scanf("%d%d",&n,&m))
 {
  if(n==0&&m==0)
   break;
  int i;
  for(i=0;i<n;i++)
   scanf("%d",&w[i]);
  for(i=0;i<n;i++)
   scanf("%d",&num[i]);
  memset(f,false,sizeof(f));
  f[0]=true;
  for(i=0;i<n;i++)
   multipack(m,w[i],num[i]);
  int res=0;
  for(i=1;i<=m;i++)
   if(f[i]==true)
    res++;
  cout<<res<<endl;
 }
 return 0;
}

 

 

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值