poj3061

尺取法模板题,学了一下挑战书上的写法,感觉很精炼

#include<stdio.h>
#include<iostream>
using namespace std;
int a[100010];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,s,low=1,up=1,tmp=0;
        int res=100010;
        scanf("%d %d",&n,&s);
        for(int i=1;i<=n;i++)
        scanf("%d",&(a[i]));
        for(;;)
        {
            while(up<=n&&tmp<s)
                tmp+=a[up++];
            if(tmp<s) break;
            res=min(res,up-low);//tmp是low到up-1的区间和
            tmp-=a[low++];




        }
        if(res==(100000+10))
            cout<<0<<endl;
        else
            cout<<res<<endl;


    }
    return 0;


}
原文地址:https://www.cnblogs.com/eason9906/p/11754954.html