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

2
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5

Sample Output

2
3
 #include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int ms=;
int a[ms];
int sum[ms];
int n,S;
void solve()
{
sum[]=;
for(int i=;i<n;i++)
sum[i+]=sum[i]+a[i];
if(sum[n]<S)
{
puts("");
return ;
}
int ans=n;
for(int s=;sum[s]+S<=sum[n];s++)
{
int t=lower_bound(sum+s,sum+n+,sum[s]+S)-sum;
ans=min(ans,t-s);
}
printf("%d\n",ans);
return ;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>S;
for(int i=;i<n;i++)
cin>>a[i];
solve();
}
return ;
}

O(n)

#include"iostream"
#include"cstdio"
#include"algorithm"
#include"cstring"
using namespace std;
const int ms=;
int a[ms];
int n,S;
void solve()
{
int s=,sum=,t=;
int ans=n+;
while()
{
while(t<n&&sum<S)
{
sum+=a[t++];
}
if(sum<S)
break;
ans=min(ans,t-s);
sum-=a[s++];
}
if(ans>n)
{
puts("");
return ;
}
printf("%d\n",ans);
return ;
}
int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&S);
for(i=;i<n;i++)
scanf("%d",&a[i]);
solve();
}
return ;
}

Subsequence的更多相关文章

  1. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  2. [LeetCode] Is Subsequence 是子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  3. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  4. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  5. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  6. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  7. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  8. CF724D. Dense Subsequence[贪心 字典序!]

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  10. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

随机推荐

  1. [.NET源码学习]实例化Font,遭遇字体不存在的情况。

    实例化Font类时,当传入参数为不存在或未安装的字体时,Windows系统会用Microsoft Sans Serif字体替代该字体. Msdn: "For more information ...

  2. [Hive - Tutorial] Querying and Inserting Data 查询和插入数据

    Querying and Inserting Data Simple Query Partition Based Query Joins Aggregations Multi Table/File I ...

  3. CentOS6 root 用户 vi/vim 无法开启高亮

    编辑 /etc/profile.d/vim.sh if [ -n "$BASH_VERSION" -o -n "$KSH_VERSION" -o -n &quo ...

  4. bash里,echo对换行符的处理

    echo -e "#include <stdio.h>\nint main()\n{\n printf(\"hello world\\\n\");\n ret ...

  5. 开源软交换系统 FreeSwitch 与 Asterisk 比较

    Asterisk 与freeswitch都是流行的开源软交换服务器,Asterisk出现的比较早,大概在1999年开始此项目,应该是最流行的开源软交换服务器,整个社区上下游都已经很成熟. freesw ...

  6. Java缓存学习之四:EhCache

    EhCache 关键词:纯Java的进程内缓存框架.Java分布式缓存.缓存数据有两级:内存和磁盘.缓存数据会在虚拟机重启的过程中写入磁盘.是hibernate默认的缓存provider: Ehcac ...

  7. 最新CentOS6.x下redis安装

    1:软件环境: 系统版本:CentOS release 6.5 redis版本:redis-cli 3.0.5 安装目录:"/usr/local/redis" 下载软件:" ...

  8. Nexus搭建Manven

    Nexus相当于中转服务器,减轻网络的负载,加速项目搭建的进程 1.下载地址:http://www.sonatype.org/nexus/go 2.下载的是zip包,解压后进入D:\nexus-2.8 ...

  9. 剑指OFFER之跳台阶(九度OJ1388)

    题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入包括一个整数n(1<=n< ...

  10. [c++]程序的内存划分理解

    全局和静态数据区:用于存放全局变量和静态变量(全局变量和局部变量) 常量数据区:用于存放常量数据 代码区:用于存储代码 栈:用于局部变量和函数参数 堆:程序员申请(程序员控制的部分,new/delet ...