题面
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.
题意
给你一个序列 W (
Wi>0 ),求出 r−l+1 最小的满足 ∑ri=lWi≥S 的子序列
解法
尺取法:
尺取法是一种很使用的技巧,在 ACM 赛事中经常用到,这里推荐一篇博客,里面有很多例题的讲解,包括本题:http://blog.csdn.net/consciousman/article/details/52348439
我们考虑对于 【l,r】 区间,如果 ∑ri=lWi 已经满足条件了,那么对于 r<R ,显然有 ∑Ri=lWi>S ,所以我们实现预处理好序列的前缀和 sum[] ,然后对于一个起点 l ,二分找到最小的满足sumx−suml−1≥S 的 x ,答案就是最小的x−l+1 ,复杂度O( nlogn )
我们可以发现,符合条件的区间的左端点一定是单调递增的,所以,当区间和小于S时右端点向右移动,和大于等于S时,左端点向右移动以进一步找到最短的区间,如果右端点移动到区间末尾其和还不大于等于S,结束区间的枚举,复杂度O( n )
复杂度
O(
nlogn )或O( n )
代码
O(
nlogn ):
#include<iostream>
#include<cstdlib>
#include<cstdio>
#define Lint long long int
using namespace std;
const int MAXN=100010;
int w[MAXN];
int n,S;
int ans;
int get(int L)
{
int l=L,r=n;
while( l<=r )
{
int mid=(l+r)/2;
if( w[mid]-w[L-1]>S ) r=mid-1;
else
if( w[mid]-w[L-1]<S ) l=mid+1;
else { l=mid;break ; }
}
if( l>n ) return n+1;
else return l-L+1;
}
int main()
{
while( scanf("%d%d",&n,&S)!=EOF )
{
ans=n+1;
for(int i=1,x;i<=n;i++) scanf("%d",&x),w[i]=w[i-1]+x;
for(int i=1;i<=n;i++) ans=min( ans,get( i ) );
printf("%d\n",ans==n+1 ? 0 : ans );
}
return 0;
}
O( n <script type="math/tex" id="MathJax-Element-20">n</script>):
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 100005
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
LL a[100010];
int n, t, ans = INF;
LL sum, s;
int main()
{
scanf("%d", &t);
while (t--){
scanf("%d %I64d", &n, &s);
for (int i = 0; i < n; i++) scanf("%I64d", a+i);
int st = 0, en = 0;
ans = INF; sum = 0;
while (1){
while (en<n && sum<s) sum += a[en++];
if (sum < s) break;
ans = min(ans, en-st);
sum -= a[st++];
}
if (ans == INF) ans = 0;
printf("%d\n", ans);
}
return 0;
}