给定一个长为N的序列和s,求一个最小的子串使得sum(子序列)>= s;
思路:Twopoint,r维护加入,l维护减除,r-l+1就是这个结果,还要判断下是否存在这样的串。
int a[100001];
int n, s;
int main(int argc, const char * argv[])
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&s) == 2)
{
int sum = 0;
int tail = 0;
int len = INF;
for (int i = 1;i <= n;++i)
{
scanf("%d", &a[i]);
sum += a[i];
while(sum >= s && tail < i)
{
sum -= a[++tail];
len = min(len, i - tail + 1);
}
}
if (len == INF) cout << 0 << endl;
else cout << len << endl;
}
return 0;
}