[二分-最小化最大值] - poj3104-Drying

原创 2018年04月17日 21:30:08
Drying
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20853 Accepted: 5246

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

题意:

每件衣服都有一定单位水分,在不适用烘干器的情况下,每件衣服每分钟自然流失1个单位水分,

但如果使用了烘干机则每分钟流失K个单位水分,但是遗憾是只有1台烘干机,

每台烘干机同时只能烘干1件衣服,请问要想烘干N件衣服最少需要多长时间?

题解:

二分枚举时间,对与当前时间mid,遍历每一个ai,如果ai > mid 就代表需要用吹风机了,

而用吹风机每分钟扣k点水!注意:吹风机的效果不与自然流失的同时生效.

换句话说,吹风机的效果为k - 1

需要特判的是,当k=1时,吹风机的效果等同与自然流失,于是答案就是max(ai);

对于二分的两个边界来说,left 为 0,right为max(ai) (表示不需要用吹风机最多用的时间)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
ll arr[maxn],n,k,sum;
bool check(ll mid) {
    ll temp,cnt;
    for(int i=0,j=mid;i<n;i++) {
        temp = arr[i] - mid;
        if(temp > 0) {
            if(j) {
                cnt = temp / k;
                if(temp % k) cnt++;
                if(j >= cnt) j-=cnt;
                else return false;
            }
            else return false;
        }
    }
    return true;
}
int main()
{
    while(~scanf("%lld",&n)){
        ll left = 0,right = 0,sum = 0;
        for(int i=0;i<n;i++) scanf("%lld",&arr[i]), right = max(arr[i],right);
        scanf("%lld",&k);k--;
        if(k == 0){
            printf("%lld\n",right);
            continue;
        }
        while(left <= right) {
            ll mid = (left + right) >> 1;
            if(check(mid)) right = mid - 1;
            else left = mid + 1;
        }
        printf("%lld\n",left);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38013346/article/details/79981110

::细细品味ASP.NET (二)::

细细品味ASP.NET (二)青苹果工作室(编译) 01-5-17 下午 01:33:55Web表单 ASP.NET Web Forms就是Web页面,同现在你用 ASP编写代码所做的工作是一样的。...
  • coolstar
  • coolstar
  • 2001-05-23 13:07:00
  • 734

二分答案方法 — 最大值最小化问题

最大值最小化问题(分治解法) 把一个包含n个正整数的序列划分成m个连续的子序列(每个正整数恰好属于一个序列)。设i个序列的各数之和为S(i),你的任务是让所有S(i)的最大值尽量小 #...
  • u010258605
  • u010258605
  • 2015-01-31 10:03:38
  • 4279

POJ: Drying(二分、值最小化)

题目链接:POJ - 3104 题意:有n件湿衣服,分别给出其含水量。若衣服放在空气中,每分钟含水量会减少1;若把衣服放进烘干器里面则含水量每分钟会减少k,当衣服含水量为0时说明衣服已经干了,现...
  • lisong_jerry
  • lisong_jerry
  • 2017-07-26 22:20:10
  • 82

poj 3273 Monthly Expense (二分最小化最大值)

题目链接:http://poj.org/problem?id=3273
  • u013903504
  • u013903504
  • 2014-11-09 21:59:16
  • 504

[ACM] POJ 3258 River Hopscotch (二分,最大化最小值)

River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6697  ...
  • sr19930829
  • sr19930829
  • 2014-07-23 17:51:10
  • 1374

zzuli 1919 (二分 最大值最小化)

Description晴天想把一个包含n个整数的序列a分成连续的若干段,且和最大的一段的值最小,但他有强迫症,分的段数不能超过m段,然后他就不会分了。。。他想问你这个分出来的和最大的一段的和最小值是多...
  • xiaosshhaa
  • xiaosshhaa
  • 2016-09-06 13:21:06
  • 188

POJ---3273(二分,最小化最大值)

和这道题一样了: Description 聪哥在暑假参加了打零工的活动,这个活动分为n个工作日,每个工作日的工资为Vi。有m个结算工钱的时间,聪哥可以自由安排这些时间,也就是说什么时候拿钱...
  • sgh666666
  • sgh666666
  • 2018-02-04 01:17:17
  • 53

二分查找中的最大值的最小值问题

挑战程序设计2的 allocation#include #include using namespace std; int a[100001]; int k,n; int panduan(int m...
  • qq_36616023
  • qq_36616023
  • 2017-07-18 20:11:38
  • 345

POJ 3273 二分求最大化最小值

Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20086 Accep...
  • qq_22902423
  • qq_22902423
  • 2016-01-28 16:12:26
  • 2292

[ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14158 ...
  • sr19930829
  • sr19930829
  • 2014-07-23 16:05:24
  • 2311
收藏助手
不良信息举报
您举报文章:[二分-最小化最大值] - poj3104-Drying
举报原因:
原因补充:

(最多只允许输入30个字)