POJ 3061 Subsequence

其实做这个题是为了解决一道最近比赛做到的题,但是吧来写博客的时候我突然想到,自己以前可能做过这个题,
查了一下POJ的提交记录,我果然在三年前交过这个题 滑动窗口板子题
在这里插入图片描述

Subsequence

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 32899 Accepted: 13687
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

最气人的是我翻了一下,三年前交的代码,自己没看懂 ,看了一会才明白,啊啊啊啊啊啊,那时候的代码是又丑又菜

#include 
#include 
#include 
#define Maxn 100005
#define LL long long
using namespace std;
//  之前叫啥啥window来着   中文名就是 滑动窗口
int a[Maxn];
LL dp[Maxn];
const int INF = 0x3f3f3f3f;
int main(int argc,char* argv[]) {
    int T,n; LL S;
    scanf("%d",&T);
    while(T--) {
        scanf("%d %lld",&n,&S);
        for(int i=1; i<=n; i++) scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=n; i++)
            dp[i] = dp[i - 1] + a[i];
        int Ans = INF,i = 1;
        for(int j=1; j<=n; j++) {
            if(dp[j] - dp[i - 1] < S) continue;
            while(dp[j] - dp[i] >= S) i++;// 表明i去掉也能大于S
            Ans = min(Ans,j - i + 1);
        }
        if(Ans == INF) printf("0\n");
        else printf("%d\n",Ans);
    }

    return 0;
}

你可能感兴趣的:(ACM,数据结构)