poj3061(尺取法)

Subsequence

题意:

  给出一个序列,要求找出一个长度最短的连续子区间,满足区间上所有数之和大于等于S,输出这个最短长度。

分析:

  枚举每个点为左端点,用尺取法找到其右端点,取n次结果中的最小值就好了。

代码:

#include <stack>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std;
#define ll long long
#define ull unsigned long long
#define cls(x) memset(x,0,sizeof(x))
#define clslow(x) memset(x,-1,sizeof(x))

const int maxn=1e5+100;

int n,T,S;

int a[maxn];

int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&S);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }

        int s=1,e=1;
        int ans=n+1,sum=0;
        while(true)
        {
            while(e<=n&&sum<S)  sum+=a[e++];
            if(sum<S)   break;
            ans=min(ans,e-s);
            sum-=a[s++];
        }
        if(ans==n+1)    ans=0;
        printf("%d\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/shutdown113/p/9384612.html

时间: 2024-08-01 12:02:36

poj3061(尺取法)的相关文章

POJ3061 尺取法

题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增加,若满足条件,则逐渐减少尺子头部直到不满足条件为止,保存 尺子长度的最小值(尾部-头部+1)即可. 理论上累计区间和+二分查找的暴力也能过. 代码如下: #include <stdio.h> #include <algorithm> #include <string.h>

poj3061尺取法

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 sequen

poj3061 Subsequence&amp;&amp;poj3320 Jessica&#39;s Reading Problem(尺取法)

这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针(t)要么不动要么也往前走.满足这种特点的就可以考虑尺取法. poj3061 比较简单,也可以用二分做,时间复杂度O(n*logn).用尺取法可以O(n)解决. #include<iostream> #include<cstdio> #include<cstdlib> #i

尺取法 poj3061 poj3320

尺取法就是反复推进区间的开头和结尾,来求满足条件的最下区间. poj3061 http://poj.org/problem?id=3061 给定一个都是正整数的序列,要我们求总和不小于S的连续子序列的长度的最小值 如果序列   是总和最迟大于S的连续子序列 那么  所以只有加上, 从开始的连续子序列才有可能大于S 所以从开始的总和最初大于S的连续子序列是则一定有 1 #include <stdio.h> 2 #include <string.h> 3 #include <st

【尺取法】POJ3061 &amp; POJ3320

POJ3061-Subsequence [题目大意] 给定长度微n的数列整数及整数s.求出总和不小于s的连续子序列的长度的最小值.如果节不存在,则输出0. [思路] 尺取法五分钟裸裸裸~刷水刷出了罪恶感:( 基本做法:设置l和r代表当前区间[l,r],若S(l,r)<s,则 r++.若S(l,r)≥s,则 l++,直至S(l,r)<s.如果当前S(l,r)<s且r=n则退出.输出最小区间长度[l,r]即可. 1 #include<iostream> 2 #include<

POJ3061 Subsequence(二分前缀和法+尺取法)

二分+前缀和法 满足条件的子序列长度在(0,n)之间,sum[x+i]-sum[i]为从从第i个元素开始序列长度为x的元素的和.前缀和可在O(n)的时间内统计 sum[i]的值.再用二分找出满足条件的最小的子序列长度. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #

poj3061 Subsequence ,尺取法

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 sequen

poj3061(Subsequence)尺取法

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 o

【转】毛虫算法&mdash;&mdash;尺取法

转自http://www.myexception.cn/program/1839999.html 妹子满分~~~~ 毛毛虫算法--尺取法 有这么一类问题,需要在给的一组数据中找到不大于某一个上限的"最优连续子序列" 于是就有了这样一种方法,找这个子序列的过程很像毛毛虫爬行方式,我管它叫毛毛虫算法,比较流行的叫法是"尺取法". 喏,就像图里的妹纸一样~ 还是举个栗子: Poj3061 给长度为n的数组和一个整数m,求总和不小于m的连续子序列的最小长度 输入 n = 1