革命尚未成功,同志还需努力!
#include<iostream>
#include<math.h>
using namespace std;
const int maxn = 100010;
int m, n, sum[maxn] ;
int upper_bound(int a[], int l, int r, int x) {
int mid;
while (l<r)
{
mid = (l + r)>>1;
if (a[mid] > x)r = mid;
else l = mid + 1;
}
return l;
}
int main(){
int a[maxn], min = 100000010;
cin >> n >> m;
sum[0] = 0;
for (int i = 1; i <= n; i++)
{
cin >> sum[i];
sum[i] += sum[i - 1];
}
for (int i = 1; i <= n; i++)
{
int j = lower_bound(sum, i, n + 1, sum[i-1] + m);
if (sum[j-1]-sum[i-1]==m)
{
min = m;
break;
}
else if (j<=n&&sum[j]-sum[i-1]<min)
{
min = sum[j] - sum[i - 1];
}
}
for (int i = 1; i <= n; i++)
{
int j = lower_bound(sum, i, n + 1, sum[i - 1] + min);
if (sum[j-1]-sum[i-1]==min)
{
cout << i << '-' << j-1 << endl;
}
}
}
相关
upper_bound
//返回第一个大于x的位置
int upper_bound(int a[], int l, int r, int x) {
int mid;
while (l<r)
{
mid = (l + r) >> 1;
if (a[mid] > x)r = mid;//返回第一个大于等于x的位置:a[mid]>=x
else l = mid - 1;
}return l;
}