POJ3104 Drying

来源:互联网 发布:网页页面设计软件 编辑:程序博客网 时间:2024/03/29 17:48

原题连接
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

这道题的意思就是:有n件湿衣服,分别含有的水分为a[i],有一个烘干机,一次只能烘一件衣服,每分钟可以使水分减少k,而自然晾晒的衣服每分钟水分减少1,求使全部衣服变干的最少时间。
首先,最少的时间为1,最多的时间为m=max(a[i])。使用二分搜素。
开始berore = 0; later = m
接下来让mid = (before + later)/2
但是还有一个问题,就是如何判断mid是否满足条件??
  (o゜▽゜) o☆
对于第i件衣服,
如果,a[i] <= mid,直接自然风干就好;
如果,a[i] > mid,则先用烘X分钟,另外的mid-X的时间自然风干就好。那么,这个X等于多少呢?列方程得~~kX+(mid-X)>=a[i]
<( ̄︶ ̄)> 这样X就出来了,X>=(a[i]-mid)/(k-1),由于X为整数,所以要向上取整,于是X = (a[i] -mid +k-2)/(k-1);这就是第i件衣服所需烘干的时间
然后把每件衣服的最少烘干时间加起来,与mid比较:小于等于mid则表明mid时间可行,把mid赋值给later;大于mid则矛盾,mid时间内不可行,把mid赋值给before。

这里写图片描述

就是这样滴~~,奉上代码如下.    d=====( ̄▽ ̄*)b

 
#include <iostream>
#include <cstdio>
#include <algorithm>
typedef long long ll;
using namespace std;
int a[100005];
int k, ans;
int i, before, later, mid;
int n;
bool solve(int x)
{
int i, sum = 0;
for (i = 0; i < n; i++)
{
if (a[i] > x)
{
sum += (a[i]-x+k-2)/(k-1);
if (sum > x)
return false;
}
}
return true;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt", "r", stdin);
#endif
while(~scanf("%d", &n))
{
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
cin >> k;
sort(a, a+n);
if (k == 1)
{
cout << a[n - 1] << endl;
continue;
}
before = 1;
later = a[n - 1];
while(before <= later)
{
mid = (before + later ) / 2;
if (solve(mid))
{
ans = mid;
later = mid - 1;
}
else
{
before = mid + 1;
}
}
cout << ans << endl;
}
return 0;
}

0 0
原创粉丝点击