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. hadoop多次格式化后,导致datanode启动不了,怎么办?(伪分布式)

    根据当初 hadoop 安装目录下 conf 目录的 core-site.xml 的设置,找到该目录:   进入该目录 在 data 和 name 文件夹下均有  current 文件夹 ,和 cur ...

  2. SCAU 07校赛 10317 Fans of Footbal Teams

    10317 Fans of Footbal Teams 时间限制:1000MS  内存限制:65535K 题型: 编程题   语言: 无限制 Description Two famous footba ...

  3. [iOS 多线程 & 网络 - 2.0] - 发送接收 服务器信息

    A.搭建java服务器 使用eclipse.tomcat和struts2框架搭建一个简单的服务器 1.准备好合适版本的JDK.eclipse EE.tomcat.struts2 框架包 2.配置JDK ...

  4. [iOS UI进阶 - 2.1] 彩票Demo v1.1

    A.需求 1.优化项目设置 2.自定义导航栏标题按钮 3.多版本处理 4.iOS6和iOS7的适配 5.设置按钮背景 6.设置值UIBarButtonItem样式   code source: htt ...

  5. HDU 4489 The King’s Ups and Downs (DP+数学计数)

    题意:给你n个身高高低不同的士兵.问你把他们按照波浪状排列(高低高或低高低)有多少方法数. 析:这是一个DP题是很明显的,因为你暴力的话,一定会超时,应该在第15个时,就过不去了,所以这是一个DP计数 ...

  6. ActiveMQ学习笔记(二) JMS与Spring

    上文可见,JMS Native API使用起来不是特别方便.好在Spring提供了很好的JMS支持. (一)配置ConnectionFactory 如果使用连接池的话,不要忘记activemq-poo ...

  7. Android系统中Parcelable和Serializable的区别

    转载:https://greenrobot.me/devpost/android-parcelable-serializable/ 进行Android开发的时候,我们都知道不能将对象的引用传给Acti ...

  8. sql 调用函数的方法

    USE [ChangHong_612]GO/****** Object: StoredProcedure [dbo].[st_MES_RptInspectWeight] Script Date: 09 ...

  9. shell脚本的入参

    shell脚本参数可以任意多,但只有前9个可以被访问,使用shift命令可以改变这个限制.参数从第一个开始,在第九个结束.$0 程序名字$n 第n个参数值,n=1..9 $* 所有命令行参数$@    ...

  10. MATLAB remove outliers.

    Answer by Richard Willey on 9 Jan 2012 Hi Michael MATLAB doesn't provide a specific function to remo ...