首页 > 代码库 > CodeForces 670D Magic Powder

CodeForces 670D Magic Powder

二分。

二分一下答案,然后验证一下。

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<iostream>using namespace std;typedef long long LL;const double pi=acos(-1.0),eps=1e-6;void File(){    freopen("D:\\in.txt","r",stdin);    freopen("D:\\out.txt","w",stdout);}template <class T>inline void read(T &x){    char c=getchar(); x=0;    while(!isdigit(c)) c=getchar();    while(isdigit(c)) {x=x*10+c-0; c=getchar();}}const int maxn=100010;LL k,a[maxn],b[maxn];int n;bool check(LL x){    LL sum=0;    for(int i=1;i<=n;i++)    {        if(a[i]*x<=b[i]) continue;        sum=sum+a[i]*x-b[i];        if(sum>k) return 0;    }    if(sum<=k) return 1;    return 0;}int main(){    scanf("%d%lld",&n,&k);    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);    for(int i=1;i<=n;i++) scanf("%lld",&b[i]);    LL L=0,R=3e9,ans;    while(L<=R)    {        LL mid=(L+R)/2;        if(check(mid)) L=mid+1,ans=mid;        else R=mid-1;    }    printf("%lld\n",ans);    return 0;}

 

CodeForces 670D Magic Powder