Poj 3104 (Binary)

Source: Internet
Author: User
Drying
Time Limit:2000 MS   Memory Limit:65536 K
Total Submissions:7104   Accepted:1823

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
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 areNClothes Jane has just already ed. Each of them tookAiWater during processing. 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 decreasesKThis minute (but not less than zero-if the thing contains less
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 implements tively. The drying process ends when all the clothes are dry.

Input

The first line contains a single integerN(1 ≤N≤ 100 000). The second line containsAiSeparated by spaces (1 ≤
Ai≤ 109). The third line containsK(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

Greedy simulation will time out. The key here is to think of binary. Then pay attention to several details. 1. Note that the value may exceed int. 2. During each drying process, there is no natural dryness. 3 when k is 1. 4 a small technique, integer calculation, a/B on the integer can be used (a + b-1)/B.

#include<cstdio>#include<algorithm>#include<vector>#include<set>#include<map>#include<iostream>#include<string>#include<cstring>#include<queue>#include<cmath>using namespace std;typedef long long LL;const int maxn = 100000 + 5;const int INF = 1000000000;int a[maxn];int n,k;bool can(int x){    int cnt = 0;    for(int i = 0;i < n;i++){        if(a[i] > x){            cnt += (a[i] - x + k - 2) / (k - 1);            if(cnt > x) return false;        }    }    return true;}int main(){    while(scanf("%d",&n) != EOF){        for(int i = 0;i < n;i++) scanf("%d",&a[i]);        sort(a,a+n);        scanf("%d",&k);        if(k == 1){            printf("%d\n",a[n-1]);            continue;        }        int l = 1,r = a[n-1];        int ans;        while(l <= r){            int mid = l+(r-l)/2;            if(can(mid)){                ans = mid;                r = mid-1;            }            else l = mid+1;        }        printf("%d\n",ans);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.