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

Source

Southeastern Europe 2006

#include<cstdio>
#include<cstring>
#include <iostream>
using namespace std;
const int maxn=100005;
int a[maxn];
int n,s;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&s);
for(int i=0;i<n;++i)
scanf("%d",&a[i]);
int i=0,j=0,ans=n+1,sum=0;
while(1)
{
while(sum<s && i<n)
sum+=a[i++];
if(sum<s) break;
ans=min(ans,i-j);
sum-=a[j++];
}
if(ans>n) ans=0;
printf("%d\n",ans);
}
return 0;
}

poj3061 Subsequence【尺取法】的更多相关文章

  1. POJ3061——Subsequence(尺取法)

    Subsequence POJ - 3061 给定长度为n的数列整数a0,a1,a2-an-1以及整数S.求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0. 反复推进区间的开头和末尾,来 ...

  2. POJ 3061 Subsequence 尺取法

    转自博客:http://blog.chinaunix.net/uid-24922718-id-4848418.html 尺取法就是两个指针表示区间[l,r]的开始与结束 然后根据题目来将端点移动,是一 ...

  3. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  4. POJ3061 Subsequence 尺取or二分

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  5. POJ 3061 Subsequence(尺取法)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18145   Accepted: 7751 Desc ...

  6. POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9050   Accepted: 3604 Descr ...

  7. poj3061 Subsequence(尺取法)

    https://vjudge.net/problem/POJ-3061 尺取发,s和t不断推进的算法.因为每一轮s都推进1所以复杂度为O(n) #include<iostream> #in ...

  8. poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)

    这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...

  9. poj3061 Subsequence ,尺取法

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  10. 尺取法 poj3061 poj3320

    尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间. poj3061 http://poj.org/problem?id=3061 给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的 ...

随机推荐

  1. SpringMVC 运行过程

    加入jar包 在web.xml中配置DispatherServlet 加入SpringMVC配置文件 编写处理请求的处理器,并表示为处理器 编写视图 可见的运行流程: 实际的运行流程:

  2. Robot Framework快捷键图标制作 去掉cmd命令窗口

    安装好Robot Framework之后,通过 C:\Python27\Scripts\ride.py 启动时会带上一个命令行窗口: 怎样让启动的界面后面不带这个命令行窗口,且图片以机器人显示? 方法 ...

  3. Android下拉刷新

    以下是我自己花功夫编写了一种非常简单的下拉刷新实现方案,现在拿出来和大家分享一下.相信在阅读完本篇文章之后,大家都可以在自己的项目中一分钟引入下拉刷新功能 最近项目中需要用到ListView下拉刷新的 ...

  4. 网络流之最大流算法(EK算法和Dinc算法)

    最大流 网络流的定义: 在一个网络(有流量)中有两个特殊的点,一个是网络的源点(s),流量只出不进,一个是网络的汇点(t),流量只进不出. 最大流:就是求s-->t的最大流量 假设 u,v 两个 ...

  5. VIDIOC_S_INPUT 作用 (转载)

    转载:http://blog.csdn.net/kickxxx/article/details/7088658 G_INPUT和S_INPUT用来查询和选则当前的input 一个video设备节点可能 ...

  6. 靶形数独 2009年NOIP全国联赛提高组(搜索)

    靶形数独 2009年NOIP全国联赛提高组  时间限制: 4 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description 小城和小华都是热爱数 ...

  7. redis 客户端工具 RedisDesktopManager

    https://redisdesktop.com/download 可以查看到spring+redis  缓存的数据

  8. 计算几何值平面扫面poj2932 Coneology

    Coneology Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4097   Accepted: 859 Descript ...

  9. 自定义View(10)*onSizeChanged,onMeasure,onDraw的注意事项及正确写法

    1,onSizeChanged 触发: 当view的第一次分配大小或以后大小改变时的产生的事件. 工作: 计算绘制内容的位置,面积等相关值.避免每次在onDraw中计算了. 注意: 计算时不要忘记pa ...

  10. 无法收集统计信息,怎样优化SQL。

    特殊情况如下 客户的统计信息是固定的,没办法收集统计信息 . SQL profile 是最后考虑方案,因为同样写法sql 比较多,几十条. Parallle 并行客户一般不考虑接受,OLTP 系统. ...