大吉大利

https://ac.nowcoder.com/acm/contest/911/I 

思路:二分答案;

#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define IT set<node>::iterator
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long LL;
typedef long long ll;
const int maxn=3e5+5;
ll a[maxn];
ll b[maxn];
ll c[maxn];
int n;
bool check(int x)//x枚
{
    ll res=0;
    for(int i=1;i<=n;i++)
        c[i]=a[i];
    c[1]-=x;
    if(c[1]<=0)
        return false;
    for(int i=2;i<=n;i++)
    {
        if(c[i]>=c[1])
        {
            if(c[i]>=b[i]&&b[i]>=c[1])
            {
                int temp=(c[i]-b[i])+1;
                if(temp%b[i]==0)
                {
                    res+=temp/b[i];
                    c[i]-=temp/b[i]*b[i];
                }
                else res+=temp/b[i]+1,c[i]-=(temp/b[i]+1)*b[i];
                if(c[i]>=c[1])
                    res++;//比b[i]小 再清空
            }
            else if(c[i]>=b[i]&&b[i]<c[1])//比c[1]小就好
            {
                int temp=(c[i]-c[1])+1;
                if(temp%b[i]==0)
                {
                    res+=temp/b[i];
                    c[i]-=temp/b[i]*b[i];
                }
                else res+=temp/b[i]+1,c[i]-=(temp/b[i]+1)*b[i];
            }
            else if(c[i]<b[i])
                res++;
        }
    }
    //cout<<x<<' '<<res<<' ';
    return res<=x;
}
int main()
{
    #ifdef ONLINE_JUDGE
    FAST_IO;
    #endif // ONLINE_JUDGE
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=2;i<=n;i++)
    {
        cin>>b[i];
    }
    int left=0,right=a[1];
    int ans=-1;
    while(left<=right)
    {
        int mid=(left+right)/2;
        if(check(mid))
        {
            ans=mid;
            right=mid-1;
        }
        else left=mid+1;
    }
//    if(ans==-1)
//    {
//        for(int i=0;i<=a[1]/2;i++)
//        {
//            if(check(i))
//            {
//                ans=i;
//                break;
//            }
//        }
//    }
    cout<<ans<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值