12.31 长度最小的子数组 WA40分 #求助

#include <bits/stdc++.h>
using namespace std;
int n,t,a[300005];
long long s;
int main()
{
	scanf("%d",&t);
	while (t--)
	{
		scanf("%d",&n);
		scanf("%dlld",&s);
		long long minn=LLONG_MAX;
		long long sum=0;
		for(int i=1; i<=n; i++)
		{
			scanf("%d",&a[i]);
		}
		for(long long i=1, j=1; i<=n; i++)
		{
			for(j=max(i,j); j<=n; j++)
			{
				if(sum>=s && sum)
				{
					break;
				}
				sum+=a[j];
			}
			if (sum>=s)
				minn=min(minn,j-i);
			sum-=a[i];
		}
		if(minn == LLONG_MAX) minn=0;
		printf("%lld\n",minn);
	}
	return 0;
}
3 个赞

我也是wa40,AC不了好烦啊

2 个赞
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
    int f[300010];
    int minn=1e9;
   long long sum=0;
    long long n,s;
    cin>>n>>s;
    for(int i=1;i<=n;i++)
    {
        cin>>f[i];
    }
    for(int i=1,j=0;i<=n;i++)
    {
       while(sum<s && j<=n)
       {
             j++;
            sum+=f[j];
        }
        if(sum>=s)
        {
            minn=min(j-i+1,minn);
        }
        sum-=f[i];
        }
        if(s==0) cout<<"1"<<endl;
        else cout<<(minn==1e9?0:minn)<<endl;
    }
    return 0;
}