WOJ1301-Subsequence

来源:互联网 发布:三菱plc 编程手册 编辑:程序博客网 时间:2024/05/22 10:43

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.

输入格式

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.

输出格式

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

样例输入

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

样例输出

23


#include<stdio.h>int main(){    int t;    scanf("%d",&t);    while(t--){        int n,s,i,j=0,ans=0,length=0,sum=0,flag=0;         int in[100000];        scanf("%d %d",&n,&s);        for(i=0;i<n;i++) scanf("%d",&in[i]);        for(i=0;j<n;){           if(i==j)           sum=in[i];           else{              if(flag) sum=sum+in[j];              else sum=sum-in[i-1];          }          if(sum>=s){            flag=0;            length=j-i+1;            if(ans==0) ans=length;            else if(length<ans) ans=length;            }          else flag=1;          if(ans==1) break;           if(flag) j++;          else i++;      }      printf("%d\n",ans);    }    return 0;}