Drying (二分法)

来源:互联网 发布:d3.js官网demo 编辑:程序博客网 时间:2024/06/06 19:41

Drying

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 52   Accepted Submission(s) : 14
Problem 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 byk 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
<span lang="en-us"><p>The first line contains a single integer <i>n</i> (1 ≤ <i>n</i> ≤ 100 000). The second line contains <i>a<sub>i</sub></i> separated by spaces (1 ≤ <i>a<sub>i</sub></i> ≤ 10<sup>9</sup>). The third line contains <i>k</i> (1 ≤ <i>k</i> ≤ 10<sup>9</sup>).</p></span>
 

Output
<p>Output a single integer — the minimal possible number of minutes required to dry all clothes.</p>
 

Sample Input
<b>sample input #1</b>32 3 95<b>sample input #2</b>32 3 65
 

Sample Output
<b>sample output #1</b>3<b>sample output #2</b>2
 

Source
PKU
 

题意:

有一些衣服,每件衣服都有一定水量,有一个暖气片,每次可以烘掉k滴水,每件衣服每分钟可自动蒸发一滴水,用暖气片烘干时不掉。问最少可以多长时间烘干所有衣服。

思路:

首先想到二分枚举最小的时间,那么就要找衣服水量比最小时间还要大的衣服,则最短时间是蒸发一段时间然后烘干一段时间,在这里,假设需要x分钟机器,那么自然风干就需要(mid-x)分钟,x和mid需要满足:

k*x+(mid-x)>=a[i];那么每件衣服最小需要x>=(a[i]-mid)/(k-1);

注意当k==1时要特殊处理,所需时间即为水量最大的那个衣服。用cin会超时。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#include <iomanip>#define maxn 100005#define mod 1000000007#define INF 0x3f3f3f3f#define exp 1e-6#define pi acos(-1.0)using namespace std;long long int a[maxn],maxx,sum,k;int main(){    //ios::sync_with_stdio(false);    int n,i;    long long low,high,mid;    while(scanf("%d",&n)!=EOF)    {        memset(a,0,sizeof(a));        maxx=0;        for(i=0;i<n;i++){                scanf("%lld",&a[i]);        if(a[i]>maxx)  maxx=a[i];        }        scanf("%d",&k);        if(k==1) {printf("%lld\n",maxx); continue;}        low=1,high=maxx;        long long res=0;        while(high>=low)        {            mid=(high+low)/2,sum=0;            for(i=0;i<n;i++)            {                if(a[i]>mid){long long s=ceil((a[i]-mid)*1.0/(k-1));sum+=s;}            }            if(sum<=mid)            {                res=mid;                high=mid-1;            }            else                low=mid+1;        }        printf("%lld\n",res);    }    return 0;}