Test instructions: gives n integers, given integer s, to find the shortest continuous sequence, so that their sum is greater than or equal to s
The first is to ask for a prefix and then use Lower_bound to find
The second type, the ruler,
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A -typedefLong LongLL; - Const intINF = (1<< -)-1; the Const intMod=1000000007; - Const intmaxn=100005; - - intn,s; + intA[MAXN],SUM[MAXN]; - + intMain () { A while(SCANF ("%d%d", &n,&s)! =EOF) { atMemset (A,0,sizeof(a)); -memset (SUM,0,sizeof(sum)); - for(inti =0; i < n;i++) scanf ("%d",&a[i]); -sum[0] = a[0]; - for(inti =1; i < n;i++) Sum[i] = sum[i-1] +A[i]; - in //for (int i =0;i <= n;i++) - //printf ("sum[%d] =%d\n", i,sum[i]); to if(sum[n-1] <S) { +printf"0\n"); - } the Else{ * intres =INF; $ for(ints =0; Sum[s] + s <= sum[n-1];s++){Panax Notoginseng intt = lower_bound (sum,sum + n,sum[s] + s)-sum; -res = min (res,t-s); the //printf ("res =%d\n", res); + } Aprintf"%d\n", res); the } + } - return 0; $}
View Code
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A -typedefLong LongLL; - Const intINF = (1<< -)-1; the Const intMod=1000000007; - Const intmaxn=100005; - - intA[MAXN]; + intn,s; - + voidsolve () { A intt =0, S =0; at intres =INF; - intsum =0; - for(;;) { - while(t < n && Sum <S) { -Sum + = a[t++]; - } in if(Sum < S) Break; -res = min (res,t-s); toSum-= a[s++]; + } - if(Res > N) res =0; theprintf"%d\n", res); * } $ Panax Notoginseng intMain () { - while(SCANF ("%d%d", &n,&s)! =EOF) { theMemset (A,0,sizeof(a)); + for(inti =0; i < n;i++) scanf ("%d",&a[i]); A solve (); the } + return 0; -}
View Code
LA 2678 subsequence