题目网站:
http://acm.hdu.edu.cn/showproblem.php?pid=6709

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 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≤1e5),k(1≤k≤1e9)
, 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条鱼要捉来煮,每次捉鱼都需要k分钟,但每条鱼要煮t分钟,求出最快煮完所有鱼的时间。
这道题一开始直接让我跟队友自闭,一开始的思路是把煮时间长的鱼先钓上来,而且!一开始找出的样例都证明可行并且敲了出来,然后wa(-5)。后来出了个反例推翻了。在最后8分钟想出了ac思路,就是把每条鱼要煮的时间,如果大于k,就再煮的时候捉t[i]/k条鱼,剩下的时间再就到数组。这样我们就可以得出一个时间全小于k的数组(如果鱼还没有钓完,而如果鱼已经掉完,接下来安心煮鱼就好)。此时无论有多少条鱼没钓没煮,都必须在每条鱼煮的时候钓一条鱼,直到所有鱼钓完。所以此时把得到的数组从大到小排序,有多少条鱼没钓,sum就加多少个k,再加上剩下的鱼要煮的时间,即可得出答案。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll sum ;
int num , t , n , k , a[100005] ;
int cmp(int a,int b){return a>b;}
int main () {
 ios::sync_with_stdio(false);
 cin >> t ;
 while (t--){
  cin >> n >> k ;
  num = 1 ;  //先捉一条鱼上来煮
  sum = k ;  //第一条鱼的时间
  for (int i = 1 ; i <= n ;  i++){
   cin>>a[i];
   if (num < n){
    int x = a[i] / k ; //当前煮鱼时间最多可以完整得捉多少鱼
    if (num + x >= n){
     a[i] = a[i] - (n - num) * k ;
     sum = sum + (n - num) * k ;
     num = n ;
    }else{
     sum = sum + x * k ;
     a[i] = a[i] % k ;
     num = num + x ;
    }
   }
  }
  sort(a+1,a+1+n,cmp);  //此时数组里的数都是小于K的,所以先算捉鱼时间,捉完鱼但没煮的就安心煮鱼好了
  for(int i = 1 ; i <= n ; i++){
   if(num<n){
    sum = sum + k ;
    num++;
   }else{
    sum = sum + a[i];
   }
  }
  cout<<sum<<endl;
 }
 return 0;
}

Fishing Master (思维+贪心)的更多相关文章

  1. HDU 6709“Fishing Master”(贪心+优先级队列)

    传送门 •参考资料 [1]:2019CCPC网络选拔赛 H.Fishing Master(思维+贪心) •题意 池塘里有 n 条鱼,捕捉一条鱼需要花费固定的 k 时间: 你有一个锅,每次只能煮一条鱼, ...

  2. [贪心,dp] 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 Fishing Master (Problem - 6709)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6709 Fishing Master Time Limit: 2000/1000 MS (Java/Othe ...

  3. 2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest- H. Rikka with A Long Colour Palette -思维+贪心

    2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest- H. Rikka with A Long Colour Palette -思维+贪心 [Proble ...

  4. E. The Contest ( 简单DP || 思维 + 贪心)

    传送门 题意: 有 n 个数 (1 ~ n) 分给了三个人 a, b, c: 其中 a 有 k1 个, b 有 k2 个, c 有 k3 个. 现在问最少需要多少操作,使得 a 中所有数 是 1 ~ ...

  5. HDU-6709 Fishing Master

    Description Heard that eom is a fishing MASTER, you want to acknowledge him as your mentor. As every ...

  6. Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】

    B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...

  7. Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找

    The link to problem:Problem - D - Codeforces   D. Range and Partition  time limit per test: 2 second ...

  8. 【Fishing Master HDU - 6709 】【贪心】

    题意分析 题意:题目给出n条鱼,以及捕一条鱼所用的时间k,并给出煮每一条鱼的时间,问抓完并煮完所有鱼的最短时间. 附题目链接 思路: 1.捕第一条鱼的时间是不可避免的,煮每条鱼的时间也是不可避免的,这 ...

  9. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

随机推荐

  1. hw笔试题-01

    #include <stdlib.h> #include <stdio.h> #include <string.h> int str_split(char *inp ...

  2. CSP-S 2019 AFO记

    DAY -1 上午并没有改出题,然而调出了动态$dp$,于是顺便$AC$了保卫王国,于是就很愉悦. 下午考前自闭赛,把会的题和原题写了出来,然后就$rank1$了,感觉自己$rp--$. 晚上发现$T ...

  3. java1.8 AQS AbstractQueuedSynchronizer学习

    AQS concurrent并发包中非常重要的顶层锁类,往往用的比较多的是ReentrantLock,然而ReentrantLock的实现依赖AbstractQueuedSynchronizer在到上 ...

  4. 使用Node.js时如何引入jQuery

    使用Node.js时如何引入jQuery 首先安装jQuery依赖 npm install jquery 然后安装jsdom npm install jsdom 引入jQuery 新版正确的依赖方式 ...

  5. 从零到一手写基于Redis的分布式锁框架

    1.分布式锁缘由 学习编程初期,我们做的诸如教务系统.成绩管理系统大多是单机架构,单机架构在处理并发的问题上一般是依赖于JDK内置的并发编程类库,如synchronize关键字.Lock类等.随着业务 ...

  6. sklearn 学习 第一篇:分类

    分类属于监督学习算法,是指根据已有的数据和标签(分类)进行学习,预测未知数据的标签.分类问题的目标是预测数据的类别标签(class label),可以把分类问题划分为二分类和多分类问题.二分类是指在两 ...

  7. 练手WPF(四)——贪吃蛇小游戏的简易实现(下)

    八.生成新的单节蛇身我们这里先说说游戏小原理好了,游戏运行后,通过计时器事件不断生成新的单节蛇身类SnakeNode,添加到List中的0位置,原来的蛇头变成了第二节.该节新蛇头的坐标通过蛇头前进方向 ...

  8. 读取txt文件内容,并按一定长度分页显示

    private List<string> SaveContentUpload(FileUpload file) { List<string> list_content = ne ...

  9. VS Code 自动修改和保存 代码风格 == eslint+prettier

    最近因为用到VS Code,需要统一所有人的代码风格(前端语言js/html/css等,或者后端语言 go/python等也可以这么用). 所以参考了一些网络资料,记录下设置步骤,以便后续查阅. St ...

  10. .NET Core 收徒,有缘者,可破瓶颈

    最近感悟天命,偶有所得,故而打算收徒若干,以继吾之传承. 有缘者,可破瓶颈,职场巅峰指日可待. 入门基本要求: 1.工作经验:1年或以上. 2.拜师费用:3999元(RMB). 传承说明: 1.收徒人 ...