ZOJ 3187 Inviting Friends (二分+完全背包)

Inviting Friends


Time Limit: 1 Second      Memory Limit: 32768 KB


You want to hold a birthday party, inviting as many friends as possible, but you have to prepare enough food for them. For each person, you need n kinds of ingredient to make good food. You can use the ingredients in your kitchen, or buy some new ingredient packages. There are exactly two kinds of packages for each kind of ingredient: small and large.

We use 6 integers to describe each ingredient: ,x, y, s1, p1, s2, p2, where x is the amount (of this ingredient) needed for one person, y is the amount currently available in the kitchen, s1 and p1 are the size (the amount of this ingredient in each package) and price of small packages, s2 and p2 are the size and price of large packages.

Given the amount of money you can spend, your task is to find the largest number of person who can serve. Note that you cannot buy only part of a package.

Input

There are at most 10 test cases. Each case begins with two integers n and m (1 <= n <= 100, 1 <= m <= 100000), the number of kinds of ingredient, and the amount of money you have. Each of the following n lines contains 6 positive integers x, y, s1, p1, s2, p2 to describe one kind of ingredient (10 <= x <= 100, 1 <= y <= 100, 1 <= s1 <= 100, 10 <= p1 <= 100, s1 < s2 <= 100, p1 < p2 <= 100). The input ends with n = m = 0.

Output

For each test case, print the maximal number of people you can serve.

Sample Input

2 100
10 8 10 10 13 11
12 20 6 10 17 24
3 65
10 5 7 10 13 14
10 5 8 11 14 15
10 5 9 12 15 16
0 0

Sample Output

5
2

题意:有n(n<=100)种物品,小明有m(m<=100000)元钱,每种物品有六个属性:x(每个人需要这种物品x件),res(仓库现在剩余res件),xs(一小包该物品所含的件数),xv(一小包该物品的价格),ds(一大包该物品所含的件数),dv(一大包该物品的价格)

问你小明的m元钱能让最多多少人每种物品都分到足够件数。

思路:

显然我们可以二分答案,然后对每个答案判断钱是否能够每件物品都买到足够件数。

我先是按大包小包的性价比贪心买,然后果断WA

对于每一种物品,它的大包和小包又是相当于两种物品,有无穷多包可以买,求买够x件的最小价格。这不是完全背包吗?

容量设置为x到x+(一包大小包最大的包含的件数)-1,这样在完全背包时我们就可以去背包容量在x到x+(一包大小包最大的包含的件数)-1的最小价格了。

注意二分的上界,处理不好会使中间结果爆longlong。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3fLL
using namespace std;
const int maxn=100010;
ll n,m,k,q;
ll ans;
ll dp[1200000];
struct node
{
    ll x,res,xs,xv,ds,dv;
}a[maxn];
ll cal(int id,ll sum)
{
    //cout<<sum<<endl;
    ll aa=min(a[id].xs,a[id].ds);
    ll bb=max(a[id].xs,a[id].ds);
    if(sum<aa) return min(a[id].xv,a[id].dv);
    sum+=bb-1;
    for(int i=0;i<=sum;i++)
    dp[i]=inf;
    dp[0]=0;
    for(int i=1;i<=sum;i++)
    if(i>=a[id].xs&&((i-a[id].xs==0)||dp[i-a[id].xs])){
        dp[i]=min(dp[i],dp[i-a[id].xs]+a[id].xv);
    }
    for(int i=1;i<=sum;i++)
    if(i>=a[id].ds&&((i-a[id].ds==0)||dp[i-a[id].ds])){
        dp[i]=min(dp[i],dp[i-a[id].ds]+a[id].dv);
    }
    for(int i=sum-(bb-1);i<sum;i++)
    {
        dp[sum]=min(dp[i],dp[sum]);
    }
    return dp[sum];
}
ll jud(ll mid)
{
    ll sum=m;
    for(int i=0;i<n;i++)
    {
        ll tmp=mid*a[i].x-a[i].res;
        ll aa=cal(i,tmp);
        if(aa>sum) return 0;
        sum-=aa;
    }
    return 1;
}
int main()
{
    while(scanf("%lld %lld",&n,&m)!=EOF)
    {
        if(!n&&!m) break;
        ll l=0,r=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lld%lld%lld%lld%lld%lld",&a[i].x,&a[i].res,&a[i].xs,&a[i].xv,&a[i].ds,&a[i].dv);
            ll tmp=max(((m/a[i].dv+1)*a[i].ds+a[i].res)/a[i].x,((m/a[i].xv+1)*a[i].xs+a[i].res)/a[i].x);
            r=max(r,tmp+1);
        }
        ans=0;
        while(l<r){
             ll mid=(l+r)>>1;
             if(jud(mid)) {ans=mid;l=mid+1;}
             else r=mid;
        }
        if(jud(r)) ans=r;
        printf("%lld\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值