您的位置:首页 > 其它

poj3061 尺取法 <挑战程序设计竞赛>

2018-02-06 18:40 211 查看
2018-1-6

直接使用尺取法求解即可,需要注意的是:如果说所有数的和都不能满足条件的话,那么就直接输出0即可。

尺取法的头部每次增加1,尾巴每次增加到满足条件即可。

#include<iostream>
#include<cstdio>
using namespace std;

const int N = 100000;
int x[N+1];
int t,n,s;

void res(){
int now=x[1],j=2,mn=N;
for (int i=1;i<=n;i++){
now-=x[i-1];
while (now<s&&j<=n){
now+=x[j++];
}
if (now<s) break;
mn=min(mn,j-i);
}
cout<<mn<<endl;
}

int main(){
while (cin>>t){
while (t--){
int ss=0;
cin>>n>>s;
for (int i=1;i<=n;i++){
scanf ("%d",&x[i]);
ss+=x[i];
}
if (ss<s){
cout<<0<<endl;
}else{
res();
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 尺取法