【POJ】3061

来源:互联网 发布:611资源网 新域名在线 编辑:程序博客网 时间:2024/06/05 16:52

http://poj.org/problem?id=3061

给定长度n的数列,整数S,求和不小于S的连续子序列的长度的最小值。

尺取法例题。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <algorithm>using namespace std;int t;int n,S;int a[100005];int sum[100005];int main(){    cin >> t;    while (t--){        cin >> n >> S;        for (int i=0;i<n;i++){            cin >> a[i];        }        int ans=n+1;        int s=0,t=0,sum=0;        while (1){            while (t<n&&sum<S){                sum+=a[t++];            }            if (sum<S) break;            ans=min(ans,t-s);            sum-=a[s++];        }        if (ans>n) ans=0;        cout << ans << endl;    }}
原创粉丝点击