#include<stdio.h>
#include<algorithm>
using namespace std;
int n, m;
long long sum[100010]={0};
int binary_search(int a,int search){//返回第一个大于等于m的j
int l=a, r=n;
while(l<r){
int mid=(l+r)/2;
if( (sum[mid]-sum[a-1])>=search )r=mid;
else l=mid+1;
}
return l;
}
int main(){
int i, j, temp, flag=0,min=100000010;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){scanf("%d",&temp);sum[i]=sum[i-1]+temp;}
for(i=1;i<=n;i++){
j=binary_search(i,m);
if(sum[j]-sum[i-1]==m){printf("%d-%d\n",i, j); flag=1;}
else if(sum[j]-sum[i-1]<min&&sum[j]-sum[i-1]>=m){min=sum[j]-sum[i-1];}
}
if(flag==0){
for(i=1;i<=n;i++){
j=binary_search(i,min);
if(sum[j]-sum[i-1]==min){printf("%d-%d\n",i, j);}
}
}
return 0;
}
PAT-A1044
最新推荐文章于 2022-09-12 20:16:28 发布