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(1≤T≤20)T(1≤T≤20), denoting the number of test cases.

For each test case, the first line contains two integers n(1≤n≤105),k(1≤k≤109)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(1≤ti≤109)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;
}


原文地址:https://www.cnblogs.com/yzxqq/p/11407985.html

时间: 2024-10-29 11:43:32

Fishing Master HDU - 6709 (贪心)acm的相关文章

【Fishing Master HDU - 6709 】【贪心】

题意分析 题意:题目给出n条鱼,以及捕一条鱼所用的时间k,并给出煮每一条鱼的时间,问抓完并煮完所有鱼的最短时间. 附题目链接 思路: 1.捕第一条鱼的时间是不可避免的,煮每条鱼的时间也是不可避免的,这些都要算上. 2.可以优化的是煮鱼的时间,在时间允许的范围内可进行捕其他鱼.当然煮鱼的时间也许不够捕其他鱼,这就需要增加额外的时间. 3.设在煮每条鱼煮的时间内抓的最多的鱼数为cnt,捕鱼的时间为cost,将每次额外增加的时间存储在一个数组中,记为fre[ ]. 4.更多详细信息在代码中给出. AC

Fishing Master (思维+贪心)

题目网站:http://acm.hdu.edu.cn/showproblem.php?pid=6709 Problem Description Heard that eom 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 yo

hdu 4292 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4296 Problem Description Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building. T

hdu 4442 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4442 Problem Description WANGPENG is a freshman. He is requested to have a physical examination when entering the university. Now WANGPENG arrives at the hospital. Er-.. There are so many students, and the nu

hdu 1050(贪心算法)

Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19278    Accepted Submission(s): 6582 Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

hdu 4105 贪心思想

淋漓尽致的贪心思想 波谷一定是一位数,波峰一位数不够大的时候添加到两位数就一定够大了的. 当在寻找波谷碰到零了就自然当成波谷. 当在寻找波峰时碰到零时,将前面的波谷加到前一个波峰上,让当前的零做波谷,使得波谷的值尽量小,这就是本题最关键的贪心思想,一直想不到. 代码中:a表示前一个值,b表示当前考虑的值,tag为偶数时表示正在寻找波谷,奇数时在寻找波峰. #include<iostream> #include<cstdio> #include<cstring> #inc

HDU 4923 (贪心+证明)

Room and Moor Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that:

hdu 2037 贪心

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27361    Accepted Submission(s): 14439 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" &quo