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的更多相关文章

  1. 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 ...

  2. POJ 3093 Margaritas on the River Walk(背包)

    题意 n个有体积的物品,问选取一些物品,且不能再继续选有多少方法? n<=1000 题解 以前的考试题.当时是A了,但发现是数据水,POJ上WA了. 把体积从小到大排序枚举没选的物品中体积最小的 ...

  3. Margaritas on the River Walk_背包

    Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...

  4. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  5. POJ 1700 Crossing River (贪心)

    Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...

  6. POJ 3258 River Hopscotch(二分法搜索)

    Description Every year the cows hold an event featuring a peculiar version of hopscotch that involve ...

  7. poj 1700 Crossing River 过河问题。贪心

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9887   Accepted: 3737 De ...

  8. 二分搜索 POJ 3258 River Hopscotch

    题目传送门 /* 二分:搜索距离,判断时距离小于d的石头拿掉 */ #include <cstdio> #include <algorithm> #include <cs ...

  9. E - River Hopscotch POJ - 3258(二分)

    E - River Hopscotch POJ - 3258 Every year the cows hold an event featuring a peculiar version of hop ...

随机推荐

  1. SharePoint Online 创建门户网站系列之定制栏目

    前 言 SharePoint Online自带的库就带有二级页面和详细页面,也就是Allitems页面和DispForm页面,但是实在不够美观,尤其对于门户网站这一企业门面来说,更是无法接受. 下面, ...

  2. 【C语言】C语言数据类型

    目录: [数据类型图] [基本数据类型]   · 整型   · 实型   · 字符型   · 布尔类型 1.数据类型图 2.基本数据类型 · 整型 用于准确表示整数,根据表示范围的不同分为三种:短整型 ...

  3. 使用 PHPMailer 发送邮件

    转载 http://blog.csdn.net/liruxing1715/article/details/7914974 PHPMailer 的官方网站:http://phpmailer.worxwa ...

  4. MAC OS 系统使用心得

    1.Windows快捷键在 mac os 里怎么调用 今天用teamviewer链接我在公司的电脑.我想调试程序,我程序默认F5是启动调试,但在mac os里,F5是调节屏幕亮度的. 这时候遇到快捷键 ...

  5. Sumlime Text编辑文件后快速刷新浏览器

    作为Web开发人员,我们经常会这么做:在编辑器中调整代码,保存文件,切换到浏览器,然后刷新浏览器页面来查看结果.在代码编辑过程中,我们需要重复进行很多次这些操作. 如果你使用的是Sublime Tex ...

  6. 我参加了51CTO博客大赛,求投票!

    我是张传波,也是Fireball(火球). 我参加了51CTO博客大赛,距离网络投票截止没有几天了,求投票!我的参赛链接:http://blog.51cto.com/contest2013/82313 ...

  7. [MySQL Reference Manual] 4 MYSQL Program

    4 MYSQL Program 目录 4 MYSQL Program 4.3 MySQL Server和Server启动程序 4.3.1 mysqld 4.3.2 mysqld_safe 4.3.3 ...

  8. 十五天精通WCF——第十一天 如何对wcf进行全程监控

    说点题外话,我们在玩asp.net的时候,都知道有一个叼毛玩意叫做“生命周期”,我们可以用httpmodule在先于页面的page_load中 做一些拦截,这样做的好处有很多,比如记录日志,参数过滤, ...

  9. spring 整合 ActiveMQ

    1.1     JMS简介 JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到 ...

  10. Ngigx+Tomcat配置动静分离,负载均衡

    由于公司使用过Ngnix,对于刚接触Nginx来说,感觉有些好奇,于是研究了下. 本人在windows下使用的版本是nginx-1.8.1: 1. 启动Ngnix 双击nginx-1.8.1文件夹中n ...