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

题意:问最少要几个连续的数字大于给定的s;

题解: 尺取法:

这是他的执行过程。



先找到一个大于s的序列,然后对这个序列处理,直到小于s,然后在加入后面的数字,不断的循环。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[100000+10];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
long long sum=0;
int st=0,en=0;
int ans=0x3f3f3f3f;
while(1)
{
while(en<n&&sum<m)
sum+=a[en++];
if(sum<m)
break;
ans=min(ans,en-st);
sum-=a[st++];
}
if(ans==0x3f3f3f3f) ans=0;
printf("%d\n",ans);
} return 0;
}

Poj3061Subsequence的更多相关文章

  1. poj--3061--Subsequence(贪心)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4498 Desc ...

  2. 【尺取法】POJ3061 & POJ3320

    POJ3061-Subsequence [题目大意] 给定长度微n的数列整数及整数s.求出总和不小于s的连续子序列的长度的最小值.如果节不存在,则输出0. [思路] 尺取法五分钟裸裸裸~刷水刷出了罪恶 ...

随机推荐

  1. linux安装jdk7步骤

    linux安装jdk7步骤: 1.首先使用命令查看linux系统版本号: lsb_release -a 2.下载对应的jdk版本,笔者使用的是jdk-7u79-linux-x64.tar.gz: 3. ...

  2. blueterm蓝牙超级终端(源码)

    今天FQ访问外文网站砍看见的终端程序,貌似现在已经开源了,这个软件源码方便开发者开发开发蓝牙与相关的设备的通信,所以在此分享下,给需要的人 项目地址:点击打开,感谢作者,我已经实验过了,导入到ecli ...

  3. Unicode字符集

    Unicode字符集的出现是为了弥补ASCII码只能表示128个字符的限制.在实际应用中,如若我们想显示汉字或日文等等,显然使用ASCII是不可能的.Unicode占用了两个字节,即16位,能表示的字 ...

  4. PHP报错configure error Cannot find libmysqlclient under usr

    编译PHP报错configure error Cannot find libmysqlclient under usr的解决方法 (问题产生,mysql是yum安装的,libmysqlclient* ...

  5. dac verilog ad5601

    首先从官网下载数据手册.DAC有串行有并行,ad5601是串行,(需要好多时钟沿的移位内部转换为并行在输出). 按照手册的时序编写程序, 关注下芯片的波特率范围 看看手册的数据传输那些事有效的数据位 ...

  6. linux 命令——21 find(转)

    在 使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行.但有些系统对能够传递给exec的命 令长度有限制,这样在find命令运行几分钟之后,就 ...

  7. 如何在SAP Cloud for Customer自定义BO中创建访问控制

    文章作者: Yi 已获得Yi的转载许可. 访问控制方式和使用注意事项 1. C4C中的访问控制有两种方式 RelevantForAccessControl AccessControlContext 2 ...

  8. 关于wp8.1 runtime模式下面的摄像头调用拍照问题和应用生命周期问题

    现在的msdn文档,还找不到详细的wp8.1的摄像头拍照文档,只有一个序列拍照,类似九连拍的文档,而且这文档感觉就是windows8.1搬过来应付的,wp8.1模式,只要有一个地方处理不好,手机就会死 ...

  9. EF和linq语句查询条件不等于某个参数出现的问题

    where t.a!=字符串   这是错误的写法,正确为 where t.a!=字符串.trim() 其他类型变量需要保持实体类型和查询条件参数的类型是一致的,不然出现的语句可能会是 类似`Exten ...

  10. POJ 2010 Moo University - Financial Aid(堆维护滑窗kth,二分)

    按照score排序,贪心,从左到右用堆维护并且记录前面的最小N/2个花费之和. 然后从右向左枚举中位数,维护N/2个数之和加上并判断是否满足条件.(stl的队列没有clear(),只能一个一个pop. ...