Inviting Friends

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 241 Accepted Submission(s): 97

Problem Description

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, p1p2<=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

Source

2009 “NIT Cup” National Invitational Contest

二分+完全背包

由于不好计算具体的人的数量,可以提前估计好人的数量,采用二分的方式进行寻找答案

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; typedef long long LL; const int MAX = 1e5+10; int n,m; int L,R; int Dp[800000]; struct node
{
int x;
int y;
int s1;
int p1;
int s2;
int p2;
}Th[110]; int w[3],v[3]; int Judge()//估计人的数量范围
{
int tmp=INF;
for(int i=1;i<=n;i++)
{
if(m*1.0/Th[i].p1*Th[i].s1>m*1.0/Th[i].p2*Th[i].s2)
{
tmp=min(tmp,(m/Th[i].p1*Th[i].s1+Th[i].y)/Th[i].x);
}
else
{
tmp=min(tmp,(m/Th[i].p2*Th[i].s2+Th[i].y)/Th[i].x);
}
}
return tmp+10;
} int Backpack(int s,int need)//完全背包
{
for(int i=1;i<=need+Th[s].s2;i++)
{
Dp[i]=INF;
}
Dp[0]=0;
int tmp=need+Th[s].s2;
w[0]=Th[s].p1;
w[1]=Th[s].p2;
v[0]=Th[s].s1;
v[1]=Th[s].s2;
for(int i=0;i<2;i++)
{
for(int j=v[i];j<=tmp;j++)
{
Dp[j]=min(Dp[j-v[i]]+w[i],Dp[j]);
}
}
int Max=INF;
for(int i=need;i<=tmp;i++)
{
Max=min(Max,Dp[i]);
}
return Max;
} bool BB(int num)
{
int sum=0;
for(int i=1;i<=n;i++)
{
int tmp=num*Th[i].x-Th[i].y;
if(tmp<=0)
{
continue;
}
sum+=Backpack(i,tmp);
if(sum>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",&Th[i].x,&Th[i].y,&Th[i].s1,&Th[i].p1,&Th[i].s2,&Th[i].p2);
}
L=1;
R=Judge();
int ans=0;
while(L<=R)
{
int mid=(L+R)>>1;
if(BB(mid))
{
ans=max(ans,mid);
L=mid+1;
}
else
{
R=mid-1;
}
}
printf("%d\n",ans); }
return 0;
}

Inviting Friends(二分+背包)的更多相关文章

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

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

  2. 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 ] [ ...

  3. HDU 4606 Occupy Cities ★(线段相交+二分+Floyd+最小路径覆盖)

    题意 有n个城市,m个边界线,p名士兵.现在士兵要按一定顺序攻占城市,但从一个城市到另一个城市的过程中不能穿过边界线.士兵有一个容量为K的背包装粮食,士兵到达一个城市可以选择攻占城市或者只是路过,如果 ...

  4. UVA 1149 Bin Packing 二分+贪心

    A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...

  5. P1510 精卫填海

    P1510 精卫填海二分答案二分背包容量,判断能否满足v.判断的话就跑01背包就好了. #include<iostream> #include<cstdio> #include ...

  6. P2370 yyy2015c01的U盘

    P2370 yyy2015c01的U盘 题目背景 在2020年的某一天,我们的yyy2015c01买了个高端U盘. 题目描述 你找yyy2015c01借到了这个高端的U盘,拷贝一些重要资料,但是你发现 ...

  7. 洛谷 P2370 P2370 yyy2015c01的U盘

    https://www.luogu.org/problemnew/show/P2370 二分+背包 #include <algorithm> #include <iostream&g ...

  8. NOIP 模拟 $30\; \rm 毛三琛$

    题解 \(by\;zj\varphi\) 二分答案,考虑二分背包中的最大值是多少. 枚举 \(p\) 的值,在当前最优答案不优时,直接跳掉. 随机化一下 \(p\),这样复杂度会有保证. Code # ...

  9. 2021.8.4考试总结[NOIP模拟30]

    T1 毛衣衬 将合法子集分为两个和相等的集合. 暴力枚举每个元素是否被选,放在哪种集合,复杂度$O(3^n)$.考虑$\textit{meet in the middle}$. 将全集等分分为两部分分 ...

随机推荐

  1. MAXFLOAT

    CGSizeMake(300, MAXFLOAT),是计算宽和高的,里面的MAXFLOAT通俗点说就是最大的数值,代表你的label的宽和高是随着你label内容而变化,不用担心因为label内容过长 ...

  2. 设置UISegmentedControl中字体大小

    [segmentedControl setTitleTextAttributes:@{NSFontAttributeName : DYBoldFont(20)}  forState:UIControl ...

  3. struts2 radio标签 单选按钮

    <s:radio name="sex" label="性别" list="#{'男':'男','女':'女'}" value=&quo ...

  4. Android -- 背景虚化

    1,在项目中我们常有这样的需求,就是在个人中心的时候,当用户登录后,要显示用户登陆后的头像,然后背景是用户头像的虚化 ,接下来就来实现一下这个功能,先看一下效果 2,实现起来也挺简单的,没什么难度 , ...

  5. MSSQL 判断实例中是否存在某种表

    执行语句 SELECT 'SELECT * FROM '+Name+'..SysObjects Where XType=''U'' and name=''tab_scartrim'' ORDER BY ...

  6. 夺命雷公狗---Thinkphp----16之首页的完成及全站的完成

    刚才我们首页只是完成了一部分的数据,那么这里我们就来将他所以的完成: IndexController.class.php控制器代码如下所示: <?php namespace Home\Contr ...

  7. ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境(转)

    原文地址:http://blog.csdn.net/shangyuan21/article/details/18153605 我们都知道开发iPhone等ios平台的移动应用时需要使用Mac本,但是M ...

  8. 省市区三级联动(二)JS部分简单版

    通过对上一篇<省市区三级联动>的学习发现JScript部分省市区的填充代码几乎相同,所以可以写成一个函数. 注意:html部分和chuli.php部分不变 1.下拉列表填充可以写成带参数的 ...

  9. [Ubuntu] Ubuntu14.04 64bit 编译安装nginx1.7+php5.4+mysql5.6

    我的操作系统是Ubuntu14.04,其它linux系统的操作流程类似. 主要安装的软件是nginx1.7+php5.4+mysql5.6 1. 创建必要目录 sudo mkdir ~/setup s ...

  10. ASP.NET输出流至少要有256个字节的数据后Response.Flush方法才会生效

    很多时候我们写的asp.net程序会因为做很多操作,所以会花上一分钟甚至几分钟时间.为了使软件使用者能够耐心的等待程序的执行,我们经常会希望有一个进度条来表示程序执行的状态.或者最起码要显示一个类似: ...