poj Seuence

来源:互联网 发布:win7打印机网络共享 编辑:程序博客网 时间:2024/06/04 23:34


Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

A seuence qof 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

题目大意:给出一组数,从中找出最小的序列,使他们的和干好>=S

解题思路:由于是找出连续的数列,所以从左边开始加,直到和超出S,超出之后依次剪掉左边的,前世保证和在大于等于S,同时在剪掉之前,向找到此时的数列区间,意思就是加到得最右边位置,减去最左边的位置。这样的过程会有多个,留下最小的。在减得和小于S之后,接着刚才的,继续往右加。知道最后结束。

#include<stdio.h>#include<string.h>const int MAX=100100;int a[100010]; int main(){int n,s;int T;int S;int min;int j,k,i;while(scanf("%d",&T)!=EOF){while(T--){ scanf("%d%d",&n,&s);memset(a,0,sizeof(a));for(i=0;i<n;i++){scanf("%d",&a[i]);}S=0;i=0;min=MAX;for(j=0;j<n;){S+=a[j++];if(S<s&&j<n) continue;while(S>=s){int temp;temp=j-i;min=min>temp?temp:min;S-=a[i];i++;}} if(min==MAX)//没有找到    printf("0\n");elseprintf("%d\n",min) ;} }return  0;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 怀了侄子的孩子怎么办 老人受风和发烧怎么办 在医院去世的人怎么办 加菲小时候掉线怎么办 加菲鼻子不通气怎么办 九个月宝宝怕狗怎么办 吃水果查出酒驾怎么办 吃荔枝查出酒驾怎么办 吃水果测出酒驾怎么办 喂奶喝了2杯啤酒怎么办 买了全保撞人了怎么办 喝酒骑摩托被撞怎么办 吃补肾的药上火怎么办 黄芪吃多了上火怎么办 煲汤当归放多了怎么办 科一考试迟到了怎么办 高考没上专科线怎么办 五笔打字忘字怎么办 淘宝违规扣12分怎么办 淘宝卖家违规了怎么办 淘宝店被删除了怎么办 我做了背债业务怎么办 用钱宝被冻结了怎么办 客厅地砖被淹了怎么办 狗牙花叶子发黄怎么办 弹弓皮筋太紧了怎么办 子宫内膜厚15mm怎么办 子宫内膜厚16mm怎么办 运动鞋大了一码怎么办 学生负担太重了怎么办 仙剑4没用的装备怎么办 老婆和老妈吵架怎么办 梦幻109经验多了怎么办 脑梗出院了头晕怎么办 闲鱼被骗了200怎么办 闲鱼被骗了怎么办举报 荒岛求生火灭了怎么办 万一吃到望月鳝怎么办 我受到微信恐吓怎么办 网上办理信用卡被骗了怎么办 刺客信条2刚出生怎么办