Codefroces D2. Magic Powder - 2(二分)
http://codeforces.com/problemset/problem/670/D2
http://codeforces.com/problemset/problem/670/D1
The term of this problem is the same as the previous one, the only exception — increased restrictions.
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.
1 1000000000
1
1000000000
2000000000
10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1 1 1 1 1
0
3 1
2 1 4
11 3 16
4
4 3
4 3 5 6
11 12 14 20
3
二分模板
#include<iostream>
using namespace std;
int binary_search(int a[],int l,int r,int k)
{
int mid,ans;
while(l<=r)
{
mid=(l+r)>>1;
if(a[mid]<=k){
l=mid+1;
ans=mid;
}
else r=mid-1;
}
return ans;//也可ans+n;
}
int main()
{
int a[11]={1,2,4,7,7,7,7,8,10,11};
cout<<binary_search(a,0,9,4)<<endl;
return 0;
}
题解
#include<iostream> #include<cstdio> using namespace std; long long a[100006],b[100006]; int main() { long long n,k,i; scanf("%lld%lld",&n,&k); for(i=0;i<n;i++) { scanf("%lld",&a[i]); } for(i=0;i<n;i++) { scanf("%lld",&b[i]); } long long l=0,r=2000000000; while(l<=r) { long long mid=(l+r)>>1; long long sum=0; for(i=0;i<n;i++) { if(mid*a[i]>b[i]) sum+=(mid*a[i]-b[i]); if(sum>k) break; } if(sum<=k) l=mid+1; else r=mid-1; } printf("%lld",l-1); return 0; }