poj[3093]Margaritas On River Walk
Posted AronQi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj[3093]Margaritas On River Walk相关的知识,希望对你有一定的参考价值。
Description
One of the more popular activities in San Antonio is to enjoy margaritas in the park along the river know as the River Walk. Margaritas may be purchased at many establishments along the River Walk from fancy hotels to Joe’s Taco and Margarita stand. (The problem is not to find out how Joe got a liquor license. That involves Texas politics and thus is much too difficult for an ACM contest problem.) The prices of the margaritas vary depending on the amount and quality of the ingredients and the ambience of the establishment. You have allocated a certain amount of money to sampling different margaritas.
Given the price of a single margarita (including applicable taxes and gratuities) at each of the various establishments and the amount allocated to sampling the margaritas, find out how many different maximal combinations, choosing at most one margarita from each establishment, you can purchase. A valid combination must have a total price no more than the allocated amount and the unused amount (allocated amount – total price) must be less than the price of any establishment that was not selected. (Otherwise you could add that establishment to the combination.)
For example, suppose you have $25 to spend and the prices (whole dollar amounts) are:
Vendor A B C D H J Price 8 9 8 7 16 5
Then possible combinations (with their prices) are:
ABC(25), ABD(24), ABJ(22), ACD(23), ACJ(21), ADJ( 20), AH(24), BCD(24), BCJ(22), BDJ(21), BH(25), CDJ(20), CH(24), DH(23) and HJ(21).
Thus the total number of combinations is 15.
Input
The input begins with a line containing an integer value specifying the number of datasets that follow, N (1 ≤ N ≤ 1000). Each dataset starts with a line containing two integer values V and D representing the number of vendors (1 ≤ V ≤ 30) and the dollar amount to spend (1 ≤ D ≤ 1000) respectively. The two values will be separated by one or more spaces. The remainder of each dataset consists of one or more lines, each containing one or more integer values representing the cost of a margarita for each vendor. There will be a total of V cost values specified. The cost of a margarita is always at least one (1). Input values will be chosen so the result will fit in a 32 bit unsigned integer.
Output
For each problem instance, the output will be a single line containing the dataset number, followed by a single space and then the number of combinations for that problem instance.
Sample Input
2 6 25 8 9 8 7 16 5 30 250 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Sample Output
1 15 2 16509438
Hint
Note: Some solution methods for this problem may be exponential in the number of vendors. For these methods, the time limit may be exceeded on problem instances with a large number of vendors such as the second example below.
题解
题目大意:给定n个物品和背包容量,求将背包填满的方案数。
算法1:是这样一个思路,考虑第i个物品为剩下物品中体积最小的,那么在它之前的物品必须全数放入(无后效性),之后比它大的就用01背包方案数算法求解方案数即可。时间复杂度(T*n*n*m)
算法2:考虑到算法1的时间复杂度在n较大时会超时,需要进行优化。观察之后会发现,每次的01背包方案数统计会有极多的重复,为了将重复处利用起来,并减少无用的计算,我们反过来做背包。从最大的物品做起,那么对于第i个物品来说,视作第i-1个物品是剩下物品中体积最小的,那么,对它进行永久性的01背包统计就不会影响到i+1到n的物品方案数了,至于方案数的统计,在第i个物品时统计的就是第i个物品本身当做不放入物品时的方案数了。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; inline int read(){ int x=0,c=getchar(),f=1; for(;c<48||c>57;c=getchar()) if(!(c^45)) f=-1; for(;c>47&&c<58;c=getchar()) x=(x<<1)+(x<<3)+c-48; return x*f; } int T,n,m,ans,pre,a[1001],f[1001]; int main(){ T=read(); for(int t=1;t<=T;t++){ ans=pre=0; n=read(); m=read(); for(int i=1;i<=n;i++) pre+=(a[i]=read()); sort(a+1,a+1+n); if(a[1]>m){ printf("%d 0\n",t); continue; } memset(f,0,sizeof(f)); f[0]=1; for(int i=n;i;i--){ pre-=a[i]; for(int j=max(0,m-pre-a[i]+1);j<=m-pre;j++) ans+=f[j]; for(int j=m;j>=a[i];j--) f[j]+=f[j-a[i]]; } printf("%d %d\n",t,ans); } return 0; }
以上是关于poj[3093]Margaritas On River Walk的主要内容,如果未能解决你的问题,请参考以下文章