POJ 3104 Drying (神题 啊 ~) -- from lanshui_Yang
来源:互联网 发布:数据挖掘原理与应用 编辑:程序博客网 时间:2024/06/05 06:38
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 kthis 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 #132 3 95sample input #232 3 65
Sample Output
sample output #13sample output #22
题目大意不多说,主要说说 思想 。 刚开始做这道题时,就卡了好久,好不容易 想出办法 ,结果 提交 后 TLE ,哎,最后 还是 上网 搜了一下 大神们的 解题报告 ,才知道此题需用 二分 ,不得不感叹 大神们 的智慧啊 !! 以下说说本人的见解, 此题的思路是:二分 时间 ,假设 时间 为 t,那么含水量小于或等于 t 的衣服直接风干就行了,不需考虑,设含水量大于 t 的第i件衣服所带的 水量 为Ai ,需要用 吹风机的 次数 为 X ,用于 风干 的时间为 Y,吹风机一分钟 能够 吹干的水量为 k,则 可得到 以下两式 : X + Y = t ; X + Y * k >= Ai ; 由以上两式可得 : Y >= (Ai - t)/(k - 1) 算出所有含水量大于 t 的衣服 的 最小的 Y , 然后相加得到 sum ,再用 sum 与 t 比较 ,作为 二分中 上下界改变的条件 。具体用法的 详解 请看程序:#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;long long a[100005]; // 注意 : 此题 需用 long long ,用 int 会 WA !!int main(){ int n; scanf("%d",&n); int i; long long max = -1; for(i=1; i<=n; i++) { scanf("%lld",&a[i]); if(max < a[i]) // 找出 含水量的最大值 max = a[i]; } long long k ; scanf("%lld",&k); if (k == 1) //k == 1 的情况 需要单独判断 !!! { printf("%lld\n",max); } else { long long left = 1,right = max,mid; // 用含水量的最大值 作为 上边界 while (right > left) { mid = (left + right)/2; int j ; long long sum =0; for(j=1; j<=n; j++) { if(a[j] > mid) { long long tmp = ceil((a[j]-mid) * 1.0 / (k-1)); // ceil 是 向上取整 的函数 sum += tmp; } } if(sum > mid) // 此处 是 ">" , 不要写成 ">=" !! { left = mid + 1; } else { right = mid; // WA 了无数次 原来 是这里出错了,可千万不能写成 right = mid - 1 !!!! } } mid = ( right + left )/2; printf("%lld\n",mid); } return 0;}
- POJ 3104 Drying (神题 啊 ~) -- from lanshui_Yang
- POJ Chores - from lanshui_Yang
- POJ 3104 Drying(二分)
- poj 3104 Drying(二分)
- POJ 3104 Drying (二分)
- (二分法)【POJ-3104】Drying
- POJ 2080 Calender -- from lanshui_Yang
- POJ 1273 Drainage Ditches(EK) - from lanshui_Yang
- POJ 1135 Domino Effect (spfa + 枚举)- from lanshui_Yang
- POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang
- POJ 1251 Jungle Roads(最小生成树水题) - from lanshui_Yang
- POJ 2195 Going Home(KM)- from lanshui_Yang
- POJ 2400 Supervisor, Supervisee (KM + 回溯) - from lanshui_Yang
- POJ 1274 The Perfect Stall(二分匹配)- from lanshui_Yang
- POJ 1469 COURSES (二分匹配) - from lanshui_Yang
- POJ 3169 Layout(差分约束) - from lanshui_Yang
- POJ - 3104 Drying (二分搜索)
- POJ 3104 Drying (二分搜索)
- SAS接口完全互连指南(SATA到MINI SAS发展履历)
- pkm的linq to Entities学习1
- poj 1703 java
- 使用备份数据库.bal文件还原正在使用的数据库
- C++多线程编程简单实例
- POJ 3104 Drying (神题 啊 ~) -- from lanshui_Yang
- 苦逼的程序员发发牢骚
- 最小树形图(朱-刘算法)
- 获取手机通讯录联系人信息
- NSFetchedResultsSectionInfo 是什么对象
- C#注释的正确使用方法(Delphi与此类似)
- jquery异步调用页面后台方法
- Factory模式
- pb中打开帮助文件的若干种方法