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
Sample Output
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=,c=getchar(),f=;
for(;c<||c>;c=getchar())
if(!(c^))
f=-;
for(;c>&&c<;c=getchar())
x=(x<<)+(x<<)+c-;
return x*f;
}
int T,n,m,ans,pre,a[],f[];
int main(){
T=read();
for(int t=;t<=T;t++){
ans=pre=;
n=read();
m=read();
for(int i=;i<=n;i++)
pre+=(a[i]=read());
sort(a+,a++n);
if(a[]>m){
printf("%d 0\n",t);
continue;
}
memset(f,,sizeof(f));
f[]=;
for(int i=n;i;i--){
pre-=a[i];
for(int j=max(,m-pre-a[i]+);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 ;
}
poj[3093]Margaritas On River Walk的更多相关文章
- 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 ...
- POJ 3093 Margaritas on the River Walk(背包)
题意 n个有体积的物品,问选取一些物品,且不能再继续选有多少方法? n<=1000 题解 以前的考试题.当时是A了,但发现是数据水,POJ上WA了. 把体积从小到大排序枚举没选的物品中体积最小的 ...
- Margaritas on the River Walk_背包
Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- POJ 1700 Crossing River (贪心)
Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...
- POJ 3258 River Hopscotch(二分法搜索)
Description Every year the cows hold an event featuring a peculiar version of hopscotch that involve ...
- poj 1700 Crossing River 过河问题。贪心
Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9887 Accepted: 3737 De ...
- 二分搜索 POJ 3258 River Hopscotch
题目传送门 /* 二分:搜索距离,判断时距离小于d的石头拿掉 */ #include <cstdio> #include <algorithm> #include <cs ...
- E - River Hopscotch POJ - 3258(二分)
E - River Hopscotch POJ - 3258 Every year the cows hold an event featuring a peculiar version of hop ...
随机推荐
- yii redies 不同的工程缓存key的问题
参考这篇文章 基本配置操作: yii main.php中: return array( ... 'components'=>array( 'redis_cache' => array ( ...
- Android 友盟分享躺过的几个坑,大坑,坑爹啊
前言:公司上线了一个项目,做Android 客户端集成友盟分享,在做得过程中,遇到了下面3个问题,希望其他小伙伴注意. ) 错误1: 1.错误描述 我是照着友盟的集成文档一步一步的集成的,做完后,发现 ...
- Java中"\t"表示几个空格
\t是补全当前字符串长度到8的整数倍,最少1个最多8个空格补多少要看你\t前字符串长度 测试程序: 测试结果: 总结:运行到“\t”时,判断当前字符串长度,将当前字符串长度补到8的倍数(不包括0).
- UIWindows 使用注意
只有Window才有自发权利显示,其他View都需要由Window负责显示. 注:* 不能将控制器直接添加到Window上,会出现野指针错误. * 不建议直接使用window开发(比 ...
- Android Studio教程--Android Studio 2.1安装与配置
1.下载Android Studio 去官网https://developer.android.com/studio/index.html下载最新版的Android Studio2.1(自备梯子) 或 ...
- 优化MySchool数据库(三)
使用T_SQL 编写业务逻辑: 如何定义及使用“变量”: ---- 让电脑帮我记住一个名字(王二) C#: string name ; [定义一个变量] name = "王二&qu ...
- XML解析之DOM详解及与SAX解析方法的比较
XML解析(DOM) XML文件解析方法介绍 我们所用到的NSXMLParser是采用SAX方法解析 SAX(Simple API for XML) 只能读,不能修改,只能顺序访问,适合解析大型XML ...
- 关于Storyboard的使用
前言:说起来码龄很久似的,但是还是有很多基础的知识都不知道,比如下面介绍的关于Stroyboard的使用.(本篇博文随笔会不断补充关于Storyboard的使用技巧,持续更新) 目录: 1.使用Str ...
- SVN导出/导入、SVN备份/还原 【小白版】
一.导出: 1.进入svn安装路径bin文件夹下,使用 cd 命令. 在windows下,win+R 键入 cmd 回车 打开命令窗口cmd,进入下列目录(svn服务器安装目录bin): " ...
- Apache 反向代理实现为http添加https的外衣
Apache 反向代理 金天:坚持写东西,不是一件容易的事,换句话说其实坚持本身都不是一件容易的事.如果学习有捷径,那就是不断实践,不断积累.写笔记,其实是给自己看的,是体现积累的一种方式,要坚持. ...