CodeForces - 670D2 Magic Powder - 2(二分)
2017-07-21 12:13
302 查看
点击打开题目链接
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
题目大意:
有n种原料和k课魔法粉末,第二行为做一个饼干需要的每种原料数,第三行为每种原料总共有多少克。魔法粉末可以任意加到原料上,求最多可以做多少饼干。做的二分题还是少,当时没想到二分饼干数,一直超时样例。
思路:
二分饼干数求解。
附上AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=100000+5;
ll n,k;
ll maxd;
ll sum;
struct nodes{
ll need,have;
}node[maxn];
int main(){
ios::sync_with_stdio(false);
while(cin>>n>>k){
sum=0;
for(int i=1;i<=n;i++)
cin>>node[i].need;
for(int i=1;i<=n;i++){
cin>>node[i].have;
maxd=max((node[i].have+k)/node[i].need,maxd);
}
ll l=0,r=maxd;
while(l<=r){
ll mid=(l+r)/2;
ll tmp=k;
ll ans=0;
for(int i=1;i<=n;i++){
if(tmp<0){
ans=1;
break;
}
if(node[i].have/node[i].need>=mid)continue;
else if(node[i].have/node[i].need<mid){
tmp-=mid*node[i].need-node[i].have;
if(tmp<0){
ans=1;
break;
}
}
}
if(ans==1){//tmp<0,魔法粉末不够用
r=mid-1;
}
else if(ans==0){
sum=max(sum,mid);
l=mid+1;
}
}
cout<<sum<<endl;
}
return 0;
}
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
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
题目大意:
有n种原料和k课魔法粉末,第二行为做一个饼干需要的每种原料数,第三行为每种原料总共有多少克。魔法粉末可以任意加到原料上,求最多可以做多少饼干。做的二分题还是少,当时没想到二分饼干数,一直超时样例。
思路:
二分饼干数求解。
附上AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=100000+5;
ll n,k;
ll maxd;
ll sum;
struct nodes{
ll need,have;
}node[maxn];
int main(){
ios::sync_with_stdio(false);
while(cin>>n>>k){
sum=0;
for(int i=1;i<=n;i++)
cin>>node[i].need;
for(int i=1;i<=n;i++){
cin>>node[i].have;
maxd=max((node[i].have+k)/node[i].need,maxd);
}
ll l=0,r=maxd;
while(l<=r){
ll mid=(l+r)/2;
ll tmp=k;
ll ans=0;
for(int i=1;i<=n;i++){
if(tmp<0){
ans=1;
break;
}
if(node[i].have/node[i].need>=mid)continue;
else if(node[i].have/node[i].need<mid){
tmp-=mid*node[i].need-node[i].have;
if(tmp<0){
ans=1;
break;
}
}
}
if(ans==1){//tmp<0,魔法粉末不够用
r=mid-1;
}
else if(ans==0){
sum=max(sum,mid);
l=mid+1;
}
}
cout<<sum<<endl;
}
return 0;
}
相关文章推荐
- CodeForces 377B 优先队列 + 二分
- CodeForces - 862E 二分+错位求和技巧
- codeforces 16C C. Monitor(二分)
- Codeforces 651D Image Preview【思维+二分+前缀后缀和】
- Codeforces 633E. Startup Funding (概率+非递归线段树/Sparse Table +二分)
- 【CodeForces】660C - Hard Process(二分,尺取法)
- CodeForces 672D Robin Hood(二分 + 思维)
- codeforces 485D. Maximum Value(二分+思维)
- codeforces 626C 二分判定
- CodeForces - 344E Read Time && CodeForces - 830A Office Keys 二分+贪心
- codeforces 251A Points on Line(二分or单调队列)
- CodeForces 578C Weakness and Poorness 二分
- CodeForces 287B Pipeline(二分)
- Codeforces 456 div2 A B ***D(思维题) ***E(二分/DFS/数论)
- codeforces 350 div2 D Magic Powder - 2 二分
- Educational Codeforces Round 17 C && codeforces 762C C. Two strings (二分 + 字符串前后缀)
- CodeForces 371C Hamburgers(二分)
- CodeForces-232A【构造 二分】
- Codeforces 490E. Restoring Increasing Sequence 二分
- CodeForces - 672D Robin Hood (思维二分)