POJ 3061 Subsequence (尺取法)

来源:互联网 发布:东方快车翻译软件 编辑:程序博客网 时间:2024/05/16 19:52


Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 7604Accepted: 2879

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
附:
#include <iostream>#include <string>#include <string.h>#include <map>#include <stdio.h>#include <algorithm>#include <queue>#include <vector>#include <math.h>#include <set>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))#define Max_N 100008#define Max_S 100000008 using namespace std;int g_N, g_S;int g_Seq[Max_N]; int FindSubsequence(){    int res = g_N + 1;    int left = 1, right = 1, sum = 0;    while(1){        while(right <= g_N&&sum < g_S){            sum += g_Seq[right++];        }        if(sum < g_S) break;        res =  Min(res , right - left);        sum -= g_Seq[left++];    }    return res > g_N?0:res;}int main(){    int test;    cin>>test;    while(test--){        scanf("%d%d",&g_N,&g_S);        for(int i = 1;i <= g_N;i++){            scanf("%d",&g_Seq[i]);        }        printf("%d\n",FindSubsequence());    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 在泰国买到假货怎么办 在泰国旅游遇到危险该怎么办? 拾到小米手环怎么办 苹果充电打游戏触点怎么办 衣服上金属锈了怎么办 光之子门锁住了怎么办 挖到金矿石了该怎么办 邻居是精神病扔石头怎么办 汽车保养手册丢了怎么办 五号电池没电了怎么办 电脑打印时显示脱机怎么办 博新计划两年后怎么办 门帘粘扣老是掉怎么办 魔术粘贴带双面背胶不粘了怎么办 斜挎包包磨衣服怎么办 电脑ip被别人知道了怎么办 西乐葆不止痛了怎么办 家里鸽子多捣蛋的不行怎么办 联璧倒闭了理财钱怎么办 众筹平台雷了怎么办 App理财无法提现怎么办 喝咖啡喝的心慌怎么办 睾丸被用力捏破流血怎么办 足球大师球队声望过高怎么办 fm17买了好多球员怎么办 fm2018电脑适应了战术怎么办 家纺摆放跑色怎么办 买的t恤太小了怎么办? 汽车全包围脚垫翘边怎么办 针织衣服线跑了怎么办 老板跟老板娘吵架我应该怎么办 偷看老板娘洗澡被发现怎么办? 幽浮2人挂了怎么办 蜻蜓fm业务扣费怎么办 退出键退不到主页面怎么办 3d纯英文版怎么办 3d单位设置错了怎么办 翼龙贷款不还款怎么办 lol视角变低了怎么办 王者荣耀体验卡太多怎么办 文件打开后超出电脑屏幕怎么办