POJ 1742 coins 多重背包单调队列优化

Description

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example, 

N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10 

means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each. 

Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine. 

Notes: 
@ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc. 

Input

The program input is from standard input. Each data set in the input stands for a particular transaction and has the format: 

cash N n1 D1 n2 D2 ... nN DN 

where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers in the input. The input data are correct. 

Output

For each set of data the program prints the result to the standard output on a separate line as shown in the examples below. 

Sample Input

735 3  4 125  6 5  3 350
633 4  500 30  6 100  1 5  0 1
735 0
0 3  10 100  10 50  10 10

Sample Output

735
630
0
0

题目描述:给定一些c[i]和w[i]的钱,求这些钱能组成的1~m的所有可行的数量。

这道题想一想,如果直接做背包的话显然会超时。观察到每一个dp[i]这个状态不是能就是不能,所以就可以用一个bool的数组来存储,最后维护一个单调队列。

为了方便起见我们可以先将题目中输入的c[i]和w[i]分类,如果c[i]等于1的话那么就可以做一个简单的01背包。如果c[i]*w[i]>m那么就是一个完全背包最后将严格的分组包求出来,我们就可以用一个单调队列来维护了。

维护分成几个部分。如果他是从同一组的推到出来的(我们规定mod w[i]余数相同的分成一组),如果head-tail==c[i]那么就说明这个数不能从同一组的推出来那么我们就需要把它前面那个从队首踢出来,记一个sum表示当前这个组内能够推出来的个数。如果sum不等于0则一组都可以推出,就可以的到所有的dp[i]了,最后求和输出即可。

代码如下(请忽略蒟蒻用头-尾的惨痛教训)

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int maxn=155;

const int maxm=1e5+5;

int w[maxn],c[maxn];

int n,m;

bool dp[maxm],que[maxm];

int solve()
{
	int rt=0;
	memset(dp,0,sizeof(dp));
	memset(que,0,sizeof(que));
	dp[0]=1;
	for (int i=0;i<n;i++)
	{
		if (c[i]==1)
		{
			for (int j=m;j>=w[i];j--)
	         if (!dp[j]&&dp[j-w[i]])
	            dp[j]=1;
		}
	    else if (c[i]*w[i]>m)
	    {
	    	for (int j=w[i];j<=m;j++)
	          if (!dp[j]&&dp[j-w[i]])
	             dp[j]=1;
	    }  
	    else
	    {
	    	for (int d=0;d<w[i];d++)
	    	{
	    		int en=-1,st=0,sum=0;
	    		for (int j=d;j<=m;j+=w[i])
	    		{
	    			if (en-st==c[i])//惨痛教训 
	    			   sum-=que[st++];
					que[++en]=dp[j];
					sum+=dp[j];
			        if (!dp[j]&&sum)
				       dp[j]=1;   
	    	    }
	    	}
	    }
	}
	for (int i=1;i<=m;i++)
	   rt+=dp[i];
	return rt;  
}

int main()
{
	while (scanf("%d%d",&n,&m)&&n)
	{
		for (int i=0;i<n;i++)
		   scanf("%d",&w[i]);
		for (int i=0;i<n;i++)
		   scanf("%d",&c[i]);
		printf("%d\n",solve());
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/beloved_rancy/article/details/79507337