题目描述:
思路:
初始能量值具有二段性和单调性,即存在一个临界值使初始能量值最小,可用二分来寻找
代码部分:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int a[N];
int n;
// check函数检验该能量值是否符合题意
bool check(int x)
{
for(int i=0;i<n;i++)
{
x=2*x-a[i];
if(x>1e5) return true;// 这一点很重要 当某点的能量值大于最大值时,表明往后的能量是不会小于0
if(x<0) return false;// 不符合要求
}
return true;
}
int main ()
{
cin>>n;
for(int i=0;i<n;i++) scanf("%d",&a[i]);
int l=0,r=1e5;
// 寻找左端点
while(l<r)
{
int mid=(l+r)/2;
if(check(mid)) r=mid;
else l=mid+1;
}
cout<<l<<endl;
return 0;
}
- 友友们,若是发现什么错误,请指出来呦,我们疫一起互相进步呀!最后,点个赞赞吧,谢谢啦