Codeforces Round #350 (Div. 2) D2. Magic Powder - 2 670D 【二分答案】
2016-07-20 11:19
351 查看
D2. Magic Powder - 2
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The term of this problem is the same as the previous one, the only exception — increased restrictions.
Input
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.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
Output
Input
Output
Input
Output
Input
Output
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define rep(i,l,r) for(int i = l;i<=r;i++)
#define Rep(i,l,r) for(int i = l;i<r;i++)
using namespace std;
const int maxn = 100010;
int n,k;
typedef long long ll;
int a[maxn],b[maxn];
/*bool check(int cnt)
{
ll kk = k;
Rep(i,0,n){
ll use = (ll)a[i]*cnt-b[i];
if(use<=0)continue;
else{
if(kk<use)return false;
else kk-=use;
}
}
return true;
}*/
bool check(int cnt) {
int kk = k;
for (int i = 0; i < n; ++i) {
ll owe = (ll)a[i] * cnt - b[i];
if (owe <= 0) continue;
else {
if (kk < owe) return false;
else kk -= owe;
}
}
return true;
}
void ir() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
for (int i = 0; i < n; ++i) scanf("%d", &b[i]);
}
int main()
{
scanf("%d%d",&n,&k);
Rep(i,0,n)scanf("%d",a+i);
Rep(i,0,n)scanf("%d",b+i);
int m = 0;Rep(i,0,n)m = max(m, (int)((b[i] + k) / a[i]) + 1);
int l = 0,r = m;
// [i,r)
while(r-l>1)
{
int mid = ((ll)r+l)>>1;
if(check(mid)){
l = mid;
}else{
r = mid;
}
}
printf("%d",l);
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The term of this problem is the same as the previous one, the only exception — increased restrictions.
Input
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.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
1 1000000000 1 1000000000
Output
2000000000
Input
10 1 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1 1 1 1 1 1 1 1 1 1
Output
0
Input
3 1 2 1 4 11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define rep(i,l,r) for(int i = l;i<=r;i++)
#define Rep(i,l,r) for(int i = l;i<r;i++)
using namespace std;
const int maxn = 100010;
int n,k;
typedef long long ll;
int a[maxn],b[maxn];
/*bool check(int cnt)
{
ll kk = k;
Rep(i,0,n){
ll use = (ll)a[i]*cnt-b[i];
if(use<=0)continue;
else{
if(kk<use)return false;
else kk-=use;
}
}
return true;
}*/
bool check(int cnt) {
int kk = k;
for (int i = 0; i < n; ++i) {
ll owe = (ll)a[i] * cnt - b[i];
if (owe <= 0) continue;
else {
if (kk < owe) return false;
else kk -= owe;
}
}
return true;
}
void ir() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
for (int i = 0; i < n; ++i) scanf("%d", &b[i]);
}
int main()
{
scanf("%d%d",&n,&k);
Rep(i,0,n)scanf("%d",a+i);
Rep(i,0,n)scanf("%d",b+i);
int m = 0;Rep(i,0,n)m = max(m, (int)((b[i] + k) / a[i]) + 1);
int l = 0,r = m;
// [i,r)
while(r-l>1)
{
int mid = ((ll)r+l)>>1;
if(check(mid)){
l = mid;
}else{
r = mid;
}
}
printf("%d",l);
return 0;
}
相关文章推荐
- top 内存字段解析
- [置顶] 多GPU运行Deep Learning 和 并行Deep Learning(待续)
- SystemUI下拉通知栏的源码分析
- 装饰模式(Decorator)
- Redis客户端开发包:Jedis学习-高级应用
- Java的native关键字以及JNI
- 嵌入式系统 Boot Loader 技术内幕【转】
- HttpRequestDeviceUtils
- mr的logs的查看
- 【phantomjs】使用phantomjs生成highChart的图片(待完善)
- 2016 Multi-University Training Contest 1 1001 Abandoned country (最小生成树+DFS)
- 模板使用入门示例
- urllib模块
- TabLayout的简单使用
- leetcode:word-break
- Java程序实现导出Excel的方法(支持IE低版本)
- Spring入门案例 注解方式配置(一)
- uva567
- web前端开发技术文档书写规范
- JQuery根据name属性查找