poi 3061 尺取例题1
题目传送门/res tp poj
白书题
尺取法例题
#include<iostream>
#include<algorithm>
using namespace std;
const int L = 100100;
typedef long long ll;
ll a[L],sum[L],s;
int n,T;
int getans(){
int ans = n+1;
int fr=1,re=1;
while(fr<=re){
while(sum[re]-sum[fr-1] <s){
re++;if(re == n+1)return ans;
}
while(sum[re]-sum[fr-1] >=s){
ans = min(ans,re-fr+1);fr++;
}
}
return ans;
}
int main(){
cin>>T;
while(T--){
cin>>n>>s;
for(int i = 1;i<=n;++i) cin>>a[i];
for(int i = 1;i<=n;++i)sum[i]=sum[i-1]+a[i];
int ans = getans();
if(ans == n+1) cout<<0<<endl;
else cout<<ans<<endl;
}
}