Atcoder Beginner Contest 144 E - Gluttony(二分)

题目链接:https://atcoder.jp/contests/abc144/tasks/abc144_e
题意:
有n个人,每个人吃一单位东西的时间为ai,现在有n份食物,每份食物的份量为fi单位。
每个人必须对应一份食物,一个人只允许对应一份食物,一份食物也只允许对应一个人。你可以安排由哪个人吃哪份食物。每个人吃完食物的时间为ai* fi
在所有人开始吃之前,有k次培训,每一次培训可以让某个人的ai-1(最多减至0)。
问在经历k次培训后,要求n个人中吃完食物所需耗费的最长时间最短。输出最短的最长耗费时间。(即:每个人吃完食物的时间为ai* fj。求所有ai* fj中最大的,你可以通过培训和安排是这个最大值最小,输出这个最小的最大值)
题解:
二分答案。
判断函数中,记录满足这个答案所需要的最小的培训次数,如果培训次数小于等于k,则最终答案可以变小点;反之,最终答案要变大一点。
AC代码:

#include <bits/stdc++.h>
//#pragma GCC optimize(2)
using namespace std;
#define LL long long
const int maxn = 1e6 + 5;
int n;
LL a[maxn], b[maxn];
LL k, sum;
bool check(LL x)
{
    LL times=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]*b[i]<=x)
            continue;
        else
        {
            LL need=x/b[i];
            times+=a[i]-need;
        }
    }
    if(times<=k)
        return true;
    else 
        return false; 
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        sum += a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    sort(a,a+n);
    sort(b, b + n,greater<LL>());
    if (sum <= k)
    {
        cout << 0 << "\n";
        return 0;
    }
    else
    {
        LL l=0,r=1e12,m,ans=0;
        while(l<=r){
            m=(r-l)/2+l;
            if(check(m))
                r=m-1,ans=m;
            else
                l=m+1;           
        }
        cout<<ans<<"\n";
    }
    return 0;
}

欢迎评论!

猜你喜欢

转载自blog.csdn.net/wjl_zyl_1314/article/details/102801225