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) 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值,当sum < s 时程序结束。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 100010;
typedef long long ll;
int t,n,ss;
int s[maxn];
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&ss);
        for(int i =0;i<n;i++) scanf("%d",s+i);
        int l=0,r=0,sum=0,res=maxn;
        while(1){
            while(r<n&&sum<ss){
                sum+=s[r++];
            }
            if(sum<ss) break;
            res=min(res,r-l);
            sum-=s[l++];
        }
        if(res==maxn) printf("0\n");
        else  printf("%d\n",res);
    }
}
时间: 2024-10-12 11:44:33

Subsequence 尺取法的相关文章

[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)尺取法

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

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

POJ 3061  Subsequence   尺取法   挑战146页

---恢复内容开始--- Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10487   Accepted: 4337 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 < 10

POJ 3061 Subsequence(尺取法)

传送门 Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11284 Accepted: 4694 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