Fishing Master (thinking + greedy)

Topic website:
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 you find fishing MASTER eom, the trial is as follow:
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 k 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 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.
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!”.
So what’s the shortest time to pass the trial if you arrange the time optimally?


Input


The first line of input consists of a single integer
T(1≤T≤20), denoting the number of test cases.
For each test case, the first line contains two integers n(1≤n≤1e5),k(1≤k≤1e9)
, denoting the number of fish in the pool and the time needed to catch a fish.
the second line contains n integers, t1 , t2 , … ,tn (1≤ti≤109),denoting the least time needed to cook the i-th fish.


Output


For 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


Hint


Case 1: Catch the 3rd fish (5 mins), put the 3rd fish in, catch the 1st fish (5 mins), wait (3 mins),
take the 3rd fish out, put the 1st fish in, catch the 2nd fish(5 mins),
take the 1st fish out, put the 2nd fish in, wait (5 mins), take the 2nd fish out.
Case 2: Catch the 1st fish (4 mins), put the 1st fish in, catch the 2nd fish (4 mins),
take the 1st fish out, put the 2nd fish in, wait (3 mins), take the 2nd fish out.


Subject to the effect that there are n Zhuolai to cook fish, catch fish need every k minutes, but each fish cook t min and the fastest cook the fish all the time.
This question let me start directly with autistic teammate, the idea is to start a long time to cook fish to catch, but! Beginning to identify the sample proves feasible and knocked out, and then wa (-5). Later, out of a counter-example overthrown. In the last eight minutes ac came up with the idea, it is the time to cook each fish, if more than k, the time to cook the catch t [i] / k fish, and then to the rest of the array. So that we can come to a time of less than full array of k (if the fish catch has not finished, and if the fish has been finished off, then boiled fish like peace of mind). At this point no matter how many fish did not catch cook must catch a fish at the time of each fish and cook until done to catch all the fish. So at this time to get an array of descending order of how many fish did not catch, sum will increase the number of k, plus the rest of the fish cook time can answer.


Code


#include <bits / STDC ++ H.>
the using namespace STD;
typedef Long Long LL;
LL SUM;
int NUM, T, n-, K, A [100005];
int CMP (int A, int B) {return A> B; }
int main () {
 iOS :: sync_with_stdio (to false);
 CIN >> T;
 the while (T -) {
  CIN >> >> n-K;
  NUM =. 1; // up to catch a fish cooked
  sum = k; // first fish time
  for (int I =. 1; I <= n-; I ++) {
   CIN >> a [I];
   IF (NUM <n-) {
    int X = a [I] / K; // The current time may complete many boiled fish catch fish in most
    IF (NUM + X> = n-) {
     (n-- - NUM) A [I] = A [I] * K;
     SUM = SUM + (n-- NUM) * K ;
     NUM = n-;
    } the else {
     SUM = SUM + X * K;
     A [I] = A [I]% K;
     X + NUM = NUM;
    }
   }
  }
  Sort (. 1 + A, + n-A +. 1, CMP); // At this point in the array is less than the number K, so the first count time to catch fish, catch fish, but not complete boiled peace of mind of boiled fish good
  for (int I =. 1; I <= n-; I ++) {
   IF (NUM <n-) {
    SUM = SUM + K;
    NUM ++;
   } the else {
    SUM SUM + a [I] =;
   }
  }
  COUT SUM << << endl;
 }
 return 0;
}

Guess you like

Origin www.cnblogs.com/jjmmboom/p/11470233.html