【Fishing Master HDU - 6709 】【贪心】

题意分析

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

AC代码

/*贪心*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = int(1e5+10);
int T, n, k;
int cnt;        //在煮鱼期间最多能钓上来的鱼数
int t[maxn], fre[maxn];
LL cost;
int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &k);
        cost = k;       //捕第一条鱼的时间不可避免
        cnt = 0;
        memset(t, 0, sizeof(t));
        memset(fre, 0, sizeof(fre));
        for(int i = 0; i < n; i++)
        {
            scanf("%lld", &t[i]);
            cost += t[i];       //煮鱼的时间不可避免
            cnt += t[i] / k;
            fre[i] = k - t[i] % k;      //k - 每次煮鱼的时间所剩余的可利用时间 = 多投入的时间
        }
        if(cnt < n - 1)
        {
            sort(fre, fre + n);
            for(int i = 0; i < n - 1 - cnt; i++)     //贪心思想,从小往大加
                cost += fre[i];
        }
        printf("%lld\n", cost);
    }
}

参考博客:http://morecoder.com/article/1265379.html

原文地址:https://www.cnblogs.com/KeepZ/p/11408837.html

时间: 2024-10-30 05:13:25

【Fishing Master HDU - 6709 】【贪心】的相关文章

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 fi

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 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

HDU 4932 贪心

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 191    Accepted Submission(s): 38 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

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