codeforces 670D2

D2. Magic Powder - 2
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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克魔法粉,每份魔法粉能能當一份任一材料,現在已知做一個蛋糕需要a1,a2…an份材料,你有b1,b2…bn份材料,問你最多能做出多少個蛋糕.
分析:二分答案,l=0,r=max((a[i]+k)/b[i])(可以更優化,但沒必要)

#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
long long a[100010],k,maxi,n,b[100010];
void readdata()
{
    scanf("%d%d",&n,&k);
    for (long long i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for (long long i=1;i<=n;i++)
    {
        scanf("%d",&b[i]);
        maxi=max(maxi,(b[i]+k)/a[i]+1);
    }
}
bool check(long long x)
{
    long long z=k;
    for (long long i=1;i<=n;i++)
    {
        long long w=b[i]-a[i]*x;
        if (b[i]-a[i]*x<0)
        {
            z=z+w;
        }
        if (z<0) return false;
    }
    return true;
}
void work()
{
    long long l=0;
    long long r=maxi;
    while (l+1<r)
    {
        long long mid=((long long)l+r)>>1;
        if (check(mid)) l=mid;
        else r=mid;
    }
    printf("%d",l);
}
int main()
{
    readdata();
    work();
}

重要的東西放最後 :越界!!!(wa了別怪我)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章