POJ 3104-Drying(二分+贪心)
2016-04-19 22:16
465 查看
C - DryingTime Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d& %I64uSubmit Status Practice POJ3104DescriptionIt 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 ata 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 amountof 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 amountof 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.InputThe 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 containsk (1 ≤ k ≤ 109).OutputOutput 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 5Sample Output
sample output #1
3
sample output #2
2
题意:
n件衣服各含a_i水分,自然干一分钟一单位,放烘干机一分钟k单位,一次只能晒一件。求最短时间。
思路:
这题有一个坑点,那就是用烘干机就不会自然干。而这个贪心的公式也是很容易求出来的。那就是每一件衣服一共要用烘干机多少次,设它为X次,即(mid(二分的天数)-X)+X*(一天的烘干值)〉= 当前衣服的水分就能求出答案了。
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<map>#include<set>using namespace std;typedef long long ll;#define T 100000+50#define inf 0x3f3f3f3fll N,M;ll a[T];bool slove(ll x){ll cur = 0,j,tmp;for(int i=0;i<N;++i){if(a[i]<=x)continue;tmp = (a[i]-x)/(M-1);while((M-1)*tmp+x<a[i])tmp++;cur += tmp;if(cur>x)return false;}return true;}int main(){#ifdef zscfreopen("input.txt","r",stdin);#endifint i,j,k;while(~scanf("%lld",&N)){ll L=1,R=1000000000,mid,ans,ma=0;for(i=0;i<N;++i)scanf("%lld",&a[i]),ma=max(a[i],ma);scanf("%lld",&M);if(M==1){printf("%lld\n",ma);continue;}while(L<=R){mid = (L+R)/2;if(slove(mid)){ans = mid;R = mid-1;}else {L = mid+1;}}printf("%lld\n",ans);}return 0;}
相关文章推荐
- C++二分查找在搜索引擎多文档求交的应用分析
- C语言编程中实现二分查找的简单入门实例
- C#二分查找算法实例分析
- 二分查找算法在C/C++程序中的应用示例
- 在MySQL中实现二分查找的详细教程
- Java实现二分查找算法实例分析
- Python二分查找详解
- 简介二分查找算法与相关的Python实现示例
- 漫谈递归:二分查找算法的递归实现
- 1.一些 贪心算法 的简单思维题:
- 二分查找
- 贪心算法——字典序最小问题
- 贪心算法——区间调度问题
- [LeetCode] Find Minimum in Rotated Sorted Array
- 折半查找法
- "二分查找(Binary Search)"与"斐波那契查找(Fibonacci Search)"
- 初学ACM - 组合数学基础题目PKU 1833
- 二分查找
- C#版二分查找(代碼)
- POJ ACM 1001