It's obvious that n-side complexity can't get through.
Consider the optimization of the most value of the query, you can consider using a heap or a monotonous queue to do.
Heap:
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5 using namespacestd;6 7 Const intINF =99999999;8 Const intN =32768;9 intA[n];Ten intN, l, U; One A structNode - { - intl; the ints; - Node () {} -Node (int_l,int_s) - { +L =_l; -s =_s; + } A BOOL operator< (ConstNode & T)Const at { - returns <T.s; - } - }; - - intSolve () in { -Priority_queue<node>Q; to intAns =INF; + for(inti = l; I <= N; i++ ) - { theQ.push (Node (i-l, A[i-l])); * while(Q.top (). L + U <i) Q.pop (); $ intTMP = A[i]-Q.top (). s;Panax Notoginseng if(tmp < ans) ans =tmp; - } the returnans; + } A the intMain () + { - while(SCANF ("%d", &N), N) $ { $scanf"%d%d", &l, &u); -a[0] =0; - for(inti =1; I <= N; i++ ) the { -scanf"%d", A +i);WuyiA[i] + = a[i-1]; the } -printf"%d\n", Solve ()); Wu } - return 0; About}
Monotonic queue:
Bnuoj 27411 heap or monotonic queue maintenance interval RMQ