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 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

比较简单的题,二分也可以,但是尺取法更加高效

/*************************************************************************
    > File Name: POJ3061.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年03月30日 星期一 14时40分27秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 100100;
int sum[N];
int arr[N];

int main()
{
    int t;
    int n, s;
    scanf("%d", &t);
    while (t--)
    {
        sum[0] = 0;
        int ans = inf;
        scanf("%d%d", &n, &s);
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d", &arr[i]);
            sum[i] = sum[i - 1] + arr[i];
        }
        int i = 1, j = 1;
        while (i <= n && j <= n)
        {
            while (j <= n && sum[j] - sum[i - 1] < s)
            {
                ++j;
            }
            if (j <= n)
            {
                ans = min(ans, j - i + 1);
                ++i;
            }
            else
            {
                break;
            }
        }
        if (ans == inf)
        {
            ans = 0;
        }
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-27 18:16:51

POJ3061---Subsequence(尺取法)的相关文章

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

POJ3061 Subsequence 尺取or二分

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

[ACM] POJ 3061 Subsequence (尺取法)

Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8403   Accepted: 3264 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

poj3061(尺取法)

Subsequence 题意: 给出一个序列,要求找出一个长度最短的连续子区间,满足区间上所有数之和大于等于S,输出这个最短长度. 分析: 枚举每个点为左端点,用尺取法找到其右端点,取n次结果中的最小值就好了. 代码: #include <stack> #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace st

[ACM] CSU 1553 Good subsequence(尺取法)

题目地址:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1553 给定n的数的序列,求最长连续区间满足区间内的数最大值与最小值的差<=k (尺取法) const int maxn=10010; int num[maxn]; int n,k; int MIN,MAX; int main() { while(scanf("%d%d",&n,&k)!=EOF) { for(int i=1;i<=n;i++) sc

POJ 3061 Subsequence(尺取法)

题目链接:http://poj.org/problem?id=3061 题意:给定长度为n的数列整数,以及整数S,求出总和不少于S的连续子序列的长度的最小值.如果解不存在,则输出0. 尺取法:通常是指对数组保存一对下标(起点,终点),然后根据实际情况交替推进两个端点直到解决问题的方法,这个操作很像尺蠼虫故得名. 思路:所以可以先初始化起点s,终点g,再一步一步推进,直到sum>S,然后记录此时的序列长度,再推进s,sum-=num[s],再记录长度,直到sum<S,再推进g,这样的方法,s和g

编程题-最短序列和(Subsequence)-尺取法

题目: 给定长度为n的整数数列 a0,a1,...,an?1以及整数S,求出总和不小于S的连续自序列的长度最小值.如果不存在,则输出0 样例: 输入 n = 10 S = 15 a = {5 , 1,3 ,5 ,10,7,4,9,2,8} 输出 2 (5 ,10) 思路: 尺取法通常的是保留数组的一对下标(开始到结束),然后根据实际情况交替移动. 我们假设从i开始总和超过S的连续子序列如果为ai,ai+1...ai+j 即 ai+ai+1+...+ai+j≥S 并且 ai+ai+1+...+ai

Subsequence 尺取法

Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12304   Accepted: 5165 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) ar

POJ 3061 Subsequence 尺取法,一个屌屌的O(n)算法

Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9050   Accepted: 3604 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

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