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

Southeastern Europe 2006

t题目要求找一个区间和大于S切长度最小的区间

从头到尾扫一遍

如果当前和小于s 尾指针加1

如果当前和大于等于s 删除第一个元素

 1 #include <cctype>
 2 #include <cstdio>
 3
 4 const int INF=0x3f3f3f3f;
 5 const int MAXN=100010;
 6
 7 int T,n,s,ans;
 8
 9 int a[MAXN];
10
11 inline void read(int&x) {
12     int f=1;register char c=getchar();
13     for(x=0;!isdigit(c);c==‘-‘&&(f=-1),c=getchar());
14     for(;isdigit(c);x=x*10+c-48,c=getchar());
15     x=x*f;
16 }
17
18 int hh() {
19     read(T);
20     while(T--) {
21         read(n);read(s);
22         int sum=0;
23         ans=INF;
24         for(int i=1;i<=n;++i) read(a[i]),sum+=a[i];
25         if(s>sum) {printf("0\n");continue;}
26         int head=1,tail=1;
27         sum=a[1];
28         while(head<=tail) {
29             if(sum<s&&tail<n) sum+=a[++tail];
30             else sum-=a[head++];
31             if(sum>=s)
32               if(ans>tail-head+1) ans=tail-head+1;
33         }
34         printf("%d\n",ans);
35     }
36     return 0;
37 }
38
39 int sb=hh();
40 int main(int argc,char**argv) {;}

代码

时间: 2024-08-10 10:02:55

POJ 3061 Subsequence的相关文章

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

POJ 3061 Subsequence(Two Pointers)

[题目链接] http://poj.org/problem?id=3061 [题目大意] 给出S和一个长度为n的数列,问最短大于等于S的子区间的长度. [题解] 利用双指针获取每一个恰好大于等于S的子区间,更新答案即可. [代码] #include <cstdio> int T,a[100005]; int main(){ scanf("%d",&T); while(T--){ int n,S,s,h,t,ans; scanf("%d%d",&a

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

从具体题目来谈尺取法 POJ 3061 Subsequence

题目链接:http://poj.org/problem?id=3061 先说说最朴素的算法,那就是for嵌套了,复杂度是O(n^3)太慢,想都不用想一定会超时.接下来有的人可能会想到二分搜索的思想,将时间复杂度优化成O(n*logn),我试了一下,可以AC. 但是这都不是今天要说的重点,今天要说的是一个ACM比赛中常用的技巧方法——尺取法.这是一种可以直接将时间复杂度优化到O(n)的算法. 我们先来介绍一下尺取法.尺取法,顾名思义,像尺子一样,一块一块的截取.是不是解释的有点让人纳闷-..没关系

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

POJ 3061 Subsequence 尺取

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

[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

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

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