题目: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. 杂记:VMware中为mac虚拟机扩容

    之前在VMware中安装Mac虚拟机时,硬盘选的是默认的40G,后来用的过程中随着软件的安装,特别是安装完Xcode和QT5.9之后,可用空间只剩不到3G,每次开机之后都会提醒空间不足,需要清理空间, ...

  2. 软件测试价值观-SMBT新理念

    软件测试价值观-SMBT新理念 作者:张元礼 http://blog.csdn.net/vincetest 近年来有不少软件测试同行不少有些困惑-软件测试人员的价值在哪里?我们怎么才能做好软件测试?怎 ...

  3. 量化投资学习笔记37——《Python机器学习应用》课程笔记10

    用KNN算法来进行数字识别,还是用sklearn自带的digits数据集. coding:utf-8 KNN算法实现手写识别 from sklearn import neighbors from sk ...

  4. css手写一个表头固定

    Bootstrap,layui等前端框架里面都对表头固定,表格滚动有实现,偏偏刚入职的公司选择了手动渲染表格,后期又觉得表格数据拉太长想要做表头固定.为了避免对代码改动太大,所以决定手写表头固定 主要 ...

  5. 简单谈谈HashMap

    概述 面试Java基础,HashMap可以说是一个绕不过去的基础容器,哪怕其他容器都不问,HashMap也是不能不问的. 除了HashMap,还有HashTable跟ConcurrentHashMap ...

  6. ELK springboot日志收集

    一.安装elasticsearch 可以查看前篇博客 elasticsearch安装.elasticsearch-head 安装 二.安装 配置 logstash 1.安装logstash 下载地址: ...

  7. String字符串位置移动

    有规律的String字符串位置移动 1.自定义一个有规律的String字符串 String numstr = "1,2,3,x,y,4,5"; 2.按逗号拆分numstr字符串 S ...

  8. 调用系统的loading界面

    //在状态栏显示一个圈圈转动  代表正在请求 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

  9. 图像IO

    图像IO 潜伏期值得思考 - 凯文 帕萨特 在第13章“高效绘图”中,我们研究了和Core Graphics绘图相关的性能问题,以及如何修复.和绘图性能相关紧密相关的是图像性能.在这一章中,我们将研究 ...

  10. ubunto 免输入密码 登录 putty ssh-keygen

    交互式密码不安全,现在改用 ssh 证书方式,不用输入密码使用公钥证书登录. 方法1, 此方法,仅试用于,仅使用win putty 来连接方式使用,如果双方都是 linux 如 rsync 同步等时, ...