Codeforces 279B

279B - Books

思路:尺取法求和小于等于t最长的一段的长度。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

const int N=1e5+5;
int a[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,t;
    cin>>n>>t;
    for(int i=1;i<=n;i++)cin>>a[i];
    int l=1,r=1,s=0,ans=0;
    while(true){
        while(r<=n&&s<=t)s+=a[r++];
        if(s<=t){
            ans=max(ans,r-l);
            break;
        }
        else ans=max(ans,r-l-1);
        s-=a[l++]; 
    }
    cout<<ans<<endl;
    return 0;    
} 
原文地址:https://www.cnblogs.com/widsom/p/8305087.html