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. iOS/Android网络消息推送的实现两种方法

    移动时代,用户为王,而每个APP拥有的活跃用户量(Active Users),决定了其价值. 消息推送成为了不可或缺的活跃唤起工具. 目前消息推送有如下两种途径: 1.iOS传统方式: 通过Apple ...

  2. Eclipse的自动排版设置(format)

    Java排版:         主要是在文件保存时自动触发排版等规则,省掉反复操作快捷键 Ctrl+Shift+F 的步骤.在 eclipse 中选择 Window-> Preferences- ...

  3. Android Sqlite基本命令

    要查看数据库,首先必须要找到db文件,如果拷贝到电脑上,查看的方法比较多,在手机上,用命令查看比较直接和方便. 首先要找到数据库的位置,一般数据库时存放在程序的私有目录,所以要获取root权限. 确保 ...

  4. 【代码笔记】iOS-16进制颜色与UIColor互转

    一,效果图 二,工程目录. 三,代码 RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additio ...

  5. 【读书笔记】iOS-反溃网络信息改善用户体验

    一,iOS6表视图刷新控件的使用. 二,使用等待指示器控件. 三,使用网络等待指示器. 四,使用MBProgressHUD等待指示器. 参考资料:<iOS网络编程与云端应用-最佳实践>

  6. 【Android】Mac安装EasyTether导致无法识别设备的问题

    正文 想让手机走PC网络,然后抓包,于是搜索一番后安装了一个叫EasyTether的软件.还没来得及测试,就忙着写代码去了,重启MAC以后就发现连不上手机了,一开始并没有怀疑是 EasyTether的 ...

  7. JQuery+ajax+jsonp 跨域访问

    Jsonp(JSON with Padding)是资料格式 json 的一种“使用模式”,可以让网页从别的网域获取资料. 关于Jsonp更详细的资料请参考http://baike.baidu.com/ ...

  8. 最新Sublime Text 2 激活 汉化

    0x00 Sublime Text 2 Sublime Text 2 是一个轻量.简洁.高效.跨平台的编辑器,一直在使用它,简直是coder的必备神器,自从使用它之后就深深爱上它了(/▽\=),可能因 ...

  9. [CMD]oracle数据库的导出导入

    除了推荐使用PL/SQL Developer 工具对oracle进行导出导入(http://www.cnblogs.com/whylaughing/p/5983490.html )之外,比较常用的还有 ...

  10. 搬家至独立博客 http://blog.imzjy.com

    欢迎访问 http://blog.imzjy.com