本文主要是介绍poj1742 多重背包单调队列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
如题:http://poj.org/problem?id=1742
Description
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
Output
Sample Input
3 10 1 2 4 2 1 1 2 5 1 4 2 1 0 0
Sample Output
8 4
被删了一次 不想再写了,直接发代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int w[103];
int num[103];
int f[100005],q[100005];
int n,m;
int main()
{
// freopen("C:\\1.txt","r",stdin);
while(~scanf("%d%d",&n,&m)&&n&&m)
{
int i,j,d;
memset(f,0,sizeof(f));
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
f[0]=1;
int ret=0;
for(i=1;i<=n;i++)
{
if(num[i]==1){
for(j=m;j>=w[i];j--){
if(!f[j]&&f[j-w[i]])
{
f[j]=1;
ret++;
}
}
}
else if(num[i]*w[i]>=m)
{
for(j=w[i];j<=m;j++)
if(!f[j]&&f[j-w[i]])
{
f[j]=1;
ret++;
}
}
else
{
for(d=0;d<w[i];d++)
{
int st=0,ed=-1,sum=0;
for(j=d;j<=m;j+=w[i])
{
if(st+num[i]==ed)
sum-=q[st++];
q[++ed]=f[j];
sum+=f[j];
if(!f[j]&&sum)
{
f[j]=1;
ret++;
}
}
}
}
}
printf("%d\n",ret);
}
}
这篇关于poj1742 多重背包单调队列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!