4 条题解
-
0
#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
#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
#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
- 1
信息
- ID
- 820
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 普及−
- 标签
- 递交数
- 720
- 已通过
- 161
- 上传者