题目: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. ansible使用指北(二)

    前言在上一篇文章里我们了解了ansible的常用模块,今天我们来了解下ansible-playbook,ansbile-playbook是一系统ansible命令的集合,其利用yaml 语言编写,an ...

  2. Selenium 实现自动下载文件(FirefoxOptions,FirefoxProfile) - 根据Selenium Webdriver3实战宝典

    Firefox 版本是72geckodriver 是 0.24selenium 是3.14 代码中注释有关于FirefoxOptions,FirefoxProfile的解释,请各位寻找一下,不做另外解 ...

  3. 成长日记(2) Java面向对象

    本篇主要是记录自己在学习路上的笔记,如果有哪里记错了请大家直接指出 面向对象的概念 *人为抽象的一种编程模型 *面向过程 代码集中 难以维护 *类:对事物 算法 逻辑 概念等的抽象 理解成 模板 图纸 ...

  4. Pycharm+PyQt5开发环境配置

    一.安装Python开发环境 python官网下载地址:https://www.python.org/downloads/ 注:千万不要使用最新测试版,很有可能第三方库不支持 笔者目前使用的版本是3. ...

  5. 正式学习MVC 01

    1.新建项目 点击创建新项目,选择ASP.NET web应用程序,对项目进行命名后点击创建. 截图如下: 取消勾选HTTPS配置 可选择空 + mvc 或直接选定MVC 2.目录结构分析 1) App ...

  6. DirectX11--深入理解Effects11、使用着色器反射机制(Shader Reflection)实现一个复杂Effects框架

    前言 如果之前你是跟随本教程系列学习的话,应该能够初步了解Effects11(现FX11)的实现机制,并且可以编写一个简易的特效管理框架,但是随着特效种类的增多,要管理的着色器.资源等也随之变多.如果 ...

  7. 解决Hexo博客模板hexo-theme-next的翻页按钮不正常显示问题

    用Hexo搭了个Gitpage的博客,兴冲冲的发了11篇博文后发现翻页按钮不正常显示,显示为<i class="fa fa-angle-right"></i> ...

  8. Java实现生产者消费者(一)

    问题描述:生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,生产者往存储空间中添加产品,消费者从存储空间中取走产品,当存储空间为空时,消费者阻塞,当存储空间满时 ...

  9. 如何将zTree选中节点传递给后台

    获取zTree选中节点 <body> <script type="text/javascript"> var setting = { view: { dbl ...

  10. 3DGIS+BIM集成与智慧城市应用

    ZTMap3D是基于网络的三维地理信息系统平台软件,利用 ZTMap3D能够实现三维地理信息和虚拟现实,是数字化地球和数字化城市建设的基础平台. BIM(building information mo ...