Title Address: http://poj.org/problem?id=3061
Method One: Because the elements are greater than 0, so maintain the prefix and sum[i], there must be Sum[k]>sum[i] (k>i). This way the sequence beginning S is determined, with a binary lookup to determine the minimum value of the end T that makes the sequence and is not less than S.
Time complexity O (NLOGN).
1#include <cstdio>2#include <iostream>3#include <string.h>4#include <algorithm>5#include <math.h>6#include <stdbool.h>7#include <time.h>8#include <stdlib.h>9#include <map>Ten#include <stack> One#include <queue> A#include <vector> - using namespacestd; - #defineCLR (x, y) memset (x,y,sizeof (×)) the #defineSQR (x) ((x) * (x)) - #defineRep (i,a,b) for (int i= (a); i<= (b); i++) - #defineLL Long Long - #defineINF 0x3f3f3f3f + #defineA First - #defineB Second + Const intn=1e5+131; A intN,s,a[n],sum[n]; at - voidInit () - { -CLR (A,0); -CLR (SUM,0); - } in voidSolve () - { to intCAs; + -scanf"%d",&CAs); the while(cas--){ * init (); $scanf"%d%d",&n,&s);Panax Notoginseng for(intI=0; i<n;i++) { -scanf"%d",&a[i]); thesum[i+1]=sum[i]+A[i]; + } A intres=n+1; the for(intk=0; sum[k]+s<=sum[n];k++){ + intP=lower_bound (Sum+k,sum+n,sum[k]+s)-sum; -Res=min (res,p-k); $ } $ if(res>N) { -Puts"0"); - Continue; the } -printf"%d\n", res);Wuyi } the - } Wu - intMain () About { $ solve (); - return 0; -}
Method Two: Ruler methods. Time complexity O (n).
1#include <cstdio>2#include <iostream>3#include <string.h>4#include <algorithm>5#include <math.h>6#include <stdbool.h>7#include <time.h>8#include <stdlib.h>9#include <map>Ten#include <stack> One#include <queue> A#include <vector> - using namespacestd; - #defineCLR (x, y) memset (x,y,sizeof (×)) the #defineSQR (x) ((x) * (x)) - #defineRep (i,a,b) for (int i= (a); i<= (b); i++) - #defineLL Long Long - #defineINF 0x3f3f3f3f + #defineA First - #defineB Second + Const intn=1e5+131; A intN,s,a[n]; at - - voidSolve () - { - intCAs; - inscanf"%d",&CAs); - while(cas--) { toscanf"%d%d",&n,&s); + for(intI=0; i<n;i++) { -scanf"%d",&A[i]); the } * $ intt=0, sum=0, h=0, res=n+1;Panax Notoginseng - while(true) { the while(T<n && sum<s) { +sum+=a[t++]; A } the if(Sum<s) Break; +Res=min (res,t-h); -sum-=a[h++]; $ } $ if(res>N) { -Puts"0"); - Continue; the } -printf"%d\n", res);Wuyi } the } - Wu intMain () - { About solve (); $ - return 0; -}
[POJ] 3061 subsequence (ruler)