题目: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(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.

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

题意:

有n条鱼,第i条鱼至少要被炖ti分钟(可以超过ti),钓一条鱼恰好需要k分钟(不能少于k分钟也不能大于k分钟且中途不能停止)
钓了鱼可以无限存鱼,但每次只能炖一条鱼且中途不能停止(鱼放进锅里后必须被炖ti分钟),在炖鱼时可以去钓鱼或等待,鱼炖好后可以立刻(不花时间)地拿出鱼或放进鱼
问钓完并炖完这些鱼最少要花费多少分钟

思路:

先考虑最理想的情况,只需额外花费钓一条鱼的时间,剩下的时间都用来炖鱼,也就是ans=k+Σti,这样是能充分利用时间的,因为在炖鱼的时候就可以钓鱼,每次炖鱼时可钓的鱼的数量为[ti/k],
但这种时候存在Σ[ti/k]<n-1的情况(n-1是因为第一次已经钓了一条鱼),也就是在炖鱼时不能把全部鱼都钓完,那么这样就会造成时间的浪费,为了使浪费的时间之和最少,我们需要先
搞清楚每次最少会浪费多少时间,现在炖鱼时可以钓[ti/k]条鱼,但可能不会重复利用到炖鱼的时间,还要等待ti%k的时间,但现在无法在炖鱼的时间钓完所有鱼,还需要钓need=n-1-Σ[ti/k]条鱼,
现在考虑要额外花时间去钓鱼,可以额外花need*k的时间去钓鱼,或在某些炖鱼时等待ti%k的时间时拿去钓鱼,因为钓鱼需要k分钟且不能停止,但现在ti%k时间后炖鱼才会停止,所以我们需要浪费k-ti%k时才能继续炖鱼,
可以发现k>k-ti%k,显然后者方案更优,所以如果我们需要额外钓need条鱼,则选前need小的k-ti%k去浪费,也就是对浪费时间排好序后把前need个浪费时间加到中ans最后输出ans就好了

比赛时在这个样例出了问题:

input:

1
3 500
999 20 20

output:

1540

 #include<bits/stdc++.h>
using namespace std;
const int amn=1e5+;
int t[amn],waste[amn];
int main(){
// cout<<500-(999%500);
int T,n,k;
long long ans,cancatch;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
ans=k;cancatch=; ///第一次必须要花k分钟去抓鱼,总时间初始化为k,在炖鱼时间钓鱼数量初始化为0
for(int i=;i<n;i++){
scanf("%d",&t[i]);
ans+=t[i]; ///总时间加上炖鱼时间
cancatch+=t[i]/k; ///炖鱼时间钓鱼数量+=t[i]/k
waste[i]=k-t[i]%k; ///每次浪费时间=k-t[i]%k
}
if(cancatch<n-){ ///如果炖鱼时没有掉完全部的鱼
int need=n--cancatch; ///need为还需要钓的鱼
sort(waste,waste+n); ///给浪费时间排序,取前need小的浪费时间加入总时间
for(int i=;i<need;i++)ans+=waste[i];
}
printf("%lld\n",ans);
}
}
/**
有n条鱼,第i条鱼至少要被炖ti分钟(可以超过ti),钓一条鱼恰好需要k分钟(不能少于k分钟也不能大于k分钟且中途不能停止)
钓了鱼可以无限存鱼,但每次只能炖一条鱼且中途不能停止(鱼放进锅里后必须被炖ti分钟),在炖鱼时可以去钓鱼或等待,鱼炖好后可以立刻(不花时间)地拿出鱼或放进鱼
问钓完并炖完这些鱼最少要花费多少分钟 先考虑最理想的情况,只需额外花费钓一条鱼的时间,剩下的时间都用来炖鱼,也就是ans=k+Σti,这样是能充分利用时间的,因为在炖鱼的时候就可以钓鱼,每次炖鱼时可钓的鱼的数量为[ti/k],
但这种时候存在Σ[ti/k]<n-1的情况(n-1是因为第一次已经钓了一条鱼),也就是在炖鱼时不能把全部鱼都钓完,那么这样就会造成时间的浪费,为了使浪费的时间之和最少,我们需要先
搞清楚每次最少会浪费多少时间,现在炖鱼时可以钓[ti/k]条鱼,但可能不会重复利用到炖鱼的时间,还要等待ti%k的时间,但现在无法在炖鱼的时间钓完所有鱼,还需要钓need=n-1-Σ[ti/k]条鱼,
现在考虑要额外花时间去钓鱼,可以额外花need*k的时间去钓鱼,或在某些炖鱼时等待ti%k的时间时拿去钓鱼,因为钓鱼需要k分钟且不能停止,但现在ti%k时间后炖鱼才会停止,所以我们需要浪费k-ti%k时才能继续炖鱼,
可以发现k>k-ti%k,显然后者方案更优,所以如果我们需要额外钓need条鱼,则选前need小的k-ti%k去浪费,也就是对浪费时间排好序后把前need个浪费时间加到中ans最后输出ans就好了 比赛时在这个样例出了问题:
input: 1
3 500
999 20 20 output: 1540 **/

[贪心,dp] 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 Fishing Master (Problem - 6709)的更多相关文章

  1. [BFS,A*,k短路径] 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 path (Problem - 6705)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6705 path Time Limit: 2000/2000 MS (Java/Others)    Mem ...

  2. 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛(8/11)

    $$2019中国大学生程序设计竞赛(CCPC)\ -\ 网络选拔赛$$ \(A.\hat{} \& \hat{}\) 签到,只把AB都有的位给异或掉 //#pragma comment(lin ...

  3. HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...

  4. 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛

    传送门 A.^&^ 题意: 找到最小的正数\(C\),满足\((A\ xor\ C)\&(B\ xor \ C)\)最小. 思路: 输出\(A\&B\)即可,特判答案为0的情况 ...

  5. 【赛后总结+部分题解】2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛

    赛后总结: T:今天状态一般,甚至有点疲惫.然后12点比赛开始,和队友开始看题,从最后往前面看,发现数学题公式看不懂.然后发现队友已经双开做1001和1006了,我看着1007有人A,开始做1007. ...

  6. 2019中国大学生程序设计竞赛-女生专场(重现赛)部分题解C-Function(贪心+优先队列) H-clock(模拟)

    Function 题目链接 Problem Description wls 有 n 个二次函数 Fi(x) = aix2 + bix + ci (1 ≤ i ≤ n). 现在他想在∑ni=1xi = ...

  7. 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 A题

    A - ^&^ Bit operation is a common computing method in computer science ,Now we have two positive ...

  8. 【2019中国大学生程序设计竞赛-女生专场】C - Function

    原题 韦神提供的思路orz 首先一个显然的性质,所有的c可以提出来,方程变成ax^2+bx的形式 因为x的值是离散的,而m的值又不大 所以一开始让x都为1(注意!x是正整数),然后每次挑一个x让他加一 ...

  9. HDU 5969 最大的位或 【贪心】 (2016年中国大学生程序设计竞赛(合肥))

    最大的位或 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem De ...

随机推荐

  1. 每天一个linux命令(15)-tail

    tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不断刷新, ...

  2. Ubuntu日常使用总结

    Contents 使用了将近一年的Ubuntu,感觉不用windows也可以处理日常的事务.并且我相信只要合理利用Ubuntu,一定可以取代你手中的Windows.我不是说Ubuntu有多么好,只是从 ...

  3. 学习HEXO的历程

    前言: 简介 开始搭建 命令 API测试 逛github相关的帖子时,发现了hexo.正好想要做一个个人的博客,用来记录自己的各类感悟,所以花一些时间学习学习,以后博客可以放github,省得去注册c ...

  4. Android apk签名详解——AS签名、获取签名信息、系统签名、命令行签名

    Apk签名,每一个Android开发者都不陌生.它就是对我们的apk加了一个校验参数,防止apk被掉包.一开始做Android开发,就接触到了apk签名:后来在微信开放平台.高德地图等平台注册时,需要 ...

  5. 改了改之前那个很糙的XXX

    将就着用X度去爬吧 <?php echo "***************************************\r\n"; echo "* SubDom ...

  6. 从wordpress换hexo博客后

    之前用wordpress做blog, 为什么换为hexo呢? 第一 ​ wordpress的文章都保存在服务器的数据库, 维护不是很直观. ​ 而hexo是自己编写markdown文章,本地一份,而b ...

  7. 一文搞懂JVM内存结构+GC

    一.jvm是干什么的? 大家都知道java是跨平台语言,一次编译可以在不同操作系统上运行,怎么做到的呢,看下图: javac把写的源代码(java文件),编译成字节码(class文件),字节码部署到l ...

  8. LeetCode 题解 | 面试题 10.01. 合并排序的数组

    给定两个排序后的数组 A 和 B,其中 A 的末端有足够的缓冲空间容纳 B. 编写一个方法,将 B 合并入 A 并排序. 初始化 A 和 B 的元素数量分别为 m 和 n. 示例: 输入: A = [ ...

  9. 有点长的博客:Redis不是只有get set那么简单

    我以前还没接触Redis的时候,听到大数据组的小伙伴在讨论Redis,觉得这东西好高端,要是哪天我们组也可以使用下Redis就好了,好长一段时间后,我们项目中终于引入了Redis这个技术,我用了几下, ...

  10. Javascript学习笔记-基本概念-数据类型

    1.typeof 操作符的返回值: "undefined"——如果这个值未定义: "boolean"——如果这个值是布尔值: "string" ...