HDU-6709 Fishing Master

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 you find fishing MASTER eom, the trial is as follow:c++

There are n fish in the pool. For the i - th fish, it takes at least ti minutes to stew(overcook is acceptable). To simplify this problem, the time spent catching a fish is kminutes. 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 k minutes; when you are not catching fish, you can take a cooked fish (stewed for no less than ti) 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.app

Now eom 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, eom will accept you as his apprentice and say "I am done! I am full!". If you can't, eom will not accept you and say "You are done! You are fool!".less

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

Input

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

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

the second line contains n integers, t1,t2,…,tn(1≤ti≤109) ,denoting the least time needed to cook the i - th fish.ip

Output

For each test case, print a single integer in one line, denoting the shortest time to pass the trial.ci

Sample Input

2
3 5
5 5 8
2 4
3 3

Sample Output

23
11

題解

對於每條魚,咱們確定要花費把它煮熟的時間,另外,咱們確定要花費釣上第一條魚的時間。以後咱們考慮每條魚的烹飪時間,若是在這段時間內能釣上魚,那就釣,若是都不能釣且還有魚沒釣,那就依次找須要等待時間最少的釣魚。咱們在減去釣上去的魚的時間後,剩下的時間排個序,選最大的時間,即等待時間最少,依次釣完魚便可。input

AC代碼

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 50;
ll a[N];
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        ll n, k;
        scanf("%lld%lld", &n, &k);
        for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
        ll cnt = 0;
        ll ans = 0;
        for (int i = 1; i <= n; i++) {
            ans += a[i];
            cnt += a[i] / k;
            a[i] %= k;
        }
        sort(a + 1, a + n + 1);
        for (int i = n; i >= 1; i--) {
            if (cnt >= n - 1) break;
            cnt++; ans += k - a[i];
        }
        printf("%lld\n", ans + k);
    }
    return 0;
}
相關文章
相關標籤/搜索