Subsequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14698   Accepted: 6205

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

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 100004
#define L 31
#define INF 1000000009
#define eps 0.00000001
/*
尺取
*/
int a[MAXN], n, s;
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &s);
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
int l = ,sum = ,ans = INF;
for (int r = ; r < n; r++)
{
sum += a[r];
while (sum >= s)
{
sum -= a[l];
ans = min(r - l + , ans);
l++;
}
}
if (ans != INF)
printf("%d\n", ans);
else
printf("0\n");
}
}

POJ 3061 Subsequence 尺取的更多相关文章

  1. POJ 3061 Subsequence 尺取法

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

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

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

  3. POJ 3061 Subsequence(尺取法)

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

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

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

  5. POJ 3061 Subsequence ( 二分 || 尺取法 )

    题意 : 找出给定序列长度最小的子序列,子序列的和要求满足大于或者等于 S,如果存在则输出最小长度.否则输出 0(序列的元素都是大于 0 小于10000) 分析 : 有关子序列和的问题,都可以考虑采用 ...

  6. poj 3061 Subsequence

    题目连接 http://poj.org/problem?id=3061 Subsequence Description A sequence of N positive integers (10 &l ...

  7. POJ3061 Subsequence 尺取or二分

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

  8. 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)

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

  9. POJ 3061 Subsequence 二分或者尺取法

    http://poj.org/problem?id=3061 题目大意: 给定长度为n的整列整数a[0],a[1],--a[n-1],以及整数S,求出总和不小于S的连续子序列的长度的最小值. 思路: ...

随机推荐

  1. 【转载】HashMap实现原理浅析

    HashMap和Hashtable的区别 两者最主要的区别在于Hashtable是线程安全,而HashMap则非线程安全Hashtable的实现方法里面都添加了synchronized关键字来确保线程 ...

  2. Ubuntu下FileZilla的安装(转载)

    转自:http://os.51cto.com/art/201103/247564.htm FileZilla是一个免费而且开源的FTP客户端软件,共有两种版本:客户端版本.服务器版本.FileZill ...

  3. ACM_整数反转

    整数反转 Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定一个32位int型的整数,把这个整数反着输出,如123,输出321. ...

  4. day02_12/12/2016_bean的实例化之普通工厂方式

  5. 363 Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  6. 查询分析器执行SQL很快但是ado.net很慢:请为你的SQLparameter设置DbType

    我们都知道,参数化查询可以处理SQL注入,以及提高查询的效率,因为参数化查询会使MSSQL缓存查询的计划. 但是会出现一个问题:有的时候参数化查询比直接拼接sql字符串效率低好多,甚至是查询超时. 原 ...

  7. 图解TCP/IP笔记(1)——TCP/IP协议群

    转载请注明:https://www.cnblogs.com/igoslly/p/9167916.html TCP/IP制定  制定:IETF 记录:RFC - Request for comment ...

  8. JS——switch case

    语法: switch(n) { case 1: 执行代码块 1 break; case 2: 执行代码块 2 break; default: n 与 case 1 和 case 2 不同时执行的代码 ...

  9. 查看Windows XP是否已激活的方法

    打开开始→运行, 在输入:oobe/msoobe /a,回车后系统会弹出窗口告诉你系统是否已经激活

  10. 三年半Java后端面试鹅厂,三面竟被虐的体无完肤

    经过半年的沉淀,加上对MySQL,redis和分布式这块的补齐,终于开始重拾面试信心,再次出征. 鹅厂 面试职位: go后端开发工程师,接受从Java转语言 都知道鹅厂是cpp的主战场,而以cpp为背 ...