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

  1. 2
  2. 10 15
  3. 5 1 3 5 10 7 4 9 2 8
  4. 5 11
  5. 1 2 3 4 5

Sample Output

  1. 2
  2. 3
    解题思路:这个题可以用二分,但还有一种更优的算法技巧:尺取法,利用两个下标(起点,终点)不断放缩像虫子伸缩爬行一样来爬出一个最优解,即反复地推进区间的开头和结尾,来求取满足条件的最小区间长度。
    AC代码一(79ms):尺取法:时间复杂度是0(n)。
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. using namespace std;
  5. const int maxn=1e5+;
  6. int t,n,S,sum,st,ed,res,a[maxn];
  7. int main(){
  8. while(~scanf("%d",&t)){
  9. while(t--){
  10. scanf("%d%d",&n,&S);sum=st=ed=;res=maxn;
  11. for(int i=;i<n;++i)scanf("%d",&a[i]);
  12. while(){
  13. while(ed<n&&sum<S)sum+=a[ed++];
  14. if(sum<S)break;//如果当前序列和小于S,直接退出
  15. res=min(res,ed-st);
  16. sum-=a[st++];//指针st往右走,减去队首值
  17. }
  18. if(res>n)res=;
  19. printf("%d\n",res);
  20. }
  21. }
  22. return ;
  23. }

AC代码二(94ms):二分法:时间复杂度是O(nlogn)。

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. #include<string.h>
  5. using namespace std;
  6. const int maxn=1e5+;
  7. int t,n,S,a[maxn],sum[maxn];
  8. int main(){
  9. while(~scanf("%d",&t)){
  10. while(t--){
  11. scanf("%d%d",&n,&S);
  12. memset(sum,,sizeof(sum));
  13. for(int i=;i<n;++i)scanf("%d",&a[i]),sum[i+]=sum[i]+a[i];
  14. if(sum[n]<S){puts("");continue;}//解不存在
  15. int res=n;
  16. for(int k=;sum[k]+S<=sum[n];++k){
  17. int ed=lower_bound(sum+k,sum+n+,sum[k]+S)-sum;//二分查找
  18. res=min(res,ed-k);
  19. }
  20. printf("%d\n",res);
  21. }
  22. }
  23. return ;
  24. }

题解报告:poj 3061 Subsequence(前缀+二分or尺取法)的更多相关文章

  1. POJ 3061 Subsequence【二分答案】||【尺取法】

    <题目链接> 题目大意: 给你一段长度为n的整数序列,并且给出一个整数S,问你这段序列中区间之和大于等于S的最短区间长度是多少. 解题分析:本题可以用二分答案做,先求出前缀和,然后枚举区间 ...

  2. poj 2566Bound Found(前缀和,尺取法)

    http://poj.org/problem?id=2566: Bound Found Time Limit: 5000MS   Memory Limit: 65536K Total Submissi ...

  3. Atcoder Beginner Contest 155D(二分,尺取法,细节模拟)

    二分,尺取法,细节模拟,尤其是要注意a[i]被计算到和a[i]成对的a[j]里时 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> ...

  4. POJ 3061 (二分+前缀和or尺取法)

    题目链接: http://poj.org/problem?id=3061 题目大意:找到最短的序列长度,使得序列元素和大于S. 解题思路: 两种思路. 一种是二分+前缀和.复杂度O(nlogn).有点 ...

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

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

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

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

  7. poj 3061 Subsequence

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

  8. poj 3061(二分 or 尺取法)

    传送门:Problem 3061 https://www.cnblogs.com/violet-acmer/p/9793209.html 马上就要去上课了,先献上二分AC代码,其余的有空再补 题意: ...

  9. [ACM] POJ 3061 Subsequence (仿真足)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8403   Accepted: 3264 Descr ...

随机推荐

  1. CentOS 6.X配置 NFS以及启动和mount挂载

    一.环境介绍: 服务器:centos 192.168.1.225 客户端:centos 192.168.1.226 二.安装: NFS的安装配置:centos 5 : yum -y install n ...

  2. IPTABLES基本例子

    iptables –F #删除已经存在的规则 iptables -P INPUT DROP #配置默认的拒绝规则.基本规则是:先拒绝所有的服务,然后根据需要再添加新的规则. iptables -A I ...

  3. [Zlib]_[0基础]_[使用zlib库压缩文件]

    场景: 1. WIndows上没找到系统提供的win32 api来生成zip压缩文件, 有知道的大牛麻烦留个言. 2. zlib比較经常使用,编译也方便,使用它来做压缩吧. MacOSX平台默认支持z ...

  4. vux tabbar 组件

    1.App.vue <!-- 入口文件 --> <template> <div id="app"> <!-- 视图层 --> < ...

  5. 深刻理解Java中形參与实參,引用与对象的关系

    声明:本博客为原创博客,未经同意.不得转载! 原文链接为http://blog.csdn.net/bettarwang/article/details/30989755 我们都知道.在Java中,除了 ...

  6. Bash Shell 解析路径获取文件名称和文件夹名

    前言 还是今天再写一个自己主动化打包脚本.用到了从路径名中获取最后的文件名称.这里记录一下实现过程. 当然,最后我也会给出官方的做法.(ps:非常囧,实现完了才发现原来Bash Shell有现成的函数 ...

  7. 下面forward和redirect的描述,正确的是(ABCD)

    A:forward是服务器将控制权转交给内部服务器对象,由新的对象来全权负责响应用户的请求 B:执行forward时,浏览器不知道服务器所发送的内容从那里来,浏览器地址栏中还是原来的地址 C:执行re ...

  8. how to create modals with Bootstrap

    In this tutorial you will learn how to create modals with Bootstrap. Creating Modals with Bootstrap ...

  9. (15)ServletConfig对象详解

    1,作用 主要是用于加载servlet的初始化参数.在一个web应用可以存在多个ServletConfig对象(一个Servlet对应一个ServletConfig对象) 2,创建时机和对象的获取 创 ...

  10. 以太坊 EVM内交易执行分析(一)

    以太坊上交易最终都会由EVM进行解析存入数据库,今天就来探讨一下,一笔交易是如何别EVM执行的.我们可以把交易分为三种.(注意,和交易相关的模块很多,交易的生命周期存在于整个以太坊中,我们这次只是分析 ...