poj 3104 Drying 二分搜索--查找最小yes值

来源:互联网 发布:i5处理器编程够用么 编辑:程序博客网 时间:2024/06/01 10:16
Drying
Time Limit: 2000MSMemory Limit: 65536KTotal Submissions: 11665Accepted: 3011

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 #132 3 95sample input #232 3 65

Sample Output

sample output #13sample output #22

Source

Northeastern Europe 2005, Northern Subregion
错因:这道题题意是神坑,用烘干器的时候竟然就不能自然烘干了,poj 题目真是迷,只有强行裂解一下了。
分析:典型的二分法,属于查找最小的yes类型

#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
LL a[100005],b[100005];
LL n,k;
int ok(LL mid)
{
LL cnt=0;
for(int i=1;i<=n;i++)
{
if(a[i]<mid)
continue; //!!这个地方很容易错啊,假如没有这个判断的话的当a[i]<《mid时,cnt是会减少的。
cnt+=(a[i]-mid+k-2)/(k-1);//向上取整,或者ceil
}
return cnt<=mid?1:0;
}
int main()
{
while(~scanf("%lld",&n))
{
LL l=0,r=0,mid;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
if(a[i]>r)
r=a[i];
}
scanf("%lld",&k);
if(k==1)
{
printf("%d\n",r);
continue;
} //因为后面用到了/(k-1),所以需要特别判断下k==1的情况
while(l<r)
{
mid=l+(r-l)/2;
if(ok(mid))
r=mid;
else
l=mid+1;
}
printf("%lld\n",r);
}
return 0;
}



原创粉丝点击