题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6709
题目:
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≤105),k(1≤k≤109), 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.
题意:鱼塘里面有n条鱼。每条鱼需要ti 分钟来煮。为了简化问题,每条鱼的捕捉时间都是k分钟。你一次只能捕捉一条鱼,因为只有一个锅,而且一个锅一次也只能煮一条鱼。当你要捕鱼的时候,你不能煮鱼,这意味着,你要等到捕鱼时间k结束之后才能开始煮鱼;当你不在捕鱼的时候,你可以把鱼从锅里拿出来或者把鱼放进锅里,这两个操作都不花时间。需要注意的是,鱼在锅里煮的时间不够的话是不可以拿出来的,但是这个时候你可以去捕鱼或则什么都不干等到鱼煮好。求煮完所有鱼花费的最短时间。
题解:第一次的捕鱼时间是肯定有的,接下来我们就要判断煮鱼的时间内是否可以把鱼全部捕完。毕竟煮鱼的时间都是固定的,所以我们只需要判断还需要多少捕鱼时间即可。也就是花费的时间 = 第一条捕鱼时间+每条鱼煮的时间+多余的捕鱼时间。需要注意的是,煮鱼时间要进行%k处理(k为捕鱼固定时间),多出来的每条捕鱼时间=捕鱼固定时间-处理过之后的煮鱼时间。所以要想多出来的捕鱼时间越小,那处理过的煮鱼时间就要越大。
代码及注释如下:
#include
#include
using namespace std;
typedef long long ll;
ll a[100005],n,m,ans;
int main() {
int T,cnt;
ios::sync_with_stdio(false);
cin>>T;
while(T--) {
cin>>n>>m;
ans=m,cnt=0;//第一次抓鱼的时间
for(int i=1;i<=n;i++) {
cin>>a[i];
ans+=a[i];//每一次煮鱼的时间,先加再处理
cnt+=a[i]/m;//统计在煮鱼期间,可以捕鱼的次数
a[i]%=m;//处理煮鱼时间
}
if(cnt>=n-1) {
//煮鱼的时间够抓n-1的鱼(因为第一条抓鱼时间必须有)
cout<<ans<<endl;
}
else {
sort(a+1,a+1+n);//从小到大排序,所以接下来的i从n开始
for(int i=n;cnt<n-1;cnt++,i--) {
//cnt为n-1时结束(捕鱼时间够了)
ans+=m-a[i];//浪费的时间为m-a[i]
}
cout<<ans<<endl;
}
}
return 0;
}