[Greedy, dp] 2019 Chinese Collegiate Programming Contest (CCPC) - Network trials Fishing Master (Problem - 6709)

Topic: http://acm.hdu.edu.cn/showproblem.php?pid=6709

Fishing Master

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 631    Accepted Submission(s): 170


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

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

For each test case, the first line contains two integers n(1n105),k(1k109), 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(1ti109) ,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.
 

 

Source
 

 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6712  6711  6710  6709  6708 

Meaning of the questions:

N-fish, the fish at least i minute stewed ti (ti may exceed), just need to catch a fish min k (k min of not less than nor greater than k min and can not be stopped halfway)
catch the fish can be stored unlimited fish, a fish stew but each time only and can not be stopped midway (after the fish stew must be put into the pot ti minutes), you can go fishing or wait at the fish stew, after a good fish stew can immediately (do not take the time) to come into fish or fish
asked to catch these fish stew finish and complete a minimum number of minutes it takes

Ideas:

Consider the ideal situation, just spend the extra time to catch a fish, and the rest of the time to fish stew, which is ans = k + Σti, this is the time to take advantage of, because when he Dunyu fishing, the number of fish available to catch each time Dunyu [ti / k],
but this time the presence of Σ [ti / k] <n-1, where (n-1 because the first time has been fished a fish), that is, when the fish stew can not catch all the fish are finished, then this will result in a waste of time, waste of time in order to make the sum of the minimum, we need to
figure out how much time is wasted each time at least, now when you can catch fish stew [ti / k] fish, but may not reuse All Offers to time, have to wait ti% k of the time, but now can not catch all the fish in a fish stew finish time, we also need to catch need = n-1-Σ [ ti / k] fish,
take time now to consider additional spending time to go fishing, you can spend extra time k need * to go fishing, or wait ti% k time when some fish stew go fishing, because the fish need k minutes and can not be stopped, but now the time ti% k Dunyu will stop, so we need to continue to waste k-ti when Dunyu% k,
can be found k> k-ti% k, show Then those programs better, so if we need additional need to catch fish, you need a small pre-k-ti% k choose to go to waste, it is simply a waste of time the front row after a good sequence need a waste of time added to the final ans output ans just fine

In this sample the game out of the question:


input:

1
3 500
999 20 20

output:

1540

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int amn=1e5+5;
 4 int t[amn],waste[amn];
 5 int main(){
 6 //    cout<<500-(999%500);
 7     int T,n,k;
 8     long long ans,cancatch;
 9     scanf("%d",&T);
10     while(T--){
11         scanf("%d%d",&n,&k);
12         k = ANS; cancatch = 0 ;        /// first k must spend minutes to catch fish, the total time is initialized to k, the time phishing Dunyu initialized to 0 
13 is          for ( int I = 0 ; I <n-; ++ I ) {
 14              Scanf ( " % D " , & T [I]);
 15              ANS = + [I] T;           /// total time plus time Dunyu 
16              cancatch = T + [I] / K;    /// stew number of fish fishing time T = + [I] / K 
. 17              waste [I] = KT [I]% K;   /// each waste time KT = [I] K% 
18 is          }
 . 19          IF (cancatch <N- . 1 ) {        ///If the fish is not completely off when Dunyu portion 
20 is              int need = N- . 1 -cancatch;       /// need also need to catch fish 
21 is              ; Sort (Waste, n-Waste +)         /// prior to sorting waste of time, taking need a small waste of time added to the total time 
22 is              for ( int I = 0 ; I <need; I ++) ANS + = waste [I];
 23 is          }
 24          the printf ( " % LLD \ n- " , ANS);
 25      }
 26 is  }
 27  / * *
 28  n-fish, the fish at least i minute stewed ti (ti may exceed), just need to catch a fish min k (k min of not less than nor greater than k min and can not be stopped halfway)
 29 Catch a fish can keep unlimited fish, a fish stew but each time only and can not be stopped midway (after the fish stew must be put into the pot ti minutes), you can go fishing or wait at the fish stew, fish stew can be a good immediately after (do not take the time) to come up with fish or fish into the
 30  asked to catch these fish stew finished and finished how many minutes it takes a minimum of
 31  
32  cases consider ideal, just spend the extra time to catch a fish, the rest of the time Dunyu are used, i.e. ans = k + Σti, it is to take advantage of this time, since the fishing can Dunyu time, the number of fish available to catch each time Dunyu [ti / k],
 33  but this time the presence of Σ [ti / k] <n-1, where (n-1 because the first time has to catch a fish), boiled fish i.e. when all fish catch can not finished, then this will result in a waste of time, waste of time in order to make the sum of the minimum, we need
 34  to find out how much time is wasted each time at least, now when you can catch fish stew [ti / k] fish, but may not be reused Dunyu to time, but also waiting time ti% k, but not all fish boiled fish catch End of time, but also to catch need = n-1-Σ [ ti / k] fish,
 35  now test To take into account the extra time to go fishing, you can spend extra time need * k to go fishing, or wait ti% k time when some fish stew take when fishing because fishing requires k minutes and can not be stopped, but now ti% All Offers will stop after k time, so we need to continue to waste k-ti Dunyu when k%,
 36  can be found k> k-ti% k, the latter scheme is clearly better, so if we need additional pieces need to catch fish, before the election need small k-ti% k go to waste, which is a waste of time for the front row of the sorted waste of time to the need of a final output ans ans like
 37 [  
38 is  the game shown in this sample the problem:
 39 input:
40 
41 1
42 3 500
43 999 20 20
44 
45 output:
46 
47 1540
48 
49 **/

 

Guess you like

Origin www.cnblogs.com/Railgun000/p/11403678.html