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

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.

题解

题目大意:给定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=0,c=getchar(),f=1;
    for(;c<48||c>57;c=getchar())
        if(!(c^45))
            f=-1;
    for(;c>47&&c<58;c=getchar())
        x=(x<<1)+(x<<3)+c-48;
    return x*f;
}
int T,n,m,ans,pre,a[1001],f[1001];
int main(){
    T=read();
    for(int t=1;t<=T;t++){
        ans=pre=0;
        n=read();
        m=read();
        for(int i=1;i<=n;i++)
            pre+=(a[i]=read());
        sort(a+1,a+1+n);
        if(a[1]>m){
            printf("%d 0\n",t);
            continue;
        }
        memset(f,0,sizeof(f));
        f[0]=1;
        for(int i=n;i;i--){
            pre-=a[i];
            for(int j=max(0,m-pre-a[i]+1);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 0;
}
时间: 2024-12-06 15:09:36

poj[3093]Margaritas On River Walk的相关文章

POJ 3093 Margaritas on the River Walk (0-1背包变形)

这题目的思路很巧妙,什么情况下剩下的所有物品都放不下呢?就是当前剩余物品中最小的那个也放不下.所以,先把物品按照容量从小到大排序,依次枚举当前背包为放不下的最小物品的情况. 对于当前物品i,必有1到i-1的所有物品都放进去,这时候比i大的物品谁放谁不放是不确定的.转换成0-1背包问题:把前i-1个物品都放进去以后,得到空间为tsum - sum[i-1](前缀和)的包,只要从第i+1到第n个物品中拿出一个方案填充这个包使得剩余体积小于第i个物品的体积就可以了,把总方案数累加就是结果! 注意特殊情

Margaritas on the River Walk

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.

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

背包问题总结篇

1.混合了贪心思想的背包入门 nefu1028暑假计划 01背包 给定工作开始时间.完成时间.给的工资,工作不能重叠,求最大收益. 一维Dp表示截止到当前时间的最大收益,但是事先要对结构体按结束时间排序,防止前一状态没有值 #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; int dp[1005]; struct mon

【与奥斯丁的二十种邂逅】-- 得克萨斯首府的小情调城市游 (介绍你在奥斯丁必须做的,推荐做的,和做了你就牛掰了的20件事)

明年1月底应该会去奥斯丁参加AAAI会议,偶尔看看攻略.嗨森~ 原文转自: http://blog.renren.com/share/28765132/15631046000 作者 :  张诺娅 (图片来自 @走遍美国(601684562) ) 开篇要感谢一下 @走遍美国(601684562) 小组,小站,和公共主页的老大,美国旅游达人 @朱周明相(245804183) 船长童鞋在奥斯丁的盛情款待.初到奥斯丁的时候,船长语重心长地说:我接待过这么多的人来玩奥斯丁,为神马就没有人写一篇关于奥斯丁的

POJ 3258 River Hopscotch 经典二分

点击打开链接 River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6189   Accepted: 2683 Description Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a r

POJ 3258 River Hopscotch 二分答案

River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6193   Accepted: 2685 Description Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. T

二分搜索 POJ 3258 River Hopscotch

题目传送门 1 /* 2 二分:搜索距离,判断时距离小于d的石头拿掉 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAXN = 5e4 + 10; 12 const int INF = 0x3f3f3f3