题目大意:
题解:
尺取法维护区间,当(temp>m)时头指针向右移直到(temp leq m),记录最大区间。
#include <algorithm>
#include <iostream>
using namespace std;
int n, m, s[100010];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
}
int ans = 0, temp = 0, l = 1, r = 1;
while (r <= n) {
temp += s[r];
r++;
while (temp > m) {
temp -= s[l];
l++;
}
ans = max(ans, r - l);
}
cout << ans << endl;
return 0;
}