Margaritas on the 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
【题意】给出n件物品的体积以及背包的体积,求多少种背包再也放不下东西的方法。
【思路】现将物体的体积排序,定义一个数组第i件放不下,存储前i-1的体积和sum[i-1];用dp[i]表示体积为i的时候有dp[i]种再也放不下东西的方法,
则,假设第i件放不下,则前i-1件是能放下的,当v-sum[i-1]-vol[i]+1~v-sum[i-1](v-sum[i-1]-vol[i]+1可能会小于0,这时与0取大者),
如果从体积小的物品开始枚举,考虑当第i件物品不能放入背包的情况,此时,前i-1件物品就都已经被放到背包里面去了,
那么就需要计算第i+1 ~ n件物品占据体积tmp ~ V-sum[i-1]的方法数,然后再在总方法数上加上dp数组对应的值。
那么,第i件物品就被考虑了i-1次,此时的算法复杂度为O(N^2 * V)。
为了使得每件物品只被放入到背包一次,考虑从体积大的物品开始枚举。当第i件物品不能放入背包中,而前i件物品都放入了背包中,
这时,我们把已知的i+1 ~ N件物品占据体积k ~ V-sum[i-1]的方法数加到总的方法数ans上,然后再去取第i件物品做01背包,供考虑下一件物品不能放入背包的情况使用,直到枚举完全部的物品。
在逆序枚举的时候,当第i件物品放不下的时候,第i件物品后的物品都被考虑过了,且第i件物品之后的物品也肯定放不下。
而顺序枚举的话,第i件物品之前的物品都被考虑过作为当前放不下的最小物品了,第i件物品放不下不意味着前面被考虑过的i-1件物品放不下,
这就违背我们当初的假设了,如果这么做了,还有装进背包的物品的方法就也被考虑进去了。
参考:http://www.cnblogs.com/zhexipinnong/archive/2012/11/16/2772498.html
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=;
int dp[N],sum[N],vol[N];
int n,v;
int main()
{
int t,cas=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&v);
for(int i=;i<=n;i++)
{
scanf("%d",&vol[i]);
}
sort(vol+,vol++n);
memset(dp,,sizeof(dp));
memset(sum,,sizeof(sum));
dp[]=;
int ans=;
for(int i=;i<=n;i++)
{
sum[i]=sum[i-]+vol[i];
}
for(int i=n;i>=;i--)
{
int tmp=max(,v-sum[i-]-vol[i]+);
for(int j=v-sum[i-];j>=tmp;j--)
{
ans+=dp[j];
}
for(int j=v;j>=vol[i];j--)
{
dp[j]+=dp[j-vol[i]];
}
}
if(vol[]>v) ans=;
printf("%d %d\n",cas++,ans);
}
return ;
}
Margaritas on the River Walk_背包的更多相关文章
- POJ 3093 Margaritas on the River Walk(背包)
题意 n个有体积的物品,问选取一些物品,且不能再继续选有多少方法? n<=1000 题解 以前的考试题.当时是A了,但发现是数据水,POJ上WA了. 把体积从小到大排序枚举没选的物品中体积最小的 ...
- POJ 3093 Margaritas(Kind of wine) on the River Walk (背包方案统计)
题目 Description One of the more popular activities in San Antonio is to enjoy margaritas in the park ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- poj[3093]Margaritas On River Walk
Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- HDU 3535 AreYouBusy (混合背包)
题意:给你n组物品和自己有的价值s,每组有l个物品和有一种类型: 0:此组中最少选择一个 1:此组中最多选择一个 2:此组随便选 每种物品有两个值:是需要价值ci,可获得乐趣gi 问在满足条件的情况下 ...
- HDU2159 二维完全背包
FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- CF2.D 并查集+背包
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...
- UVALive 4870 Roller Coaster --01背包
题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F , D -= K 问在D小于等于一定限度的时 ...
随机推荐
- response.getWriter().write()与out.print()的区别
1.首先介绍write()和print()方法的区别: (1).write():仅支持输出字符类型数据,字符.字符数组.字符串等 (2).print():可以将各种类型(包括Object)的数据通 ...
- IDEA 创建Java Web项目
发现项目目录没有classes和lib目录,所以自己创建 点击OK,选中"Jar Directroy"-->点击"OK" 然后直接把jar复制到这个目录下 ...
- C#语言基础——7月21日
C#语言基础 一.语言基础 (一).函数的四要素: 名称,输入,输出,加工(二).主函数.输出语句.输入语句: Static void Main(string[] args)//下划 ...
- F-Dining Cows(POJ 3671)
Dining Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7584 Accepted: 3201 Descr ...
- c# xml的增删改查操作 xmlDocument 的用法
1.将xml转换为DataTable string path = "";//xml的位置StringReader sr = null;XmlTextReader xmlReader ...
- oracle优化原则(二)
SQL优化原则 二.SQL语句编写注意问题 www.2cto.com 下面就某些SQL语句的where子句编写中需要注意的问题作详细介绍.在这些where子句中,即使某些列存在索引,但是由于编写了劣质 ...
- Greenplum——升级的分布式PostgresSQL
Greenplum数据库基于PostgreSQL开源技术.本质上讲,它是多个PostgreSQL实例一起充当一个数据库管理系统.Greenplum以PostgreSQL 8.2.15为基础构建,在SQ ...
- 如何在win7下配置IIS?
- VSS Plugin配置FAQ(翻译)[转]
前言(译者) 就个人的成长历程来说,刚参加工作用的是 CVS ,前前后后有接近三年的使用体验,从今年开始使用 SVN .总的来说我更喜欢 SVN ,用起来的确很方便,例如在本地源代码文件中加一个空格然 ...
- Floyd 算法的动态规划本质
[转载自:http://www.cnblogs.com/chenying99/p/3932877.html] Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(A ...