用二分答案,怎么才有40........
查看原帖
用二分答案,怎么才有40........
312780
456laji楼主2020/5/27 22:45

请求大佬帮我看看,谢谢, 代码丑陋,见谅。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x7f7f7f7f;
const int N=1e7+10;
ll num[N];

ll cheak(ll x,ll len)
{
	ll ans=0;
	for(int i=1;i<=len;i++)
	{
		if(num[i]>x)
		{
			ans+=(num[i]-x);
		}
	}
	return ans;
}

int main()
{
	std::ios::sync_with_stdio(0);
	cin.tie(0);
	
	
	ll m=-INF, n,sum;
	cin>>n>>sum;
	for(int i=1;i<=n;i++)
	{
		cin>>num[i];
		m=max(m,num[i]);
	}
	
	ll L=1,R=m,temp;
	while(L<=R)
	{
		ll mid=(L+R)>>1,temp=cheak(mid,m);
		if(temp>=sum) L=mid+1;
		else R=mid-1;
	}
	
	printf("%lld\n",R);
	return 0;
}
2020/5/27 22:45
加载中...