问题

This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.

Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.

Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.

Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.

输入 

The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.

The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

输出 

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

Sample 1

Inputcopy Outputcopy
3 1
2 1 4
11 3 16
4

Sample 2

Inputcopy Outputcopy
4 3
4 3 5 6
11 12 14 20
3

Note

In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.

In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.

解题思路 :

做一块饼干需要n种材料,k个魔法材料,

ai为每种材料的克数

bi为现有每种材料的总数,魔法材料可以代替任何一种材料,问最大能做多少。

二分思想:最多能做多少个,从比题目给的范围大的范围里二分,比如左边0,右边10000,从这个区间内开始二分。比方做一个所用到材料数乘以n的结果,小于等于我们所现有的总材料了,这n个才能做出来,如果大于我们所拥有的材料总数,说明我们的材料不够,需要从k个魔法材料里面拿,如果魔法材料被拿完了,变成负数,就意味着这n个做不成,然后缩小n的范围,否则就增大n的范围

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;int n,m;
int g[1010],k[1010],p[1010];int check(int x)
{int res=0;for(int i=0; i<n; i++){p[i]=k[i]-x*g[i];if(p[i]<0) res-=p[i];}if(res<=m) return 1;else return 0;
}int main()
{cin>>n>>m;for(int i=0; i<n; i++) cin>>g[i];for(int i=0; i<n; i++) cin>>k[i];int l=0,r=10000;while(l<r){int mid=(l+r+1)>>1;if(check(mid)) l=mid;else r=mid-1;}cout<<l;return 0;
}

输入

The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.

The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

输出

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

Sample 1

Inputcopy Outputcopy
1 1000000000
1
1000000000
2000000000

Sample 2

Inputcopy Outputcopy
10 1
1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000
1000000000 1000000000
1 1 1 1 1 1 1 1 1 1
0

Sample 3

Inputcopy Outputcopy
3 1
2 1 4
11 3 16
4

Sample 4

Inputcopy Outputcopy
4 3
4 3 5 6
11 12 14 20
3
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctype.h>
using namespace std;
typedef long long ll;ll n,m;
ll g[100010],k[100010],p[100010];ll check(ll x)
{ll res=0;for(ll i=0; i<n; i++){if(g[i]*x>k[i]) res=res+g[i]*x-k[i];if(res>m) return 0;}return 1;
}int main()
{cin>>n>>m;for(int i=0; i<n; i++) cin>>g[i];for(int i=0; i<n; i++) cin>>k[i];ll l=0,r=2000000000;while(l<r){ll mid=(l+r+1)>>1;if(check(mid)) l=mid;else r=mid-1;}cout<<l;return 0;
}

和前面那个一样,注意数据范围,还有check函数里if判断条件不能像第一个那样写,数据太大,乘法和减法一块会容易出错,最好像第二个代码那样写

Magic Powder - 1,2相关推荐

  1. Magic Powder - 2

    https://codeforces.com/contest/670/problem/D2 The term of this problem is the same as the previous o ...

  2. 【CodeForces - 670D1 】Magic Powder - 1 (模拟 或 枚举 或二分优化)

    题干: This problem is given in two versions that differ only by constraints. If you can solve this pro ...

  3. codeforce之Magic Powder

    题目: The term of this problem is the same as the previous one, the only exception - increased restric ...

  4. codeforces 670D1 Magic Powder - 1

    题目链接:http://codeforces.com/problemset/problem/670/D1 题目:Magic Powder - 1 time limit per test 1 secon ...

  5. CodeForces 670D2 Magic Powder - 2

    Description The term of this problem is the same as the previous one, the only exception - increased ...

  6. Magic Powder - 2 (CF 670_D)

    http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous ...

  7. 滑盖全面屏对决!荣耀 Magic 2 发布,正面刚小米 MIX 3

    赶在 10 月份的最后一天,荣耀 Magic 2 终于在整整两个月的官方预热中迎来了发布会:实际上,这款产品已经曝光得差不多了. 不过,作为一款滑盖全面屏手机,荣耀 Magic 2 的关注点其实有两个 ...

  8. 大新闻!Magic Leap造假,HoloLens即将入华商用

    昨天微软搞了大新闻,Terry和Alexi到了深圳,在WinHEC大会上宣布了2017上半年HoloLens正式入华商用. 而唯一竞争对手Magic Leap今天也被曝光其设备造假,各大科技媒体纷纷报 ...

  9. E - Magic Powder - 2(二分查找)

    该题数据量较大,根据题目答案最大2*10^9,可以通过二分法查找最大做的蛋糕数,用魔法素材把每个材料补充到二分的值需要的素材数,判断总共需要的素材数和k的大小关系进行二分 #include<io ...

最新文章

  1. 还是这个序列化的解释比较好懂
  2. mysql自增长2个增加_mysql – 添加第二个自动增量字段并允许重复
  3. 传智C++课程笔记-1
  4. Java Web——ResponseBean类DEMO
  5. Jenkins + Pipeline 构建流水线发布
  6. 集群NAS+SSD如虎添翼
  7. Nginx系列1之部分模块详解
  8. SmartDial - 简单你的生活
  9. Ubuntu12.04 apt-get 安装mysql
  10. EDI系统-AS2传输常见问题
  11. 电子邮件收发原理和JavaMail开发
  12. JSON 的几种简单格式和转换
  13. 驱动人生服务器正在维护,驱动人生驱动更新失败或者设备出现异常的解决方法...
  14. Dreamweaver视频教程(共53课时)
  15. 怎样开发微信小程序(最初的页面)
  16. 关于电视剧《狂飙》的一点感悟--贵人相助的重要性
  17. 外卖订单爬虫 定时自动抓取三大外卖平台上商家订单
  18. 被迫解除劳动关系通知书
  19. SVL-Simulation自动驾驶仿真器
  20. 3D Object Detection 3D目标检测综述

热门文章

  1. [Swagger] Asciidoc 配置静态章节
  2. 姿态和旋转矩阵的探究
  3. html5流程图制作,基于HTML5小巧精悍流程图绘制工具jsPlumb
  4. 张小龙演讲爆瓜:究竟是谁在监视你的手机?| 微信十年
  5. 大学生如何选择人生第一份工作
  6. SONiC镜像编译指南
  7. 蓝桥杯51单片机(一)超声波模块
  8. 睡可睡,此睡非彼睡,做个会睡觉的--IT人员!
  9. SO(3)的不可约表示
  10. 【动态规划】洛谷P2196 挖地雷