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<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; int main()
{
ios::sync_with_stdio(false);
cin.tie();
int t,n,s,a[N],sum[N];
cin>>t;
while(t--){
cin>>n>>s;
for(int i=;i<n;i++)cin>>a[i];
int sum=,st=,en=,ans=n+;
while(en<=n){
while(sum<s&&en<=n){
sum+=a[en++];
}
// cout<<sum<<" "<<st<<" "<<en<<endl;
if(sum>=s)ans=min(ans,en-st);
sum-=a[st++];
}
if(ans==n+)cout<<<<endl;
else cout<<ans<<endl;
}
return ;

非尺取:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; int main()
{
ios::sync_with_stdio(false);
cin.tie();
int t,n,s,a[N],sum[N];
cin>>t;
while(t--){
cin>>n>>s;
for(int i=;i<n;i++)
{
cin>>a[i];
if(i==)sum[i]=a[i];
else sum[i]=sum[i-]+a[i];
}
int res=n+;
for(int i=;sum[i]+s<=sum[n-];i++)
{
int j=lower_bound(sum+i,sum+n,sum[i]+s)-sum;
// cout<<j<<endl;
res=min(res,j-i);
}
if(sum[n-]>=s)cout<<res<<endl;
else cout<<<<endl;
}
return ;
}

poj3061尺取法的更多相关文章

  1. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  2. poj3061(尺取法)

    ---恢复内容开始--- 题目意思:给你一段非负序列,再给你一个值k,找出这段序列中最少的连续子序列使得和为k: 解题思路:因为都是正数,我们只需要找到一段区间不大于k,就停止,然后左边趋近看是否能得 ...

  3. 尺取法 poj3061 poj3320

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

  4. poj3061 Subsequence(尺取法)

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

  5. poj3061 poj3320 poj2566尺取法基础(一)

    poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...

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

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

  7. 【尺取法】POJ3061 & POJ3320

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

  8. poj3061 Subsequence ,尺取法

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

  9. POJ 3061 Subsequence 尺取法

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

随机推荐

  1. spring或springmvc自动生成applicationcontext.xml或springmvc文件(此文转载和借鉴多篇文章)

    在用spring或者springmvc框架进行开发时,编辑applicationcontext.xml等配置文件是必不可少的,在eclipse中打开applicationcontext.xml通常是这 ...

  2. 【SF】开源的.NET CORE 基础管理系统 -介绍篇

    [SF]开源的.NET CORE 基础管理系统 -系列导航 1.环境: .NET Core SDK (https://www.microsoft.com/net/core) SQL Server or ...

  3. SharePoint 切换用户的小技巧

    前言 从SharePoint 2013开始,SharePoint就已经去掉了”Sign in as Different User”这个功能,也就是无法切换用户登录.当然,后来我们通过修改CONTROL ...

  4. MVC5 DB FIRST

    跟着师父一直在做codefirst的开发,最近有个新需求,就是需要人家的数据库,然后来开发,现在出现问题了.整理如下 目前有个现成的我们之前的codefirst的工程代码,我记得师父说过,根据数据库生 ...

  5. supervisor安装配置

    1.安装 下载:https://codeload.github.com/Supervisor/supervisor/zip/3.1.3 2.安装 .zip cd supervisor- python ...

  6. Atom 编辑器试用

    简介 它号称"21世纪可黑客的文本编辑器".GitHub支持并开源,并支持跨平台.和brackets编辑器一样基于浏览器开发,意味着你可以使用less(包含css)来定制编辑器界面 ...

  7. MCMC(一)蒙特卡罗方法

    MCMC(一)蒙特卡罗方法 MCMC(二)马尔科夫链(待填坑) MCMC(三)M-H采样和Gibbs采样(待填坑) 作为一种随机采样方法,马尔科夫链蒙特卡罗(Markov Chain Monte Ca ...

  8. Oracle ASM数据库故障数据恢复过程

    一.故障描述 ASM磁盘组掉线 ,ASM实例不能mount.ASM磁盘组有4个500G的磁盘组成,数据库和ASM软件为10.2.0.1,急需恢复oracle数据库.二.故障分析   分析组成ASM磁盘 ...

  9. 看Lucene源码必须知道的基本概念

    终于有时间总结点Lucene,虽然是大周末的,已经感觉是对自己的奖励,毕竟只是喜欢,现在的工作中用不到的.自己看源码比较快,看英文原著的技术书也很快.都和语言有很大关系.虽然咱的技术不敢说是部门第一的 ...

  10. 深入tornado中的协程

    tornado使用了单进程(当然也可以多进程) + 协程 + I/O多路复用的机制,解决了C10K中因为过多的线程(进程)的上下文切换 而导致的cpu资源的浪费. tornado中的I/O多路复用前面 ...