其他分享
首页 > 其他分享> > poj-3104

poj-3104

作者:互联网

题目衔接:http://poj.org/problem?id=3104

Drying

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25541   Accepted: 6393

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 kwater, 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

题目大意:给你n件湿衣服,给出每件衣服的水量,再给你使用风干机每分钟能风干的水量,问你最少需要多长时间才能把说有衣服风干完

思路:二分时间,先找到最大的时间,这个不管怎么弄,最大的时间肯定不会超过这个值,然后在区间(1,max)内二分时间,判断每件衣服与当前假设时间的关系,判断如果用风干机风干需要的时间,最后判断这个时间是否正确。

代码:

/*
题目大意:给你n件湿衣服,给出每件衣服的水量,再给你使用风干机每分钟能风干的
水量,问你最少需要多长时间才能把说有衣服风干完
思路:二分
*/
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll unsigned long long
#define inf 0x3f3f3f
#define esp 1e-8
#define bug {printf("mmp\n");}
#define mm(a,b) memset(a,b,sizeof(a))
#define T() int test,q=1;scanf("%d",&test); while(test--)
const int maxn=1e6+100;
const double pi=acos(-1.0);
const int N=1100;
const int mod=1e9+7;
ll a[maxn],n,k;
bool ju(ll mid)
{
    ll tt=0;
    for(int i=0; i<n; i++)
    {
        if(a[i]>mid)
        {
            tt+=ceil((a[i]-mid)*1.0/(k-1));
        }
    }
    if(tt>mid)
        return false;
    else
        return true;
}
void b_serch(ll low ,ll high)
{
    ll mid=0,ans=0;
    while(low<=high)
    {

        mid=(low+high)/2;
        if(ju(mid))
        {
            high=mid-1;
            ans=mid;
        }
        else
            low=mid+1;
    }printf("%lld\n",ans);
}

int main()
{
    while(scanf("%lld",&n)!=EOF)
    {
        mm(a,0);
        ll m=0;
        for(int i=0; i<n; i++)
        {
            scanf("%lld",&a[i]);
            m=max(m,a[i]);

        }
        scanf("%lld",&k);
        if(k==1)
        {
            printf("%lld\n",m);
            continue;
        }
        b_serch(1,m);
    }
    return 0;
}

 

标签:dry,3104,ll,thing,poj,风干,include,define
来源: https://blog.csdn.net/lee371042/article/details/89489307