因为看柳神也是使用二分,所以写了一个滚动数组的~
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct node {
int delta,low,high;
};
bool cmp(node a,node b){
return a.delta==b.delta? a.low<b.low :a.delta<b.delta;
}
int main(){
int n,l[100010],sum=0,low=0,high=0,M,min=1000000000;
int now=0,top=0;
scanf("%d %d",&n,&M);
for(int i=0;i<n;i++){
scanf("%d",&l[i]);
}
vector<node> v(n);
for(low=0;low<n;low++){
while (now<M && high<n)now+=l[high++];
if (now<M)break;
// struct node v[top++]={now-M,low,high-1};
v[top++]={now-M+1,low+1,high};
now-=l[low];
}
sort(v.begin(),v.end(),cmp);
for(int i=0;i<n;i++){
if(v[i].delta>min)break;
if (v[i].delta==0)continue;
min=v[i].delta;
printf("%d-%d\n",v[i].low,v[i].high);
}
return 0;
}