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 along the river know as the River Walk. Margaritas may be purchased at many establishments along the River Walk from fancy hotels toJoe’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.
思路
1. 朴素背包方案统计的状态转移方程为 dp[i] += dp[i-w[i]]
2. 题目要求背包剩下的空间无法再放下任意一个还未被选择的物品, 那么需要特殊考虑
- 先对所有物品按照其价值进行排序, 对于每件物品都有拿或者不拿两种选择
- 对于第 k 件物品, 分拿或不拿两种选择. 假设第 k 件物品是未拿到的价值最小的, 那么 0~k-1 这 k 件物品一定都拿了. 然后对 K+1 ~ END 执行朴素背包方案统计即可
3. 下面的代码解析.
- delta 是指 0 ~ K-1 这 K 件物品的价值总和, 每次循环加上 dp[i], 因此名为 delta
- 初始化 dp[delta] = 0, dp[else] = 0
代码:
#include <iostream>
#include <algorithm>
using namespace std; const int MAXN = 1010;
int testCase, V, D;
int w[MAXN];
int dp[MAXN];
int solve_dp() {
int solution = 0;
int delta = 0;
for(int i = 0; i < V; i ++) { memset(dp, 0, sizeof(dp));
dp[delta] = 1;
for(int j = i+1; j < V; j++) {
for(int k = D; k >= delta+w[j]; k--) {
dp[k] = dp[k]+dp[k-w[j]];
}
} for(int k = D; k > max(0, D-w[i]); k --) { // WA 过一次, 写成 >=, 等于的话就可以装下 i 了
if(k >= delta) {
solution += dp[k];
}
}
delta += w[i];
}
return solution;
} int main() {
freopen("E:\\Copy\\ACM\\测试用例\\in.txt", "r", stdin); cin >> testCase;
int index = 0;
while(testCase--) {
index++;
cin >> V >> D;
for(int i = 0; i < V; i ++) {
scanf("%d", &w[i]);
}
sort(w, w+V);//WA 过, 忘了排序
// mainFunc
printf("%d %d\n", index, solve_dp());
}
return 0;
}
POJ 3093 Margaritas(Kind of wine) on the River Walk (背包方案统计)的更多相关文章
- poj[3093]Margaritas On River Walk
Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...
- POJ 3093 Margaritas on the River Walk(背包)
题意 n个有体积的物品,问选取一些物品,且不能再继续选有多少方法? n<=1000 题解 以前的考试题.当时是A了,但发现是数据水,POJ上WA了. 把体积从小到大排序枚举没选的物品中体积最小的 ...
- POJ 1426 Find The Multiple(背包方案统计)
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...
- Margaritas on the River Walk_背包
Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...
- POJ 2151 Check the difficulty of problems 概率dp+01背包
题目链接: http://poj.org/problem?id=2151 Check the difficulty of problems Time Limit: 2000MSMemory Limit ...
- POJ 3260 The Fewest Coins 最少硬币个数(完全背包+多重背包,混合型)
题意:FJ身上有各种硬币,但是要买m元的东西,想用最少的硬币个数去买,且找回的硬币数量也是最少(老板会按照最少的量自动找钱),即掏出的硬币和收到的硬币个数最少. 思路:老板会自动找钱,且按最少的找,硬 ...
- POJ Charlie's Change 查理之转换(多重背包,变形)
题意: 给定身上的4种硬币,分别是1 ,5 ,10, 25面额各有多张,要求组成面额p的硬币尽可能多.输出组成p的4种硬币各自的数量. 思路: 多重背包,300+ms.用01背包+二进制的方法.记录下 ...
- poj 1015 Jury Compromise(背包+方案输出)
\(Jury Compromise\) \(solution:\) 这道题很有意思,它的状态设得很...奇怪.但是它的数据范围实在是太暴露了.虽然当时还是想了好久好久,出题人设了几个限制(首先要两个的 ...
- poj 1726
http://poj.org/problem?id=1276 解题要点:用完全背包来模拟的解题,只不过加了限制条件used[]...其他的就一样了.. 注意: cash 和n 为0 的情况 #incl ...
随机推荐
- RTX——第9章 任务运行在特权级或非特权级模式
以下内容转载自安富莱电子: http://forum.armfly.com/forum.php 本章教程为大家讲解 RTX 运行模式的一个重要知识点,特权级模式和非特权级模式,有些资料或者书籍将非特权 ...
- ucos-III前言
年 uC/OS-III实时操作系统面世以来,uC/OS-III的性能和稳定性已经日趋完善.uC/OS-III对前面版本取精弃粕,推陈出新,功能和性能比起 uC/OS-II已经上了很大一步台阶.uC/O ...
- dp:FZU2030括号问题
http://acm.fzu.edu.cn/problem.php?pid=2030 给出一个字符串,其中包括3种字符: ‘(‘, ‘)’, ‘?’.其中?表示这个字符可以是’(‘也可以是’)’. 现 ...
- tomcat架构分析 (connector NIO 实现)
出处:http://gearever.iteye.com 上一篇简单记录了缺省配置的connector的内部构造及消息流,同时此connector也是基于BIO的实现.除了BIO外,也可以通过配置快速 ...
- rhel7的xfs与Oracle database
在rhel7下,安装oracle database时,能够选择xfs文件,例如以下: Requirements for Installing Oracle Database 12.1on RHEL7 ...
- hdu4366 Successor
好题. 可是感觉题目描写叙述不是非常清楚 这题仅仅是询问开除某人后,他的下属中谁会替代他的位置.不会更新这个位置 要求一个子树中忠诚度最高的人. 能够想到dfs树.保留时间戳.每一个节点便表示一个 ...
- Hbase 学习(二)各种filter
各种filter 今天的主题是Filter,hbase客户端查询的时候,自定义查询filter. 直接上例子吧,不多说别的了,第一个例子是RowFilter的. Scan scan = new Sca ...
- Eclipse 中link一个异地的Folder
Eclipse 中link一个外地的Folder New -> Folder -> Click "Advanced" --> Check "Link t ...
- Session 的生命周期
#region EntityMap /// <summary> /// 实体类值的访问器,线程安全 /// </summary> // ...
- Droptiles - 炫酷的 Metro 风格的层叠式 Web 面板
介绍 Droptiles是一套Metro风格的类似Win8的Web2.0控制面板.它采用图块(tiles)建立用户体验.图块(tiles)是一些可以从外部资源中获取数据的迷你应用.点击图块(tile) ...