LA2678

来源:互联网 发布:mac的桌面 编辑:程序博客网 时间:2024/05/01 14:41

题目大意:
求大于S的最短连续子序列的长度

代码:

#include <iostream>using namespace std;#include <stdio.h>#include <cstring>int A[100005];int B[100005];int main() {    int n,S;    while(~scanf("%d %d",&n,&S)) {        B[0] = 0;        for(int i = 1; i <= n; i++)            scanf("%d",&A[i]);        for(int i = 1; i <= n; i++) {            B[i] = B[i - 1] + A[i];        }        int ans = n + 1;        int i = 1;        for(int j = 1; j <= n; j++) {            if(B[j] - B[i - 1] < S) continue;            while(B[j] - B[i] >= S) i++;            ans = min(ans,j - i + 1);        }        printf("%d\n",ans == n +1?0:ans);    }}
0 0
原创粉丝点击