poj 3104 二分

本文探讨了在冬季衣物晾干过程中的效率提升方法,利用辐射加热器在有限条件下实现衣物快速干燥。通过分析每件衣物的初始含水量及其随时间减少的速度,提出了一种优化算法来确定使用加热器的最短总时间,从而有效缩短晾干所需时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Drying
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12568 Accepted: 3243

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
题目大意:有n件衣服需要晾干,每件含水量ai.每件衣服每分钟自然干1单位的水.每分钟可以对其中任意一件使用吹风机,其可以减少k的水.求晾干所有衣服的最少时间.
思路分析:数据量很大,如果贪心做肯定会超时,而且这道题目是被归到二分专题中的,我自然而然的就选择用二分做了,二分算法,弱表才刚刚起步,在没有学之前,我觉
得二分就是在一个有序的数列中进行查找的时候会遇到,但随着学习的深入,渐渐感觉到了自己原来认识的浅薄,二分,不仅仅是查找!二分,相当于给了很多问题求解的另一种
思路,就是枚举,寻找最优解,通过二分枚举,可以最快的确定答案。这种题目的一般思路:
1.看数据范围和题目要求(最小最大),找到题眼,选用二分法。
2.确定二分边界,比如本题所要确定的边界就是需要的时间,很显然最小为1,最大为a[n-1](sort排序后).
3.开始二分逼近,寻找正确答案,此时的重点是check函数的构造,比如本题,给你一个时间t,你如何判断这些衣服在t时间内能否晾干,这就是check函数你要解决的问题。
比如本题,对于任何一个t,遍历数组a,如果a[i]<t,继续就行,若a[i]>t,则有下列方程,设烘干时间x1,自由蒸发时间x2,a[i]<=k*x1+x2,t=x1+x2.
可以求得x1>=(a[i]-t)/(k-1),向上取整即可,向上取整有技巧,你可以原式+1,然后分子减一,即(a[i]-k+k-2)/(k-1).什么时候不满足呢?自然是需要烘干的时间
>t时。至此check函数构造完毕。
tip:k==1时需单独处理。
弱重复使用字母导致报错。。。。心塞。。调试了半天
代码:
#include<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
const int maxn=100000+100;
int a[maxn];
int n,k;
bool check(int t)
{
    int sum=0;
    if(k==1&&a[n-1]>t) return false;
    for(int i=0;i<n;i++)
    {
            if(a[i]<=t) continue;
            else
            {
            int b=(a[i]-t+k-2)/(k-1);
            sum+=b;
            if(sum>t) return false;
            }
    }
    return true;
}
int main()
{
    int ans;
   while(scanf("%d",&n)!=EOF)
   {
       for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
       sort(a,a+n);
       scanf("%d",&k);
       int l=1,r=a[n-1];
       while(l<=r)
       {
           int mid=(l+r)/2;
           if(check(mid)) ans=mid,r=mid-1;
           else l=mid+1;
       }
       printf("%d\n",ans);
   }
}

转载于:https://www.cnblogs.com/xuejianye/p/5506467.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值