Subsequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14033 Accepted: 5936
Description
A sequence of N positive integers (10 < N < 100000), eachof them less than or equal 10000, anda positive integer S (S < 100000000) are given. Write a program to find the minimal lengthofthe subsequence of consecutive elements ofthe sequence, thesumof which is greater than or equal to S.
Input
The firstline is thenumberof test cases. For each test casethe program has toreadthe numbers N and S, separated byan interval, fromthefirstline. The numbers ofthe sequence are given inthesecondlineofthe test case, separated by intervals. The input will finish withtheendoffile.
Output
For eachthecasethe program has to print theresultonseparatelineoftheoutputfile.ifnoanswer, print0.
Sample Input
210155135107492851112345
Sample Output
23
Source
Southeastern Europe 2006
先求出前缀和,然后跑遍尺取法就好了。
#include<iostream>#include<cstring>#include<cstdlib>#include<algorithm>#include<cctype>#include<cmath>#include<ctime>#include<string>#include<stack>#include<deque>#include<queue>#include<list>#include<set>#include<map>#include<cstdio>#include<limits.h>#define MOD 1000000007#define fir first#define sec second#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)#define mes(x, m) memset(x, m, sizeof(x))#define Pii pair<int, int>#define Pll pair<ll, ll>#define INF 1e9+7#define inf 0x3f3f3f3f#define Pi 4.0*atan(1.0)#define lowbit(x) (x&(-x))#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedeflonglong ll;
typedefunsignedlonglong ull;
constdouble eps = 1e-9;
constint maxn = 1e5+5;
usingnamespacestd;
inlineint read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
int sum[maxn];
int k[maxn];
int main(){
// fin;int Case=read();
while(Case--){
int n,m;
mes(sum,0);
n=read(),m=read();
for(int i=1;i<=n;++i){
k[i]=read();
sum[i]=sum[i-1]+k[i];
}
int t=1,s=1,res=inf,re=0;
for(;;){
while(t<=n+1&&re<m){
re=sum[t]-sum[s-1];
++t;
}
if(re<m){
break;
}
res=min(res,t-s);
re-=k[s];
++s;
}
if(res!=inf){
printf("%d\n",res);
}else{
puts("0");
}
}
return0;
}
SubsequenceTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14033 Accepted: 5936DescriptionA sequence of N positive integers (10 < N < 100 000), each of them less than or equal