题目链接
自己二分写挂了,得了90分,不知道哪里错了,参考大佬简洁代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <functional>
#include <set>
using namespace std;
typedef pair<int,int> PII;
#define int long long
int s[200010];
int n,h;
signed main()
{
cin>>n>>h;
for(int i=1;i<=n;i++)cin>>s[i],s[i]+=s[i-1];
int m=s[n]-h;
int res=1e9;
for(int i=0;i<n;i++)
{
auto pos=upper_bound(s,s+n+1,s[i]+m);
int t=pos-s;
if(pos!=s+n+1)res=min(res,h-(s[n]-(s[t]-s[i])));
}
if(s[n]<h)cout<<h-s[n]<<'\n';
else cout<<res<<'\n';
}