zoukankan      html  css  js  c++  java
  • Inviting Friends(hdu3244 && zoj3187)完全背包+二分

    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种原料,每种原料有6个参数:x,y,s1,p1,s2,p2。表示的含义分别是:对于第i种原料,每个人的需求量是x,现在还剩下y的量,每种原料有2种包装,一种小包的,一种打包的,每一小包的量是s1,价格是p1,打包的量是s2,价格是p2。现在给你n种原料和m的钱,求最多能请几个人。

    解题思路:二分法枚举人数,然后再根据人数判断能不能满足那么确定人数之后,就要根据人数求出每种原料的最少花钱,看会不会超支对于没一个原料都求一次完全背包,背包容量就是你需要的数量加上大包的容量,然后在need到上限间找出最小值就OK


    完全背包+二分:

    我正在学习。。。。

    题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3187


    http://acm.hdu.edu.cn/showproblem.php?pid=3244

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    
    int n,m;
    const int maxn = 110;
    const int inf = 0x3f3f3f3f;
    
    struct node
    {
        int s1,s2;
        int p1,p2;
        int x,y;
    
    }t[maxn];
    
    
    int cal_r()
    {
        int ret=inf;
        for(int i=1;i<=n;i++)
        {
            if(t[i].s1*1.0/t[i].p1 >= t[i].s2*1.0/t[i].p2)
            {
                int tmp = (m/t[i].p1*t[i].s1)+t[i].y;//都买一个看有多少
                int p = tmp/t[i].x;//人数;
                ret=min(ret,p);//找最小的最大人数
            }
            else
            {
                int tmp = (m/t[i].p2*t[i].s2)+t[i].y;
                int p = tmp/t[i].x;
                ret=min(ret,p);
            }
        }
        return ret;//返回最小的最大人数;
    }
    
    int dp[6000000+20];
    
    int cal_need(int i,int need)
    {
        int w[2],c[2];
        c[0]=t[i].p1;
        w[0]=t[i].s1;
        c[1]=t[i].p2;
        w[1]=t[i].s2;
    
        int V=need+t[i].s2;
    
        for(int i=1;i<=V;i++)
            dp[i]=inf;
    
        dp[0]=0;
    
        for(int i=0;i<2;i++)//完全背包
        {
            for(int v=w[i];v<=V;v++)
                dp[v]=min(dp[v],dp[v-w[i]]+c[i]);
        }
    
        int ret = inf;
        for(int i=need;i<=V;i++)
            ret = min(ret,dp[i]);
    
        return ret;
    }
    
    
    bool judge(int k)//判断人数是否符合购买力。。。
    {
        int s=0;
        for(int i=1;i<=n;i++)
        {
            int need = t[i].x*k-t[i].y;
            if(need<=0)
                continue;
            int tmp = cal_need(i,need);//需要多少钱;
            s+=tmp;
            if(s>m)
                return false;
        }
        return true;
    }
    
    
    
    int main()
    {
        while(~scanf("%d%d",&n,&m) && n+m)
        {
            for(int i=1;i<=n;i++)
                scanf("%d%d%d%d%d%d",&t[i].x,&t[i].y,&t[i].s1,&t[i].p1,&t[i].s2,&t[i].p2);
            int r=cal_r();
            int l=0;
            int ans = 0;
            //printf("r %d
    ",r);
            while(l<=r)
            {
                int mid=(l+r)>>1;
                if(judge(mid))
                {
                    ans=mid;
                    l=mid+1;
                }
                else
                    r=mid-1;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    
    
    
    背包!加油加油!!!!!
  • 相关阅读:
    pyftpdlib 搭建FTP服务器
    numpy 解一道简单数学题
    python 实现词云
    个人的毕业长足---- 暴走北京
    Tensorflow of GPU, hello fish by version 0.8.
    图像识别
    用一个Inception v3 架构模型实现简单的迁移学习(译:../tensorflow/tensorflow/examples/image_retraining/retrain.py)
    19.液晶屏的原理
    18.DMA-6410
    17.DMA-2440
  • 原文地址:https://www.cnblogs.com/yuyixingkong/p/3946418.html
Copyright © 2011-2022 走看看