POJ 3104 Drying(烘凉衣服)__二分
来源:互联网 发布:数据防泄密 编辑:程序博客网 时间:2024/04/28 01:30
Description
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
分析
题目大意:现有 n 件衣服需要烘干,每件衣服的含水量为 ai。如果自然晾干, 每分钟含水量减少1,如果使用烘干机烘干, 每分钟含水量减少 k (直至为0),只有一台烘干机, 每次只能烘干一件衣服,且一次至少使用1分钟。求使所有衣服含水量为0的最少时间是多少?
通过二分法来逐步逼近答案,如答案为t, 含水量小于或等于t的直接可以自然晾干,不用管,关键是计算含水量大于t的。设这件衣服采用自然晾干的时间为x,烘干的时间为y,则可得:
t<=x+y
a[i]=x+y*k
两式相减可得:
y>=(a[i]-t)/(k-1),即t时间下这件衣服至于需要烘干ceil((a[i]-t)/(k-1))的时间。
实现
#include <iostream>#include <cstring>int a[100000 + 10];int n, k;bool canSolve(int t){ int sumTime = 0; //烘干机被使用的时间 for (int i = 0; i < n; i++) { if (a[i] > t) { //用求余取代ceil的功能,可以会快点。 sumTime += (a[i] - t) / (k - 1) + ((a[i] - t) % (k - 1) > 0); if (sumTime > t) return false; } } return true;}int main(){// freopen("in.txt", "r", stdin); int l = 0, r = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] > r) { r = a[i]; } } scanf("%d", &k); if (k == 1) { //跟自然晾干的掉水量一致,选择需要自然晾干的最长时间即可。 printf("%d\n", r); } else { while (l <= r) { int mid = (l + r) >> 1; canSolve(mid) ? (r = mid - 1) : (l = mid + 1); } printf("%d\n", l); } return 0;}
- POJ 3104 Drying(烘凉衣服)__二分
- POJ 3104 Drying 二分
- POJ--3104--Drying【二分】
- POJ 3104 Drying 二分
- poj 3104 Drying (二分)
- POJ 3104 Drying (二分)
- POJ 3104 Drying (二分)
- POJ 3104- Drying-二分
- POJ 3104 Drying(二分)
- POJ - 3104 Drying(二分)
- POJ 3104 Drying【二分】
- POJ 3104 Drying(二分枚举)
- POJ 3104 Drying 二分搜索
- POJ 3104 - Drying(二分搜索)
- POJ 3104 Drying (二分+精度)
- POJ 3104 Drying-二分答案
- POj-3104 Drying 二分+贪心
- POJ 3104 Drying(二分)
- /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found
- 队列同步器
- js实现闹钟代码
- 程序设计实训一 小学生考试系统
- 大数相减
- POJ 3104 Drying(烘凉衣服)__二分
- LeetCode113. Path Sum II
- C语言怎么改变窗口的字体颜色和背景颜色?
- Android笔记#利用第三方播放器vitamio实现悬浮播放效果
- DTN-ION协议栈结构介绍
- VM虚拟机下安装Centos7.0图文教程
- 使用WindowManager.addView()模拟直播软件悬浮窗口播放视频
- 操作系统 银行家算法模拟(二)
- 物理层