[Fishing Master HDU - 6709] [greedy]

Analysis of the meaning of problems

Meaning of the questions : Title given n fish, catch a fish and used the time k, and gives each fish cook time, ask grab finished and cook the shortest time all the fish.
Attached topic Link
ideas :
1. catch the first fish of the time is inevitable, each fish cook time is inevitable, these must count.
2. Zhu Yu can be optimized is the time, within the time allowed in the other fish may be caught. Of course, boiled fish and perhaps not enough time to catch other fish, which requires additional time.
3. The maximum number of fish disposed in the cooking time to cook each fish is caught CNT, fishing time cost, each additional time will be stored in an array, referred to as a fre [].
4. More details are given in the code.

AC Code

/*贪心*/
#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);
    }
}

Reference blog: http://morecoder.com/article/1265379.html

Guess you like

Origin www.cnblogs.com/KeepZ/p/11408837.html
Recommended