描述


http://poj.org/problem?id=3061

给定长度为n的数列整数以及整数S.求出总和不小于S的连续子序列的长度的最小值,如果解不存在输出0.

Subsequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11604   Accepted: 4844

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

分析


尺取法.

定义区间左右端点l和r.l从1开始循环到n,r向后移动,直到区间和>=s,此时为左端点为l时的最短长度.对于左端点为l+1的情况,使得区间和>s的右端点一定>=r,就让r右移直到满足条件.如果r=n仍无法满足,那对于之后的l都无法满足,即可break.此题用二分O(nlogn),用尺取法O(n).

p.s.

1.此题还可用二分做,复杂度为O(nlogn).

 #include<cstdio>
#include<algorithm>
using std :: min; const int maxn=;
int q,n,s;
int a[maxn]; void solve(int n,int s)
{
int ans=n+;
int r=,sum=;
for(int l=;l<=n;l++)
{
while(r<=n&&sum<s)
{
sum+=a[r++];
}
if(sum<s) break;
ans=min(ans,r-l);
sum-=a[l];
}
if(ans>n) ans=;
printf("%d\n",ans);
} void init()
{
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&n,&s);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
solve(n,s);
}
} int main()
{
freopen("Subsequence.in","r",stdin);
freopen("Subsequence.out","w",stdout);
init();
fclose(stdin);
fclose(stdout);
return ;
}

POJ_3061_Subsequence_(尺取法)的更多相关文章

  1. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

  2. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  3. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  4. CF 701C They Are Everywhere(尺取法)

    题目链接: 传送门 They Are Everywhere time limit per test:2 second     memory limit per test:256 megabytes D ...

  5. nyoj133_子序列_离散化_尺取法

    子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...

  6. Codeforces 676C Vasya and String(尺取法)

    题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...

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

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

  8. POJ 3320 尺取法,Hash,map标记

    1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...

  9. HDU 5358 尺取法+枚举

    题意:给一个数列,按如下公式求和. 分析:场上做的时候,傻傻以为是线段树,也没想出题者为啥出log2,就是S(i,j) 的二进制表示的位数.只能说我做题依旧太死板,让求和就按规矩求和,多考虑一下就能发 ...

随机推荐

  1. JavaScript中,按值传递与按地址(引用)传递。

    JavaScript中,数字型,字符串,布尔型等基本类型,传递给变量时,传递方式为按值传递,这个很好理解,不做多解释. 而令人有所疑惑的,是数组,对象等引用类型传递给变量是,传递方式为按地址传递.此处 ...

  2. HDU 4422 The Little Girl who Picks Mushrooms(简单题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4422 题目大意:小姑娘背着5个包去山上采蘑菇,每座山上只能用一个背包采集.三个小精灵会要她3个背包,其 ...

  3. 2017 google Round D APAC Test 题解

    首先说明一下:我只是用暴力过了4道题的小数据,就是简单的枚举,大数据都不会做!下面的题解,是我从网上搜到的解答以及查看排行榜上大神的答案得出来的. 首先贴一下主要的题解来源:http://codefo ...

  4. tomcat 192.168.1.110?不烦吗?

    最近做一个在线播放器,因为要用到网络服务器做在线播放,又不想直接在本地用tomcat做实验,因为没有真实感. so,手边两台电脑,同时连在局域网. 客户端,笔记本,ip1:192.168.1.101 ...

  5. C# IO流的操作

    C# IO流的操作非常重要,我们读写文件都会使用到这个技术,这里先演示一个文件内容复制的例子,简要说明C#中的IO操作. namespace ConsoleApplication1 { class P ...

  6. 【7】了解Bootstrap栅格系统基础案例(2)

    ps.这一次要说的是“Responsive column resets”,但是不知道为什么中文官网没有给出翻译,但是在看到案例的时候,感觉这就像一个bug,我自己姑且叫这个是一个高度bug吧,方便自己 ...

  7. 利用数据库链做DML操作时报ORA-02069: global_names parameter must be set to TRUE for this operation

    按照 http://space.itpub.net/195110/viewspace-711110 的说法顺利解决问题. 通过DBLink更新远程数据的时候,如果使用到本地的sequence.函数.过 ...

  8. Codeforces Round #345 (Div. 1) B. Image Preview

    Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed ...

  9. python 脚本

    mag3.py 1,import import sys from org.eclipse.jface.dialogs import MessageDialogfrom org.eclipse.core ...

  10. 一步步学习NHibernate(1)——NHibernate介绍

    请注明转载地址:http://www.cnblogs.com/arhat 第十五章 从本章开始,老魏将给大家一起学习NHibernate这个流行的ORM框架,本来老魏想要和大家一起探讨微软的EF框架的 ...