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小于等于一定限度的时 ...
随机推荐
- Merge Sorted Array [LeetCode]
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- iOS 推送全解析
本文旨在对 iOS 推送(以下简称 推送)进行一个完整的剖析,如果你之前对推送一无所知,那么在你认真地阅读了全文后必将变成一个推送老手,你将会对其中的各种细节和原理有充分的理解.以下是 pikacod ...
- PHP 命名空间加载的理解
关于spl_autoload_register()和__autoload(),相信大多数都会选择前者了? 看两者的用法: //__autoload用法function __autoload($clas ...
- 执行MAVEN更新包
我们一般使用 mvn eclipse:eclipse 执行对maven库的引用,这样会修改项目下的classpath文件. 我们修改直接在eclipse 使用maven库作为项目的引用. 步骤如下: ...
- shell脚本初析
简单理解:运用很多工具,将复杂的步骤简单化,体现在shell脚本中框架:必须有备注,写的别人能够看得懂开头:#! /bin/bash 代表的意思是改文件使用的是bash语法要想使用该种方法运行shel ...
- sqlserver 2008 存储过程调用存储过程或方法
函数:拆分字符串,并返回一个table CREATE FUNCTION [dbo].[f_splitSTR](@s varchar(max), --待分拆的字符串@split varchar(10) ...
- AjaxUpload.3.5.js之ASP.NET 文件上传
一.引入js文件 <script type="text/javascript" src="/Scripts/JQuery.min.js"></ ...
- qml android 的一个例子qtHangMan
这个例子有2个好处: 1.解决了黑屏问题 2.演示了应用内购买的问题
- g++默认支持c++11标准的办法
//第一种,直接包含在源程序文件中,如第一行代码所示 #pragma GCC diagnostic error "-std=c++11" #include <iostream ...
- 矩阵(matrix)
我们定义一个矩阵的权值为这个矩阵四个角上的数值的最小值.现在小M有一个矩阵,他想在这个矩阵中寻找到一个权值最大的子矩阵,请你告诉他这个最大权值.(距形规模最大为2000*2000) 比赛 看到第二题那 ...