[LG1886] sliding window monotone queue

Source: Internet
Author: User

~ ~ ~ Noodles ~ ~ ~

Exercises

To observe the data range, this should be a complex O (n) problem. Take the maximum as an example, consider a monotonic queue and maintain a monotonically decreasing queue. From the previous sweep, each answer to take the team first, if the back into the larger than the front, then the number of pop-up, because it is backward sweep, so the back into if larger than the previous, then must be better, because the elimination of the first must be eliminated. If the first team is not already in the current window, then it pops up until it's legal.

An important principle when maintaining a monotone queue is that the element that "squeezes" the other person must be better than the element being squeezed out, otherwise it may not be possible to find a legal case or miss the optimal solution. Note that this is a good understanding.

The minimum value is the inverse of the maximum value.

Don't know why I used to write code so ugly,,,, re-write a good.

1#include <bits/stdc++.h>2 using namespacestd;3 #defineR Register int4 #defineAC 10010005 6 intN, K, head, tail;7 intS[ac];8 structnode{9     intx, id;Ten }q[ac]; One  AInlineintRead () - { -     intx =0;Charc = GetChar ();BOOLz =false; the      while(C >'9'|| C <'0') -     { -         if(c = ='-') Z =true; -c =GetChar (); +     } -      while(c >='0'&& C <='9') x = x *Ten+ C-'0', C =GetChar (); +     if(!z)returnx; A     Else return-x; at } -  - voidPre () - { -n = Read (), k =read (); -      for(R i =1; I <= N; i + +) s[i] =read (); in } -  to voidWork1 () + { -Head =1, tail =0; the      for(R i =1; I <= N; i + +) *     { $          while(Head <= tail && q[head].id <= i-k) + +head;Panax Notoginseng          while(S[i] < q[tail].x && Head <= tail)--tail;//It can be deleted, but the fortress comes in anyway. -Q[++tail] =(node) {s[i], i}; the         if(i >= k) printf ("%d", q[head].x); +     }     Aprintf"\ n");  the } +  - voidWork2 () $ { $Head =1, tail =0; -      for(R i =1; I <= N; i + +) -     { the          while(Head <= tail && q[head].id <= i-k) + + head;//,,, can be deleted before the end . -          while(S[i] > q[tail].x && Head <= tail)--tail;//It can be deleted, but the fortress comes in anyway.WuyiQ[++tail] =(node) {s[i], i}; the         if(i >= k) printf ("%d", q[head].x); -     }     Wuprintf"\ n"); - } About  $ intMain () - { -Freopen ("in.in","R", stdin); - pre (); A Work1 (); + Work2 (); the fclose (stdin); -     return 0; $}
View Code

Of course, if you like the short code and don't care about the constant problem, you can also write:

1#include <bits/stdc++.h>2 using namespacestd;3 #defineR Register int4 #defineAC 10010005 6 intN, K, head, tail;7 intS[ac];8 structnode{intx, id;} Q[AC];9 Ten voidCalintt) One { AHead =1, tail =0; -      for(R i =1; I <= N; i + +) -     { the          while(Head <= tail && q[head].id <= i-k) + +head; -          while(S[i] < q[tail].x && Head <= tail)--tail;//It can be deleted, but the fortress comes in anyway. -Q[++tail] =(node) {s[i], i}; -         if(i >= k) printf ("%d", q[head].x *t); +}printf ("\ n");  - } +  A intMain () at { -scanf"%d%d", &n, &k); -      for(R i =1; I <= N; i + +) scanf ("%d", &s[i]); -Cal1); -      for(R i =1; I <= N; i + +) s[i] =-S[i]; -Cal (-1); in     return 0; -}
View Code

[LG1886] sliding window monotone queue

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.