Subsequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16520   Accepted: 7008

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

 
题目大意:t个测试数据 对于一个长度为N的序列 求出总和不小于S的连续子序列的长度的最小值。
题解:
解法一、
二分搜索
前缀和是满足单调性的,计算从每一个数开始总和刚好大于s的长度。具体实现就是
二分搜索s[i]+s是否存在于前缀和数组中,就是查找以i+1开头的总和刚好大于s的最短长度。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int t,n,s,x;
int sum[];
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&s);
for(int i=;i<=n;i++){
scanf("%d",&x);
sum[i]=sum[i-]+x;
}
if(sum[n]<s){
printf("0\n");
continue;
}
int res=n;
for(int i=;sum[i]+s<=sum[n];i++){
int t=lower_bound(sum+i+,sum+n+,sum[i]+s)-sum;
res=min(res,t-i);
}
printf("%d\n",res);
}
return ;
}
时间复杂度nlog(n)
解法二、
尺取法 时间复杂度O(n)
#include<iostream>
#include<cstdio>
using namespace std;
int res,t,n,s,l,r,sum;
int a[];
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&s);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
l=r=;sum=;res=n+;
for(;;){
while(r<=n&&sum<s){
sum+=a[r++];
}
if(sum<s)break;//必须先判断才能更新解
res=min(res,r-l);
sum-=a[l++];
}
if(res>n)printf("0\n");
else printf("%d\n",res);
}
return ;
}

POJ3061 Subsequence的更多相关文章

  1. poj3061 Subsequence(尺取法)

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

  2. poj3061 Subsequence(尺取)

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

  3. POJ3061 Subsequence 尺取or二分

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

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

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

  5. poj3061 Subsequence【尺取法】

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

  6. poj3061 Subsequence ,尺取法

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

  7. POJ3061——Subsequence(尺取法)

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

  8. [POJ3061]Subsequence(二分,前缀和)

    题目链接:http://poj.org/problem?id=3061 题意:给一个长为n的数列和整数s,求一个连续的子序列,使得这个子序列长度最短并且不小于这个整数s. 统计[1~i]的子序列和su ...

  9. POJ3061 Subsequence(二进制前缀和法律+仿真足)

    二分法+前缀和法律 满足子序列长度的条件(0,n)之间,sum[x+i]-sum[i]从i元素开始序列长度x和.前缀和可在O(n)的时间内统计 sum[i]的值.再用二分找出满足条件的最小的子序列长度 ...

随机推荐

  1. OpenGL - Tessellation Shader 【转】

    http://blog.sina.com.cn/s/blog_8c7d49f20102v4qm.html Patch is just an ordered list of vertices (在tes ...

  2. SVN服务器配置说明 【转】

    http://www.cnblogs.com/ricksun/articles/1564905.html 1.前 言 花了72小时,终于把 Subversion 初步掌握了.从一个连“什么是版本控制” ...

  3. chardet的使用

    http://blog.csdn.net/jy692405180/article/details/52496599

  4. react 路由传参

    今天,我们要讨论的是react router中Link传值的三种表现形式.分别为通过通配符传参.query传参和state传参. ps:进入正题前,先说明一下,以下的所有内容都是在react-rout ...

  5. servelet

    获取页面传递过来的参数. 调用后台代码实现相关业务逻辑. 根据返回结果,进行页面跳转.   问题:httpsession? 拆解: String 里面的.spllit方法.拆开后,返回值为string ...

  6. at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:)

    错误提示 错误原因 參考链接 错误提示: at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:) 在Andro ...

  7. python的多线程问题

    在对文件进行预处理的时候,由于有的文件有太大,处理很慢,用python处理是先分割文件,然后每个文件起一个线程处理,启了10个线程,结果还比不起线程慢一些,改成多进程之后就好了. 使用multipro ...

  8. IO模式——同步(堵塞、非堵塞)、异步

    为什么IO模式非常重要?由于现代的计算机和操作系统的架构决定了CPU是稀缺资源,大家都要来一起竞争.而IO(特别是网络相关的IO)的速度往往较慢.所以怎样进行IO就有了多种模式,包含同步.异步.堵塞. ...

  9. Cocos2d-X中提高性能的方法

     1)内存使用效率: 使用大纹理 场景切换时,要尽量使用replaceScene 2)用好缓存: CCTextureCache(纹理缓存) CCSpriteFrameCache(精灵帧缓存) CC ...

  10. Matlab中图片保存的四种方法

    matlab的绘图和可视化能力是不用多说的,可以说在业内是家喻户晓的.Matlab提供了丰富的绘图函数,比如ez**系类的简易绘图函数,surf.mesh系类的数值绘图函数等几十个.另外其他专业工具箱 ...