poj 3061 Subsequence
题意:n个数,求最短的子序列和>=s
分析:尺取法,也叫蠕虫法,每次枚举起点,然后往右移动,找到右边界
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> using namespace std; const int maxn=1e5+5; int c[maxn]; int main(){ int t,n,s; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&s); for(int i=0;i<n;i++) scanf("%d",c+i); int ans=n+1; int l=0,r=0,sum=0; for(;;){ while(r<n&&sum<s) sum+=c[r++]; if(sum<s)break; ans=min(ans,r-l); sum-=c[l++]; } if(ans==n+1) puts("0"); else printf("%d\n",ans); } return 0; }