POJ2061 Subsequence

来源:互联网 发布:阿里云怎么提现 编辑:程序博客网 时间:2024/06/04 19:22
Subsequence
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14709 Accepted: 6210

Description

A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to S.

Input

The first line is the number of test cases. For each test case the program has to read the numbers N and S, separated by an interval, from the first line. The numbers of the sequence are given in the second line of the test case, separated by intervals. The input will finish with the end of file.

Output

For each the case the program has to print the result on separate line of the output file.if no answer, print 0.

Sample Input

210 155 1 3 5 10 7 4 9 2 85 111 2 3 4 5

Sample Output

23

Source

Southeastern Europe 2006

—————————————————————————————————————
题目的意思是给出n个整数,求区间和大于等于m的最小区间长度
思路:由于和具有单调性,直接尺取法
#include <iostream>#include <functional>#include <stdio.h>#include <queue>#include<cmath>#include<algorithm>using namespace std;#define LL long longconst int inf=0x3f3f3f3f;int n,m;int a[1000005];int main(){  int t;  scanf("%d",&t);  while(t--)  {      scanf("%d%d",&n,&m);    for(int i=0;i<n;i++)    {    scanf("%d",&a[i]);    }    int l=0,r=0;    int sum=0;    int ans=inf;    while(1)    {        while(r<n&&sum<m)        {            sum+=a[r++];        }        if(sum<m) break;        ans=min(ans,r-l);        sum-=a[l++];    }    if(ans==inf)        ans=0;    printf("%d\n",ans);  }    return 0;}