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;
}

最新文章

  1. PHP求职宝典系列——PHP Web 编程篇
  2. [UML]UML系列——类图class的泛化关系
  3. Javascript Function()中的降龙十八掌
  4. -bash: wget: command not found的两种解决方法
  5. duilib -- Label控件的bug(转载)
  6. php对图片反色处理
  7. 检测iOS的APP性能的一些方法
  8. Linux逻辑卷创建
  9. C语言课程设计—图书管理系统
  10. inheritAll 及 ant antfile案例分析
  11. jquery如何在加载完iframe的内容后才进行下一步操作
  12. cocos2d-x2.x环境搭建配置
  13. sqlite manager
  14. Spring配置属性文件
  15. python变量字符拼接
  16. 2018 年 3 月 iOS架构师 面试总结
  17. java根据概率生成数字
  18. DevC++出现[Error] ld returned 1 exit status,如何解决才好呢?
  19. Android数据存储:SQLite
  20. IntelliJ IDEA使用心得

热门文章

  1. 企业Openvpn环境部署
  2. PHP的基本语法
  3. 01分数规划POJ3621(最优比例生成环)
  4. RubyMine(基于IntelliJ IDEA的Ruby集成开发环境)快捷键
  5. Java基础(49):快速排序的Java封装(含原理,完整可运行,结合VisualGo网站更好理解)
  6. cocos2dx建立项目
  7. SQL——存储过程
  8. MapReduce之Mapper类,Reducer类中的函数(转载)
  9. linux_c学习笔记之curl的使用一
  10. laravel5.1启动详解