3 条题解

  • 0
    @ 2026-5-12 19:18:56
    #include<bits/stdc++.h>
    using namespace std;
    const int N=2e5+5;
    int a[N];
    long long s[N];
    
    int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
    
        int n,S;
        cin>>n>>S;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            s[i]=a[i]+s[i-1];
        }
        int r=0,ans=0;
        for(int l=1;l<=n;l++){
            if(ans>=n-l+1)break;//剪枝
            while(r<=n&&s[r]-s[l-1]<=S)r++;
            ans=max(ans,r-l);
            if(r>n)break;
        }
        cout<<ans;
        return 0;
    }
    
    • 0
      @ 2026-3-15 15:50:17
      #include<bits/stdc++.h>
      using namespace std;
      typedef long long ll;
      ll T=1;const ll N=2e5+5;
      ll n,x,y;
      ll pre[N];ll a[N];ll ans=0;
      void solve(){
      	cin>>n>>x;
      	for(ll i=1;i<=n;++i){
      		cin>>a[i];pre[i]=pre[i-1]+a[i];
      	}
      	ll l=1;ll r=1;
      	while(r<=n){
      		if(pre[r]-pre[l-1]<=x){
      			ans=max(ans,r-l+1);r++;
      		}
      		else l++;
      	}cout<<ans;
      	
      }
      
      int main(){
      	
      	while(T--)solve();
      	return 0;
      } 
      ```echarts
      
      
      • 0
        @ 2026-3-11 18:29:14
        import sys
        input = lambda:sys.stdin.readline().strip()
        
        n,s = map(int,input().split())
        a = [0] + list(map(int,input().split()))
        
        i = j = 1
        ans = 0
        sum = a[i]
        
        while(j<=n):
            if sum <= s:
                ans=max(ans,j-i+1)
                j+=1
                if j<=n:sum+=a[j]
            else:
                sum-=a[i]
                i+=1     
        print(ans)
        
        • 1

        信息

        ID
        820
        时间
        1000ms
        内存
        256MiB
        难度
        7
        标签
        递交数
        664
        已通过
        150
        上传者