CodeChef Squirrel and chestnut 题解

Link to the original question: Squirrel and Chestnut
meaning of the questions: There \ (n \) squirrel in \ (m \) under the tree, every tree will (t_i \) \ fall a fruit of time, then every \ (p_i \) time will be a further fall, now \ (n \) squirrel want to get as quickly as possible \ (k \) a fruit, and squirrels can only move once, find the minimum amount of time.
Solution: the questions, obviously can think of half the answer, is not difficult to judge, so I'll just put the code:

#include <cstdio>
#include <algorithm>
using namespace std;
#define Maxn 100000
#define ll long long
ll a[Maxn+5],b[Maxn+5];
ll c[Maxn+5];
int n,m,k;
ll maxm(ll a,ll b){
    return a>b?a:b;
}
bool cmp(ll a,ll b){
    return a>b;
}
bool check(ll mid) {
    for(int i=0;i<m;i++) {
        if(mid<a[i]){
            c[i]=0;
        }
        else{
            c[i]=maxm(0,(mid-a[i]+b[i])/b[i]);
        }
    }
    sort(c,c+m,cmp);
    ll ans=0;
    for(int i=0;i<min(n,m);i++){
        ans+=c[i];
        if(ans>=k){
            return 1;
        }
    }
    return 0;
}
int main(){
    int t;
    scanf("%d",&t);
    ll l,r,mid;
    while(t--){
        scanf("%d%d%d",&m,&n,&k);
        for(int i=0;i<m;i++){
            scanf("%lld",&a[i]);
        }
        for(int i=0;i<m;i++){
            scanf("%lld",&b[i]);
        }
        l=1,r=Maxn-1;
        while(l<=r){
            mid=(l+r)>>1;
            if(check(mid)){
                r=mid;
            }
            else{
                l=mid+1;
            }
        }
        printf("%lld\n",l);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/withhope/p/11141270.html