2 条题解

  • 1
    @ 2023-8-4 18:34:21
    #include <bits/stdc++.h>
    using namespace std;
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    typedef long long ll;
    const int N=1e6+5;
    int a[230002];
    int main()
    {
    	int n,m,sum=0,l=1,ans;
    	cin>>n>>m;
    	ans=n+1;
    	rep(i,1,n) scanf("%d",&a[i]);
    	
    	rep(r,1,n)
    	{
    		sum+=a[r];
    		while(sum>=m)
    		{
    			ans=min(ans,r-l+1);
    			sum-=a[l++];
    		}
    	}
    	if(ans==n+1) cout<<0;
    	else cout<<ans;
    	return 0;
    }
    
    • 0
      @ 2024-3-17 20:52:51
      #include <bits/stdc++.h>
      using namespace std;
      const int MAX=230010;
      int n,m,a[MAX],p[MAX];
      bool check(int mid){
      	for (int i=1;i<=n-mid+1;i++){
      		if (p[i+mid-1]-p[i-1]>=m)return true;
      	}
      	return false;
      }
      int main(){
      	scanf("%d%d",&n,&m);
      	for (int i=1;i<=n;i++){
      		scanf("%d",&a[i]);
      		p[i]=p[i-1]+a[i];
      	}
      	int l=1,r=n,mid;
      	while (l<=r){
      		mid=l+r>>1;
      		check(mid)?r=mid-1:l=mid+1;
      	}
      	printf("%d",l==n+1?0:l);
      	return 0;
      }
      
      • 1

      信息

      ID
      1101
      时间
      1000ms
      内存
      128MiB
      难度
      7
      标签
      递交数
      132
      已通过
      32
      上传者