CodeForces - 670D2 二分

http://codeforces.com/problemset/problem/670/D2


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

#include 
using namespace std;
typedef long long ll;
const ll MAXN = 2*1e9+5;
ll n, k;
ll a[100005], b[100005];
ll search(ll l, ll r)
{
	ll mid, sum;
	while (l <= r) {
		mid = (l+r)/2, sum = 0;
		//important, 遍历每种材料是否都满足条件,如果都满足条件,进入下一次判断
		for (int i = 0; i < n; i++) {
			if (b[i] < mid*a[i]) sum += (mid*a[i]-b[i]);
			if (sum > k)
				break;
		}
		if (sum == k) {//这里判断的就是看当前满足条件的mid下的sum 是否等于k。如果等于,二分结束
			return mid;
		}
		else if (sum < k) {
			l = mid+1;
		}
		else r = mid-1;
	}
	return l-1;
}
int main()
{
	while (~scanf("%lld %lld", &n, &k)) {
		for (int i = 0; i < n; i++) {
			scanf("%lld", &a[i]);
		}
		for (int i = 0; i < n; i++) {
			scanf("%lld", &b[i]);
		}
		printf("%lld\n", search(0, MAXN));
	}
	return 0;
}


你可能感兴趣的:(二分)