4 条题解

  • 0
    @ 2026-5-24 11:19:55
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    const int N = 200010;
    int a[N];
    ll pre[N]; 
    int n, S;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);
    
        cin >> n >> S;
        for (int i = 1; i <= n; ++i) {
            cin >> a[i];
            pre[i] = pre[i-1] + a[i]; 
        }
    
        int l = 1, ans = 0;
        // 枚举右端点r,找最左的l使得 [l, r] 的和 ≤ S
        for (int r = 1; r <= n; ++r) {
            while (pre[r] - pre[l-1] > S) {
                l++;
            }
            ans = max(ans, r - l + 1);
        }
    
        cout << ans << endl;
        return 0;
    }
    
    • 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
          难度
          普及−
          标签
          递交数
          720
          已通过
          161
          上传者