题目链接 http://codeforces.com/problemset/problem/670/D2
思路:二分查找可以制作多少个蛋糕,假如可以制作,那么每一种材料都必须充足。
直接上代码:
#include <bits/stdc++.h>
using namespace std;
__int64 a[111111],b[111111];
const int maxn= 2 * 1e9 + 2; //最多可以做这么多个蛋糕
__int64 n,k;
__int64 search(__int64 l,__int64 r)
{
__int64 mid,sum;
while(l <= r)
{
mid = ( l + r) / 2 ,sum = 0;
for (int i = 0 ; i < n ; i++ ) //遍历每种材料是否满足
{
sum += a[i] * mid - b[i] > 0 ? a[i] * mid - b[i] : 0 ;
if( sum > k)
break;
}
if(sum == k)
return mid;
else if(sum < k)
l = mid + 1;
else
r = mid - 1;
}
return r;
}
int main()
{
while (~scanf("%I64d %I64d",&n,&k))
{
for (int i = 0 ; i < n ; i++)
{
scanf("%I64d",&a[i]);
}
for (int i = 0 ; i < n ; i++ )
{
scanf("%I64d",&b[i]);
}
printf("%I64d\n",search(1,maxn));
}
return 0;
}