Title Link: http://poj.org/problem?id=3061
Test instructions: to a sequence of N and an integer s, to find a continuous sub-sequence, so that the length of the sub-sequence is the shortest and not less than the integer s.
statistics [1~i] of the subsequence and sum (i), (sum (0) =0). The sum (j)-sum (i-1) (i > 0) is then calculated for an interval [i,j].
Because there are no negative numbers for a given sequence, sum is a strictly non-decreasing sequence.
To the problem of minimizing the maximum value, you can double-enumerate the length of the sequence, and compute the and of the sub-sequence [i-1,i+m-1] on the prefix and. If there is one that satisfies the subsequence and ≥s, the length of the sequence is reduced and the current value is noted, and vice versa. The complexity is O (NLGN).
1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <bitset> One#include <vector> A#include <deque> -#include <queue> -#include <stack> the#include <ctime> -#include <Set> -#include <map> -#include <cmath> + - using namespacestd; + A Const intMAXN =100010; at intN, S; - intX[MAXN]; - intSUM[MAXN]; - - BOOLOkintmm) { - for(inti =1; I <= n-mm +1; i++) { in //printf ("%d%d\n", i-1, i+mm-1); - if(sum[i+mm-1]-sum[i-1] >= s)return 1; to } + return 0; - } the * intMain () { $ //freopen ("in", "R", stdin);Panax Notoginseng intT; -scanf"%d", &T); the while(t--) { +memset (SUM,0,sizeof(sum)); Ascanf"%d%d", &n, &s); the for(inti =1; I <= N; i++) { +scanf"%d", &x[i]); -Sum[i] = sum[i-1] +X[i]; $ } $ if(Sum[n] <s) { -printf"0\n"); - Continue; the } - intans;Wuyi intLL =0; the intRR =N; - while(LL <=RR) { Wu intMM = (ll + RR) >>1; - if(ok (mm)) { AboutAns =mm; $rr = mm-1; - } - Elsell = mm +1; - } Aprintf"%d\n", ans); + } the}
This problem can also be used as a ruler to maintain two pointers from left to right scanning the stored sequence, the complexity of O (n).
1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <bitset> One#include <vector> A#include <deque> -#include <queue> -#include <stack> the#include <ctime> -#include <Set> -#include <map> -#include <cmath> + - using namespacestd; + A Const intMAXN =100010; at intN, S; - intX[MAXN]; - - intMain () { - //freopen ("in", "R", stdin); - intT; inscanf"%d", &T); - while(t--) { toscanf"%d%d", &n, &s); + intsum =0; - for(inti =1; I <= N; i++) { thescanf"%d", &x[i]); *Sum + =X[i]; $ }Panax Notoginseng if(Sum <s) { -printf"0\n"); the Continue; + } A intAns =0x7f7f7f; the intLL =1; + intRR =1; -sum =0; $ while(1) { $ while(RR <= n && sum <=s) { -Sum + = x[rr++]; - } the if(Sum < s) Break; -ans = min (ans, rr-ll);WuyiSum-= x[ll++]; the } -printf"%d\n", ans); Wu } -}
[poj3061]subsequence (binary, prefix, and)