Codeforces-1070E:Getting Deals Done(二分)

版权声明:http://blog.csdn.net/Mitsuha_。 https://blog.csdn.net/Mitsuha_/article/details/83657705

传送门

思路:完成的任务数量越多,花的时间也越多,二分完成的任务数量,那么如何确定d呢?

因为确定了完成任务的数量cnt,那么肯定是选前cnt小的 p i p_i 来完成,这样花费的时间相对也会少点,那么就确定d为第cnt小的 p i p_i

#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+10;
typedef long long ll;
ll n,m,Time;
ll a[MAX],b[MAX];
int cal(ll x,ll d)
{
    ll s=0,cnt=0,sum=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]>d)continue;
        cnt++;
        sum+=a[i];
        s+=a[i];
        if(cnt==x)return s<=Time;
        if(cnt&&cnt%m==0)
        {
            s+=sum;
            sum=0;
        }
        if(s>Time)return 0;
    }
    return 0;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%lld%lld%lld",&n,&m,&Time);
        for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
        for(int i=1;i<=n;i++)b[i]=a[i];
        sort(b+1,b+n+1);
        int l=1,r=n,ans=0;
        while(r>=l)
        {
            int mid=(l+r)/2;
            if(cal(mid,b[mid]))
            {
                l=mid+1;
                ans=mid;
            }
            else r=mid-1;
        }
        if(ans==0)puts("0 1");
        else printf("%d %lld\n",ans,b[ans]);
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/Mitsuha_/article/details/83657705