# UVALive - 2678 Subsequence (尺取法)

123人阅读 评论(0)

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 nd the minimal length of the
subsequence of consecutive elements of the sequence, the sum of which is greater than or equal to
S
.
Input
Many test cases will be given. For each test case the program has to read the numbers
N
and
S
,
separated by an interval, from the rst line. The numbers of the sequence are given in the second line
of the test case, separated by intervals. The input will nish with the end of le.
Output
For each the case the program has to print the result on separate line of the output le.
SampleInput
10 15
5 1 3 5 10 7 4 9 2 8
5 11
1 2 3 4 5
SampleOutput
2
3

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
#include<algorithm>

const int maxn=1e5+5;
long long a[maxn];

int main(){
int n;
long long s;
while(~scanf("%d%lld",&n,&s)){
for(int i=1;i<=n;++i){
scanf("%lld",&a[i]);
}
int slow=1,fast=1,ans=n;
long long sum=0;
while(true){
while(fast<=n && sum<s){
sum+=a[fast];
++fast;
}
if(sum>=s)
ans=min(ans,fast-slow);
sum-=a[slow];
++slow;
if(slow>n)break;
}
printf("%d\n",ans==n?0:ans);
}
return 0;
}

1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：85302次
• 积分：4524
• 等级：
• 排名：第6902名
• 原创：363篇
• 转载：6篇
• 译文：0篇
• 评论：31条
博客专栏
 博弈论入门 文章：17篇 阅读：7131
 湖南省第九届省赛 文章：7篇 阅读：3183
评论排行
最新评论