POJ3104

来源:互联网 发布:linux系统磁盘分区 编辑:程序博客网 时间:2024/04/29 18:25

Drying

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18301 Accepted: 4615
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 #1
3
2 3 9
5

sample input #2
3
2 3 6
5
Sample Output

sample output #1
3

sample output #2
2
Source

Northeastern Europe 2005, Northern Subregion

#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>using namespace std;long long num[200000];int main(){    long long n,k;    long long maxn=0;    while(scanf("%lld",&n)!=EOF)    {            for(long long i=1;i<=n;i++)            {                scanf("%lld",&num[i]);                maxn=max(maxn,num[i]);            }            sort(num+1,num+1+n);            scanf("%lld",&k);            if(k==1)            {                printf("%lld\n",maxn);                continue;            }            long long l=1,r=maxn,mid=(r+l)/2;            long long ans=0;            while(l<r)            {                //mid=(l+r)/2;                long long sum=0;                for(long long i=1;i<=n;i++)                {                    if(num[i]>mid)                        sum+=ceil((num[i]-mid)*1.0/(k-1));                }                if(sum>mid)                    l=mid+1;                else if(sum<=mid)                {                    r=mid;                }                mid=(l+r)/2;            }            printf("%lld\n",mid);    }}

个人错误:
洗衣服的策略是什么10w肯定不能枚举啊
策略给出来了时间自然就知道啦
这个策略很难找
如果枚举时间~那么小于等于时间的全部gg
判断剩下的能不能模拟搞定判断满足要求
转换成可行性判定问题、、、
但刚开始代码写成强行模拟洗完一件最短的接着洗另一件直到结束,但是这不是最优的。可以洗一部分在晾干一部分。列方程解不等式。不存在重复,因为是可间断的。

原创粉丝点击