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 = ;
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=;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[+]; int cal_need(int i,int need)
{
int w[],c[];
c[]=t[i].p1;
w[]=t[i].s1;
c[]=t[i].p2;
w[]=t[i].s2; int V=need+t[i].s2; for(int i=;i<=V;i++)
dp[i]=inf; dp[]=; for(int i=;i<;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=;
for(int i=;i<=n;i++)
{
int need = t[i].x*k-t[i].y;
if(need<=)
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=;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=;
int ans = ;
//printf("r %d\n",r);
while(l<=r)
{
int mid=(l+r)>>;
if(judge(mid))
{
ans=mid;
l=mid+;
}
else
r=mid-;
}
printf("%d\n",ans);
}
return ;
}

背包!加油加油!!!!!

Inviting Friends(hdu3244 && zoj3187)完全背包+二分的更多相关文章

  1. POJ3111 K Best(另类背包+二分+变态精度)

    POJ3111 K Best,看讨论区说数据有点变态,精度要求较高,我就直接把循环写成了100次,6100ms过,(试了一下30,40都会wa,50是4000ms) 第一次在POJ上看到下面这种东西还 ...

  2. caioj 1086 动态规划入门(非常规DP10:进攻策略)

    一开始看到题目感觉很难 然后看到题解感觉这题贼简单,我好像想复杂了 就算出每一行最少的资源(完全背包+二分)然后就枚举就好了. #include<cstdio> #include<a ...

  3. Inviting Friends(二分+背包)

    Inviting Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...

  4. U - Inviting Friends HDU - 3244(二分答案 + 完全背包)

    U - Inviting Friends HDU - 3244 You want to hold a birthday party, inviting as many friends as possi ...

  5. P2370 yyy2015c01的U盘(二分+背包)

    思路:先说一下题意吧.就是给你n个文件大小为v,价值为c, 但是硬盘的大小为S, 而且要存的总价值大于等于p.问每次传输k大小的文件.问k的最大值是多少? 我们以k为二分对象. 直接讲检验函数吧. 假 ...

  6. 分数规划模板(洛谷P4377 [USACO18OPEN]Talent Show)(分数规划,二分答案,背包)

    分数规划是这样一个东西: 给定若干元素,每个元素有两个属性值\(a_i,b_i\),在满足题目要求的某些限制下选择若干元素并求出\(\frac{\sum a}{\sum b}\)的最大值. 如果没有限 ...

  7. CF-1055E:Segments on the Line (二分&背包&DP优化)(nice problem)

    You are a given a list of integers a 1 ,a 2 ,…,a n  a1,a2,…,an and s s of its segments [l j ;r j ] [ ...

  8. 【8.31校内测试】【找规律二分】【DP】【背包+spfa】

    打表出奇迹!表打出来发现了神奇的规律: 1 1 2 2 3 4 4 4 5 6 6 7 8 8 8 8 9 10 10 11 12 12 12 13 14 14 15 16 16 16 16 16.. ...

  9. bzoj 4753: [Jsoi2016]最佳团体【01分数规划+二分+树上背包】

    01分数规划,二分答案然后把判别式变成Σp[i]-Σs[i]*mid>=0,然后树上背包判断,设f[i][j]为在i点子树里选j个的最大收益,随便背包一下就好 最丧病的是神卡常--转移的时候要另 ...

随机推荐

  1. IE9的「console未被定义」错误

    IE从IE8+才支持console物件,但如上图所示,网页明明是IE9标准模式,为什么IE9却说console物件不存在? 但进行侦错,console.log()却又正常! 原因: IE8/IE9要先 ...

  2. 企业项目开发--cookie(2)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 2.1.3.CookieUtil:(cookie的基本操作:增删查,注意没有改)  1 package co ...

  3. java打包jar后,使之一直在linux上运行,不随终端退出而关闭

      nohup java -jar xxx.jar&

  4. Cesium Vue开发环境搭建

    最近被问到如何在 vuejs 中集成 cesium,首先想到的官网应该有教程.官网有专门讲 Cesium and Webpack(有坑),按照官网的说明,动手建了一个Demo,在这记录下踩坑过程. 一 ...

  5. IntelliJ IDEA中Debug的使用技巧

    当运行结果跟我们设想的不一致时,我们就可以用debug进行代码调试,下面是我在日常开发中对debug的一些小结 (一)基本介绍 本篇文章是基于IntelliJ IDEA2018.3.2版本,Debug ...

  6. Django模版语言自定义标签-实现前端 关联组合过滤查询

    前端关联 组合过滤查询 实现效果如图: models.py 创建表代码 from django.db import models # Create your models here. class Le ...

  7. How To Scan QRCode For UWP (2)

    这篇随笔主要介绍照相预览功能,重要使用的是MediaCapture对象,MediaCapture对象还可以用来处理录音和录制视频,本文只讨论照相功能. 1:查找摄像头 后置摄像头优先,找不到后置摄像头 ...

  8. Java读取文件加锁代码Demo(利用Java的NIO)

    本博文部分转载于:http://blog.csdn.net/wangbaochu/article/details/48546717 Java 提供了文件锁FileLock类,利用这个类可以控制不同程序 ...

  9. java—单例设计模式

    单例设计模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 1.构造方法私有化 2.声明一个本类对象 3.给外部提供一个静态方法获取对象实例 什么时候使用? 1.通过在工具类的设计中使用: ...

  10. .Net 鉴权授权

    在这里总结一下工作中遇到的鉴权和授权的方法 ① 固定token的方案 通过在nginx或者代码中写死token,或者通过在限制外网访问的方式已来达到安全授权的方式 ② session方案 分布式会话方 ...