题意:给你一个序列 求一个最短连续序列的和大于S
思路: 连续序列 很容易想到双指针 注意没有答案的情况 输出 0!!! 然后就是一个裸的双指针
ACcode:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 100005
int a[N];
int main()
{
int n, k, t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d",&n,&k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int l = 1, r = 1, cnt = 0,ans = 1e9+7;
while (1)
{
while (r <= n && cnt < k)
{
cnt+= a[r];
r++;
}
if (cnt < k)
break;
if (r - l < ans)
ans = r - l;
cnt -= a[l];
l++;
}
if (ans == 1e9 + 7)
ans = 0;
printf("%d\n", ans);
}
return 0;
}