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

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

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
using namespace std;
const int maxn=1e5+;
int t,n,S,a[maxn],sum[maxn];
int main(){
while(~scanf("%d",&t)){
while(t--){
scanf("%d%d",&n,&S);
memset(sum,,sizeof(sum));
for(int i=;i<n;++i)scanf("%d",&a[i]),sum[i+]=sum[i]+a[i];
if(sum[n]<S){puts("");continue;}//解不存在
int res=n;
for(int k=;sum[k]+S<=sum[n];++k){
int ed=lower_bound(sum+k,sum+n+,sum[k]+S)-sum;//二分查找
res=min(res,ed-k);
}
printf("%d\n",res);
}
}
return ;
}

题解报告: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. Excel中INDEX函数的使用

    1 函数INDEX( )有两种形式: 数组形式——INDEX(array,row_num,column_num)和 引用形式——INDEX(reference,row_num,column_num,a ...

  2. 【python】对象和面向对象

    类的定义 python支持多重继承,在类名后面的小括号中,可以列出多个类名,以逗号分割. __init__方法在类的实例创建后被立即调用,注意与c++中构造函数不一样,因为对象在调用__init__时 ...

  3. 相机标定(Camera calibration)

    简单介绍 摄像机标定(Camera calibration)简单来说是从世界坐标系换到图像坐标系的过程.也就是求终于的投影矩阵 P 的过程,以下相关的部分主要參考UIUC的计算机视觉的课件(网址Spr ...

  4. OSChinaclient源代码学习(2)--缓存的设计

    一.缓存的作用 请求数据的时候,首先进行推断,能否够从缓存中获取数据,假设满足条件,则直接从缓存中获取数据.否则请求新的数据.这样比没有缓存的情况下.每次都要从server请求数据要快,并且.没有网的 ...

  5. 如约而至,Java 10 正式发布! Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势 努力的孩子运气不会太差,跌宕的人生定当更加精彩 优先队列详解(转载)

    如约而至,Java 10 正式发布!   3 月 20 日,Oracle 宣布 Java 10 正式发布. 官方已提供下载:http://www.oracle.com/technetwork/java ...

  6. Ioc 器管理的应用程序设计,前奏:容器属于哪里? 控制容器的反转和依赖注入模式

    Ioc 器管理的应用程序设计,前奏:容器属于哪里?   我将讨论一些我认为应该应用于“容器管理”应用程序设计的原则. 模式1:服务字典 字典或关联数组是我们在软件工程中学到的第一个构造. 很容易看到使 ...

  7. InfoQ中文站特供稿件:Rust编程语言的核心部件

    本文为InfoQ中文站特供稿件.首发地址为: http://www.infoq.com/cn/articles/rust-core-components .如需转载.请与InfoQ中文站联系. 原文发 ...

  8. Java 实现桥接(Bridge)模式

    类图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamp3d21scDQ1Ng==/font/5a6L5L2T/fontsize/400/fill/I0 ...

  9. elasticsearch_初始篇

    一.elasticsearch简单介绍 Elasticsearch 是一个基于Lucene的分布式.可扩展.近实时的搜索与数据分析引擎. 它能从项目一开始就赋予你的数据以搜索.分析和探索的能力. 实时 ...

  10. C++ 虚函数与纯虚函数 浅析

    [摘要] 在虚函数与纯虚函数的学习中.要求理解虚函数与纯虚函数的定义,了解虚函数与纯虚函数在实例化上的差异.掌握两者在实现上的必要性.熟悉纯虚函数在子类与孙类的函数类型.本文即针对上述问题展开阐述. ...