L-周周的泡泡
怎么说呢,看错题目想了一个多小时,原来是戳破连续的泡泡,既然是连续的,就只用枚举左端点然后二分查找右端点就行了
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define endl "\n"
typedef long long ll;
typedef pair<int ,int> PII;
const int INF=0x3f3f3f3f;
const int N=2e5+5;
const int M=1e7+5;
int n,h;
ll a[N];
ll res=0;
int main()
{
cin>>n>>h;
for (int i=1;i<=n;i++) cin>>a[i];
for (int i=1;i<=n;i++) a[i]+=a[i-1];
for (int i=1;i<=n;i++)
{
int l=i-1,r=n;
ll ans=-1;
while(l<=r)
{
int mid=(l+r)>>1;
if (a[n]-(a[mid]-a[i-1])<h)
{
ans=mid;
r=mid-1;
}else l=mid+1;
}
if (ans!=-1) ans=a[n]-(a[ans]-a[i-1]);
else ans=0;
res=max(res,ans);
}
cout<<h-res<<endl;
return 0;
}