挑战程序设计竞赛3.1习题:Drying POJ - 3104
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了挑战程序设计竞赛3.1习题:Drying POJ - 3104,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3125字,纯文字阅读大概需要5分钟。
内容图文
It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.
Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.
There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.
Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).
The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.
Input
The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k ≤ 109).
Output
Output a single integer — the minimal possible number of minutes required to dry all clothes.
Sample Input
sample input #1 3 2 3 9 5 sample input #2 3 2 3 6 5
Sample Output
sample output #1 3 sample output #2 2
题目就是甩衣服,正常情况1s甩干1单位,烘干机1s甩干k单位,问就是二分(之前想过贪心,但是好像不行)
二分就要确定left和right,left肯定是0或者1都行(反正0也不可能),right是多少呢?答案是所有衣服都自然甩干(纯天然的干)的时间咯,你只要放到甩干机就至少不会慢过自然干,那也就是最大的水分咯,等到了最大的水分都自然干了,其他的早就自然干了。
然后就搜索,搜索什么呢?肯定是时间,那么怎么判断时间符不符合要求呢?我们直接按照搜索的时间(我们假设为mid)来分:小于等于搜索值mid的肯定在mid时间后干了,不需要甩干机,大于的就要了,要多少呢?(a[i] - mid + k - 2) / (k - 1)为什么是这个呢?我们看,经过了mid的时间的自然烘干,第i个只剩a[i] - mid的水分了,这些水分不能在自然干了(因为自然干时间用完了),只能烘干机,这里注意烘干机比自然甩多k-1每秒,因为我们实际上在0~mid秒才能甩,我们不妨分开两个动作,烘干过程看成自然甩每秒1水分,纯烘干每秒k-1水分,这样烘干的过程每秒还是干掉了k水分,但是我们就好计算了,我们之前的a[i] - mid就是甩的水分,现在计算纯烘干所需要的时间,就是(a[i] - mid) / (k - 1)向上取整,因为还剩有点水也要多1s烘干,我们就可以写成(a[i] - mid + k - 2) / (k - 1),这样我们就能算出如果mid内结束,需要多少秒能甩干,如果算出来的值大于mid,就矛盾了,所以不可能,就得加时间。
注意时间可能在中途计算的时候超过int的范围,虽然答案一定是在int之内的,但是由于计算假设mid完成,需要的时间可能因为mid取得较小值而使得结果超出int范围,虽然这个结果不要的,但是可能因为溢出变成了负数使得判断出现错误,得用longlong
以及当k==1时,k-1 = 0,会引发异常,得特判k==1
AC代码:
#include <cstdio> #include <algorithm> typedef long long ll; using namespace std; int a[100005]; int n, k; bool check(int mid) { ll num = 0;//注意num可能因为mid取小了而超过int for(int i = 0; i < n; i++) { if(a[i] <= mid) continue; else { num += (a[i] - mid + k - 2) / (k - 1); } } return num <= mid; } int main() { int left = 0, right = 0; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &a[i]); right = max(a[i], right); } scanf("%d", &k); if(k == 1)//当k==1就相当于全员甩干,就是最大的时间,也就是right的值 { left = right; } else { while(left < right) { int mid = (left + right) / 2; if (check(mid)) right = mid; else left = mid + 1; } } printf("%d\n", left); return 0; }
原文:https://www.cnblogs.com/jacobfun/p/12449049.html
内容总结
以上是互联网集市为您收集整理的挑战程序设计竞赛3.1习题:Drying POJ - 3104全部内容,希望文章能够帮你解决挑战程序设计竞赛3.1习题:Drying POJ - 3104所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。