CodeForces 279B Books

题目链接:CodeForces 279B Books

题目大意:
CodeForces 279B Books

题解:
尺取法维护区间,当\(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;
}
上一篇:2021.9.5笔记 Vue练习


下一篇:day11作业