Fishing Master HDU - 6709 (贪心)acm

Posted yzxqq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Fishing Master HDU - 6709 (贪心)acm相关的知识,希望对你有一定的参考价值。

Heard that eomeom is a fishing MASTER, you want to acknowledge him as your mentor. As everybody knows, if you want to be a MASTER‘s apprentice, you should pass the trial. So when you find fishing MASTER eomeom, the trial is as follow: 

There are nn fish in the pool. For the ii - thth fish, it takes at least titi minutes to stew(overcook is acceptable). To simplify this problem, the time spent catching a fish is kk minutes. You can catch fish one at a time and because there is only one pot, only one fish can be stewed in the pot at a time. While you are catching a fish, you can not put a raw fish you have caught into the pot, that means if you begin to catch a fish, you can‘t stop until after kk minutes; when you are not catching fish, you can take a cooked fish (stewed for no less than titi) out of the pot or put a raw fish into the pot, these two operations take no time. Note that if the fish stewed in the pot is not stewed for enough time, you cannot take it out, but you can go to catch another fish or just wait for a while doing nothing until it is sufficiently stewed. 

Now eomeom wants you to catch and stew all the fish as soon as possible (you definitely know that a fish can be eaten only after sufficiently stewed), so that he can have a satisfying meal. If you can complete that in the shortest possible time, eomeom will accept you as his apprentice and say "I am done! I am full!". If you can‘t, eomeom will not accept you and say "You are done! You are fool!". 

So what‘s the shortest time to pass the trial if you arrange the time optimally?

InputThe first line of input consists of a single integer T(1T20)T(1≤T≤20), denoting the number of test cases. 

For each test case, the first line contains two integers n(1n105),k(1k109)n(1≤n≤105),k(1≤k≤109), denoting the number of fish in the pool and the time needed to catch a fish. 

the second line contains nn integers, t1,t2,,tn(1ti109)t1,t2,…,tn(1≤ti≤109) ,denoting the least time needed to cook the ii - thth fish.
OutputFor each test case, print a single integer in one line, denoting the shortest time to pass the trial.Sample Input

2
3 5
5 5 8
2 4
3 3

Sample Output

23
11

题意:
给你一个n,k,n表示有n条鱼,k表示每条吊每条鱼要k分钟。
然后一行有n个数,表示第i条鱼要用xi的时间去煮。
且当开始钓某条鱼后,直到把这条鱼钓起来,中间不能干别的事。
问煮好所有的鱼用时。
思路:
我们尽量在煮鱼的时间去钓鱼,那么对于每一个xi都会有xi%k(记为ai)的时间空余,是锅里在煮鱼,而人不在钓鱼的空闲时间。
对于每一个xi我们都可以用xi/k去统计钓起来的鱼数(记为s),但可能xi比较小,统计出来的s<n-1,这时候我们就要在多用时间去钓鱼(人在钓鱼,锅不在煮鱼)。
显然如果想要时间短,那么“人在钓鱼,锅不在煮鱼”的时间必然要小。所以我们对ai进行由大到小排序,累加前(n-1)-s个k-ai的值。
然后在加上煮鱼的总时间就是答案。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define met(a,b) memset(a,b,sizeof(a))
#define ios1 ios::sync_with_stdio(false)
#define ios2 cin.tie(0);cout.tie(0)
const int maxn = 999999999;
const ll mod = 1e7 + 9;

bool cmp(int a,int b)

    return a>b;

int main()

    int t;
    cin>>t;
    while(t--)
    
        int n,k;
        cin>>n>>k;
        int x[n+10];
        int a[n+10] ,la=1 ;
        int s=0;
        ll ans=k;
        for(int i=1;i<=n;i++)
        
             scanf("%d",&x[i]);
             ans+=x[i];
             if(x[i]>=k)s+=x[i]/k;
             a[la++]=x[i]%k;
        
         sort(a+1,a+la,cmp);
         //cout<<s<<"?????????"<<endl;
         if(s>=n-1)cout<<ans<<endl;
         else
         
             int up=n-1-s;
             for(int i=1;i<=up;i++)ans+=k-a[i];
             cout<<ans<<endl;
         

    

    return 0;

 




以上是关于Fishing Master HDU - 6709 (贪心)acm的主要内容,如果未能解决你的问题,请参考以下文章

Fishing Master (思维+贪心)

[CCPC2019 ONLINE]H Fishing Master

HDU_6709 CCPC网络赛H 优先队列 贪心

hdu 汉诺塔

Gone Fishing

Gone Fishing