Algorithm:
At a glance, this question is like a binary question. In fact, it does not satisfy the binary nature.
If S is 29 and the enumeration length starts from 1 is 2, it is not satisfied, but the maximum length is 3 .. 1 2 20 10 10 3
The correct algorithm is to use L [I] to record the maximum I expansion length to left, and R [I] to record the maximum I expansion length to right ..
/** Honi, zadatak dvoniz * Autor: Goran zuzic ** sluzbeno rjesenje, trebalo Bi dobiti SVE bodove. ukupna slozenost O (n lg n ). **/# include <algorithm> # include <functional> # include <ctime> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstring> # include <set> # include <vector> # include <string> using namespace STD; const int maxn = 100100; int N, S; int A [maxn]; int L [maxn], R [maxn]; vector <in T> ubaci [maxn]; int main (void) {While (scanf ("% d", & N, & S )! = EOF) {for (INT I = 0; I <n; ++ I) {scanf ("% d", A + I);} int A = 0, B = 0; int Suma = 0; while (A <n) {If (B <n & Suma + A [B] <= s) {Suma + = A [B ++];} else {R [a] = B-A; Suma-= A [A ++] ;}} printf ("sum = % d \ n", Suma); a = B = n-1; while (A> = 0) {If (B> = 0 & Suma + A [B] <= s) {Suma + = A [B --];} else {L [a] = A-B; Suma-= A [A --] ;}} for (INT I = 0; I + 1 <n; ++ I) {int val = min (L [I], R [I + 1] ); Printf ("% d \ n", Val, I-Val + 1, I); ubaci [I-Val + 1]. push_back (I); // start point of expansion, to k} Multiset <int> PQ; For (INT I = 0; I <n; ++ I) {While (! PQ. empty () & * PQ. begin () <I) PQ. erase (PQ. begin (); For (vector <int>: iterator it = ubaci [I]. begin (); it! = Ubaci [I]. end (); ++ it) {PQ. insert (* It); printf ("I = % d \ n", I, * It);} ubaci [I]. clear (); If (! PQ. empty () printf ("% d max = % d \ n", (* PQ. rbegin ()-I + 1) * 2, * PQ. rbegin (); else printf ("0 \ n") ;}} return (0-0 );}