FishingMaster (HDU-6709) [greedy]

Topic links: https://vjudge.net/problem/HDU-6709

Meaning of the questions: n a person to catch fish, catch a fish every time with a K, a cooking time of each fish with a [i], the process can not be interrupted catch fish, cooking fish when you can catch fish, can also be more cooking for a while , n seek complete cooking fish minimum time required.

:( idea to learn the idea of the chiefs) https://blog.csdn.net/qq_40655981/article/details/100104985

  Provided num = Σ (a [i] / k),

  The problem can be discussed in two cases:

    1.num> = (n-1), a cooking i fish each case, while grasping (a [i] / k) to ensure removal of fish out of every K times in the cooking of fish.

    2.num <(n-1), this case needs to wasted time at least, need to (ka [i]% k) in ascending order of arrest and cooking fish.

code show as below:

#include<cstdio>
#include<algorithm>
using namespace std;
int t,n,k;
int a[100010];

bool cmp(int x1,int x2){
    return (k-x1%k)<(k-x2%k);
}
int main(){
    int num;
    long long ans;
    scanf("%d",&t);
    while(t--){
        num=0;
        scanf("%d%d",&n,&k);
        ans=k;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            num+=a[i]/k;
            ans+=a[i];
        }
        if(num<n-1){
            sort(a+1,a+1+n,cmp);
            num=n-num-1;
            for(int i=1;i<=num;i++){
                ans+=(k-a[i]%k);
            }
        }
        printf("%lld\n",ans);
    }
}

 

Guess you like

Origin www.cnblogs.com/xxmlala-fff/p/11784563.html