[贪心,dp] 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 Fishing Master (Problem - 6709)
题目: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
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?
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.
3 5
5 5 8
2 4
3 3
11
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条鱼,第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)的更多相关文章
- [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 ...
- 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛(8/11)
$$2019中国大学生程序设计竞赛(CCPC)\ -\ 网络选拔赛$$ \(A.\hat{} \& \hat{}\) 签到,只把AB都有的位给异或掉 //#pragma comment(lin ...
- HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛
普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...
- 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛
传送门 A.^&^ 题意: 找到最小的正数\(C\),满足\((A\ xor\ C)\&(B\ xor \ C)\)最小. 思路: 输出\(A\&B\)即可,特判答案为0的情况 ...
- 【赛后总结+部分题解】2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛
赛后总结: T:今天状态一般,甚至有点疲惫.然后12点比赛开始,和队友开始看题,从最后往前面看,发现数学题公式看不懂.然后发现队友已经双开做1001和1006了,我看着1007有人A,开始做1007. ...
- 2019中国大学生程序设计竞赛-女生专场(重现赛)部分题解C-Function(贪心+优先队列) H-clock(模拟)
Function 题目链接 Problem Description wls 有 n 个二次函数 Fi(x) = aix2 + bix + ci (1 ≤ i ≤ n). 现在他想在∑ni=1xi = ...
- 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 A题
A - ^&^ Bit operation is a common computing method in computer science ,Now we have two positive ...
- 【2019中国大学生程序设计竞赛-女生专场】C - Function
原题 韦神提供的思路orz 首先一个显然的性质,所有的c可以提出来,方程变成ax^2+bx的形式 因为x的值是离散的,而m的值又不大 所以一开始让x都为1(注意!x是正整数),然后每次挑一个x让他加一 ...
- HDU 5969 最大的位或 【贪心】 (2016年中国大学生程序设计竞赛(合肥))
最大的位或 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem De ...
随机推荐
- FPGA小白学习之路(2)error:buffers of the same direction cannot be placed in series
锁相环PLL默认输入前端有个IBUFG单元,在输出端有个BUFG单元,而两个BUFG(IBUFG)不能相连,所以会报这样的错: ERROR:NgdBuild:770 - IBUFG 'u_pll0/c ...
- Sublime Text3 旧版本下载以及破解激活方式
前言 当前Sublime Text3 出到了32**版本,以前直接输入激活码的方法已经不能使用. 而官网又不提供旧版本的下载链接,因此在此分享旧版本下载方式以及激活方式. 下载方法 通过下面这个链接下 ...
- Blind Estimation and Detection of Space-Time Trellis Coded Transmissions over the Rayleigh Fading MIMO Channel
目录 文章来源 摘要 基本概念 粒子滤波 时间序列模型 系统模型 通信系统 经典状态空间表示 论文所提出的状态空间表示 借鉴之处 文章来源 IEEE TRANSACTIONS ON COMMUNICA ...
- unittest实战(二):用例编写
# coding:utf-8import unittestfrom selenium import webdriverimport timefrom ddt import ddt, data, unp ...
- .Net Core调用oracle存储过程
一 前言 实战踩坑系列,调用第三方Oracle存储,各种血泪史,现记录如下. 二 入坑 首先,调用Oracle需要安装客户端驱动才行,但是在程序开发中下载客户端驱动是一个不明智的选择.于是,不管是微软 ...
- Electron打包H5网页为桌面运行程序
一.安装配置环境 Electron(一种桌面应用程序运行时),Electron 把 Chromium 和 Node 合并到一个单独的运行时里面,很适合开发桌面 web 形式的应用程序,通过Node它提 ...
- 超详细的HDFS读写流程详解(最容易理解的方式)
HDFS采用的是master/slaves这种主从的结构模型管理数据,这种结构模型主要由四个部分组成,分别是Client(客户端).Namenode(名称节点).Datanode(数据节点)和Seco ...
- 负载均衡框架 ribbon 二
Ribbon 负载均衡机制 官方文档地址:https://github.com/Netflix/ribbon/wiki/Working-with-load-balancers 1. Ribbon 内置 ...
- iview的render函数使用
render渲染函数详解 https://www.cnblogs.com/weichen913/p/9676210.html iview表格的render函数作用是自定义渲染当前列,权限高于key,所 ...
- Educational Codeforces Round 83 (Rated for Div. 2)A--C
题意:给出一个边数为n的等边多边形,问是否可以变成m的等边多边形.条件是同一个中心,共用原顶点. 解析:直接n%m==0即可,这样就是平分了.签到题没得说了. #include<iostream ...