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. linux 基础 —— 网络管理

    Linux 最强大的功能是什么?网络功能. 修改 dns 服务器(解析域名到 ip 地址): $ sudo vim /etc/resolvconf/resolv.conf.d/base # 添加如下内 ...

  2. 【POJ 1679】 The Unique MST

    [题目链接] 点击打开链接 [算法] 先求出图的最小生成树 枚举不在最小生成树上的边,若加入这条边,则形成了一个环,如果在环上且在最小生成树上的权值最大的边等于 这条边的权值,那么,显然最小生成树不唯 ...

  3. 23. Ext xtype : "combo" 下拉选择框

    转自:https://blog.csdn.net/majishushu/article/details/52601161

  4. apache相关补充

    apache相关补充 sendfile机制 1)不用sendfile的传统网络传输过程: read(file, tmp_buf, len) write(socket, tmp_buf, len) 2) ...

  5. Linux 系统管理命令 - uptime - 显示系统的运行时间及负载

    命令详解 重要星级: ★★★☆☆ 功能说明: uptime 命令可以输出当前系统时间.系统开机到现在的运行时间.目前有多少用户在线和系统平均负载等信息 语法格式: uptime 说明: 直接执行 up ...

  6. Unity资源的查找

    Object.Destroy static function Destroy(obj: Object, t: float = 0.0F): void;   Description Removes a ...

  7. 洛谷 P2365 任务安排【dp】

    其实是可以斜率优化的但是没啥必要 设st为花费时间的前缀和,sf为Fi的前缀和,f[i]为分组到i的最小花费 然后枚举j转移,考虑每次转移都是把j到i分为一组这样意味着j及之后的都要增加s的时间,同时 ...

  8. [Swift通天遁地]一、超级工具-(9)在地图视图MKMapView中添加支持交互动作的标注图标

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  9. Linux day01(二)虚拟机快照和克隆的用法介绍

    一:快照 优点:运行虚拟机后不用担心系统会被弄崩溃了,点击快照会立即恢复到初始状态 缺点:回滚会带来数据的丢失,所以要考虑数据恢复的成本和找回数据时进行操作的成本 1. 在导航栏中找虚拟机快照的小图标 ...

  10. deepin 安装maven

    1.在官网 http://maven.apache.org/download.cgi 下载mvn包   2.新建mvn目录 mkdir  /usr/local/mvn 把压缩包解压到mvn目录下面 修 ...