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. curl 中文乱码

    curl 中文乱码 学习了:https://blog.csdn.net/thc1987/article/details/52583789 学习了: http://blog.itpub.net/2903 ...

  2. Deepin-安装vscode

    安装方式有两种: 1.通过命令安装 sudo apt-get install vscode 2.通过deb或rpm包安装 我们是Debian系列的系统,所以用deb包,关于红帽系统,请使用rpm包. ...

  3. POI 的使用

    POI 使用 一. POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能..NET的开发人员则 ...

  4. 如何在Visual Studio 2017中使用C# 7+语法 构建NetCore应用框架之实战篇(二):BitAdminCore框架定位及架构 构建NetCore应用框架之实战篇系列 构建NetCore应用框架之实战篇(一):什么是框架,如何设计一个框架 NetCore入门篇:(十二)在IIS中部署Net Core程序

    如何在Visual Studio 2017中使用C# 7+语法   前言 之前不知看过哪位前辈的博文有点印象C# 7控制台开始支持执行异步方法,然后闲来无事,搞着,搞着没搞出来,然后就写了这篇博文,不 ...

  5. 解决javah生成.h头文件找不到找不到android.support.v7.app.AppCompatActivity的问题

    问题描写叙述: 在使用Android Studio进行JNI开发时,须要使用javah生成C或C++的头文件,可是可能会遇到: 错误: 无法訪问android.support.v7.app.AppCo ...

  6. 20170111 ABAP技术小结(全半角转换)

    DATA: it_po LIKE it_alv OCCURS 0 WITH HEADER LINE.************************************************** ...

  7. bzoj4593: [Shoi2015]聚变反应炉

    这道题的难点其实是在设DP方程,见过就应该会了 令f0,i表示先激发i的父亲,再激发i,把i的整棵子树都激发的最小费用 f1,i表示先激发i,再激发i的父亲,把i的整棵子树都激发的最小费用 设x,y为 ...

  8. 织梦首页TAG标签页的仿制

    1,tag标签的作用:主要是为了能够使得用户可以更加精确的找寻到自己所需内容.这种TAG搜索方式,比分类搜索更加的精确.具体以及节省时间. 2,怎么能够合理的优化TAG标签? A:明白网站的TAG标签 ...

  9. 【Idea】Debug模式

    Idea则是把手标放到你想显示结果的代码上,按Ctrl+F1就显示结果. 如果你想跳到下一个断点直接按F9

  10. C#函数3递归

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Console ...