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",&n,&S);ans=n;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1,j=1;i<=n;i++){
            s+=a[i];
            if(s>=S){
                ans=min(ans,i-j);
                s-=a[j++];
            }
        }printf("%d\n",ans);
    }return 0;
}

  

时间: 2025-01-12 17:01:51

POJ 3061 Subsequence(Two Pointers)的相关文章

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(最短子序列和大于等于某值的序列长度)

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

Subsequence poj 3061 二分(nlog n)或尺取法(n)

Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9236   Accepted: 3701 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(水题)

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

poj 1384 Piggy-Bank(完全背包)

http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7900 Accepted: 3813 Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income f

POJ - 1006 Biorhythms (中国剩余定理)

Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respec

Poj 1182种类(带权)并查集

题目链接 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44316 Accepted: 12934 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是

poj 2431 Expedition (贪心+优先队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6890   Accepted: 2065 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to