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. 利用WiFi Pineapple Nano渗透客户端获取SHELL

    前言: 前两篇文章介绍了The WiFi Pineapple Nano设备的一些主要功能模块,例如PineAP.SSLsplit和Ettercap等.今天给大家实际场景演示下如何利用Pineapple ...

  2. Meteor check

    check方法用于检查参数或类型是否匹配模式. 安装check包 打开命令提示符窗口,并安装该软件包. C:\Users\Administrator\Desktop\meteorApp>mete ...

  3. 多硬盘分区管理fdisk

    原文:http://blog.fens.me/linux-fdisk/ ---------------------------------------------------------------- ...

  4. webpack-Targets(构建目标)

    构建目标(Targets) 因为服务器和浏览器代码都可以用 JavaScript 编写,所以 webpack 提供了多种构建目标(target),你可以在你的 webpack 配置中设置. webpa ...

  5. Qt布局管理器的使用(一)

    曾经对Qt的布局管理器掌握的还不清楚,今天特意学习了下.感觉收获还挺大的,特意拿出来和大家分享. 首先.要明确布局管理器的用处,及使我们的界面看起来比較整洁.美化.另外一点就是为了使我们的控件可以更随 ...

  6. HDU 1160 FatMouse&#39;s Speed(DP)

    题意  输入n个老鼠的体重和速度   从里面找出最长的序列  是的重量递增时速度递减 简单的DP  令d[i]表示以第i个老鼠为所求序列最后一个时序列的长度  对与每一个老鼠i  遍历全部老鼠j  当 ...

  7. eclipse中导入其它的webproject遇到和解决的问题

    注:下面为我从网上搜来的方法.经使用及学习后整理. 学习javaweb有段时间了.对于导入新项目.遇到好多问题.但终于成功了. 错误1:string cannot be resolved to a t ...

  8. Ios 项目从头开发 MVVM模式(三)

    1.话说,本来想做个聚合查询功能.可是我的重点想研究xmpp聊天功能.所以使用mvvm模式做了全然模式51job主界面的页面. 2.首先给大家看我执行起来的界面. 3.界面非常easy,做这个界面主要 ...

  9. [办公自动化]EXCEL不大,但是保存很慢

    今天同事有一个excel文件.office 2007格式的. 折腾了半天.按照以往的经验,定位-对象,应该可以删除. 后来在“编辑”窗格的“查找和选择”里面,单击“选择窗格“.可以看到很多”pictu ...

  10. publish and submit

    http://blog.csdn.net/w_jewelry/article/details/8123639 1.Gerrit里点击“publish and submit”提示如下:Your chan ...