2499: 【普及/提高-】【P1873】EKO / 砍树

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:28 解决:11

题目描述

伐木工人 Mirko 需要砍 lns="http://www.w3.org/1998/Math/MathML"> 米长的木材。对 Mirko 来说这是很简单的工作,因为他有一个漂亮的新伐木机,可以如野火一般砍伐森林。不过,Mirko 只被允许砍伐一排树。

Mirko 的伐木机工作流程如下:Mirko 设置一个高度参数 lns="http://www.w3.org/1998/Math/MathML">(米),伐木机升起一个巨大的锯片到高度 lns="http://www.w3.org/1998/Math/MathML">,并锯掉所有树比 lns="http://www.w3.org/1998/Math/MathML"> 高的部分(当然,树木不高于 lns="http://www.w3.org/1998/Math/MathML"> 米的部分保持不变)。Mirko 就得到树木被锯下的部分。例如,如果一排树的高度分别为 lns="http://www.w3.org/1998/Math/MathML">20,15,10 和 lns="http://www.w3.org/1998/Math/MathML">17,Mirko 把锯片升到 lns="http://www.w3.org/1998/Math/MathML">15 米的高度,切割后树木剩下的高度将是 lns="http://www.w3.org/1998/Math/MathML">15,15,10 和 lns="http://www.w3.org/1998/Math/MathML">15,而 Mirko 将从第 lns="http://www.w3.org/1998/Math/MathML">1 棵树得到 lns="http://www.w3.org/1998/Math/MathML">5 米,从第 lns="http://www.w3.org/1998/Math/MathML">4 棵树得到 lns="http://www.w3.org/1998/Math/MathML">2 米,共得到 lns="http://www.w3.org/1998/Math/MathML">7 米木材。

Mirko 非常关注生态保护,所以他不会砍掉过多的木材。这也是他尽可能高地设定伐木机锯片的原因。请帮助 Mirko 找到伐木机锯片的最大的整数高度 lns="http://www.w3.org/1998/Math/MathML">,使得他能得到的木材至少为 lns="http://www.w3.org/1998/Math/MathML"> 米。换句话说,如果再升高 lns="http://www.w3.org/1998/Math/MathML">1 米,他将得不到 lns="http://www.w3.org/1998/Math/MathML"> 米木材。

输入

第 lns="http://www.w3.org/1998/Math/MathML">1 行 lns="http://www.w3.org/1998/Math/MathML">2 个整数 lns="http://www.w3.org/1998/Math/MathML"> 和 lns="http://www.w3.org/1998/Math/MathML">lns="http://www.w3.org/1998/Math/MathML"> 表示树木的数量,lns="http://www.w3.org/1998/Math/MathML"> 表示需要的木材总长度。

第 lns="http://www.w3.org/1998/Math/MathML">2 行 lns="http://www.w3.org/1998/Math/MathML"> 个整数表示每棵树的高度。

输出

 个整数,表示锯片的最高高度。

样例输入 复制

4 7
20 15 10 17

样例输出 复制

15

提示

对于 lns="http://www.w3.org/1998/Math/MathML">100% 的测试数据,lns="http://www.w3.org/1998/Math/MathML">1106lns="http://www.w3.org/1998/Math/MathML">12×109,树的高度 lns="http://www.w3.org/1998/Math/MathML"><109,所有树的高度总和 lns="http://www.w3.org/1998/Math/MathML">>


#include<bits/stdc++.h>
using namespace std;
#define maxn 1000010
typedef long long LL;
LL a[maxn],n,m;
bool P(int h) { // 当砍树高度为h时能否得到大于m的木材
    LL tot = 0;
    for (int i = 1; i <= n; i++)
        if (a[i] > h)
            tot += a[i] - h; // 按照题意模拟。
    return tot >= m;
}
int main(){
    scanf("%lld%lld",&n,&m);
    for (int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    int L=0,R=1e9,ans,mid;
    while(L<=R)
        if (P(mid=L+R>>1))  //一种压行技巧
		    ans=mid,L=mid+1;
	//如果P(mid)为真,mid可以成为答案,真正的答案可能在mid右侧,左端点右移			 
		else 
		    R=mid-1;  //P(mid)为假,答案在mid左侧,右端点左移 
    printf("%d",ans);
	return 0;
}