codeforces 670D2. Magic Powder - 2

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种原料,每一种ai克。现在有每种原料分别bi克。而且你现在还有k克的魔法粉末,每一克粉末都可以当作任意一种原料一克使用。球最多能制作多少个饼干

思路:

直接二分饼干数+贪心即可


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int inf = 2147483647;
const double PI = acos(-1.0);
const double e = 2.718281828459;
const int mod = 1000000007;
typedef long long LL;
#pragma comment(linker, "/STACK:102400000,102400000")
//freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中cin
struct node
{
	LL a, b;
}x[100005];
int main()
{
	LL n, k;
	int i, j;
	while (cin >> n >> k)
	{
		for (i = 1; i <= n; ++i)
		{
			scanf("%lld", &x[i].a);
		}
		LL m = 0;
		for (i = 1; i <= n; ++i)
		{
			scanf("%lld", &x[i].b);
			m = max((x[i].b + k) / x[i].a, m);
		}
		LL L = 0, R = m;
		LL mmax = 0;
		//cout << L << R << endl;
		while (L <= R)
		{
			LL mid = (L + R) / 2;
			LL p = k;
			LL ans = 0;
			
			for (i = 1; i <= n; ++i)
			{
				if (p < 0)
				{
					ans = 1;
					break;
				}
				if (x[i].b / x[i].a >= mid)
					continue;
				else
				{
					p -= mid * x[i].a - x[i].b;
				}
				if (p < 0)
				{
					ans = 1;
					break;
				}			
			}
			if (ans)
			{
				R = mid - 1;
			}
			else
			{
				mmax = max(mmax, mid);
				L = mid + 1;
			}
			//cout << mid <<" "<<mmax << endl;
		}
		printf("%lld\n", mmax);

	}
}
/*
3 2
2 1 4
11 3 16

*/



你可能感兴趣的:(codeforces 670D2. Magic Powder - 2)