您的位置:首页 > 其它

LA2678

2016-01-25 11:50 134 查看
题目大意:

求大于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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: