码迷,mamicode.com
首页 > 其他好文 > 详细

P1873 砍树

时间:2020-06-24 21:58:12      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:int   color   include   als   ble   turn   lse   clu   最大值   

https://www.luogu.com.cn/problem/P1873

#include<bits/stdc++.h>
using namespace std;
int n, m, a[1000005], max_h=-1;

bool check(int h){
	long long sum=0;
	for(int i=n-1; i>=0; i--){
		if(h-a[i]<0)
			sum+=a[i]-h;
		else //树木高度有序,比h低的树,没有必要继续累加 
			break;
	}
	if(sum>=m)
		return true;
	else
		return false;
}
int main()
{
	cin>>n>>m;
	for(int i=0; i<n; i++){
		cin>>a[i];
		max_h=max(max_h, a[i]);
	}
		
	sort(a, a+n);
	int l=1, r=max_h;//求出ans的最大值 
	int ans=-1;
	while(l<=r){
		int mid=r-(r-l)/2;
		if(check(mid)){
			ans=mid;
			l=mid+1;
		}
		else 
			r=mid-1;
	}
	cout<<ans;
	return 0;
}

  

P1873 砍树

标签:int   color   include   als   ble   turn   lse   clu   最大值   

原文地址:https://www.cnblogs.com/tflsnoi/p/13189934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!