Bnuoj 27411 heap or monotonic queue maintenance interval RMQ

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.