尺取法板子
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int maxn=100000+10; int a[maxn]; int t,n,S; int chiqu() { int t=0,s=0,sum=0; int ans=n+1; while(1) { while(t<n&&sum<S) { sum+=a[t++]; } if(sum<S) break; ans=min(ans,t-s); sum-=a[s++]; } if(ans>n) ans=0; return ans; } int main() { scanf("%d",&t); while(t--) { scanf("%d%d",&n,&S); for(int i=0;i<n;i++) scanf("%d",&a[i]); printf("%d\n",chiqu()); } return 0; }
时间: 2024-11-03 21:36:05