The minimum value of the M-interval in the valley 1440

Source: Internet
Author: User

The minimum value of the M-interval in the valley 1440

Address: http://www.luogu.org/problem/show?pid=1440

Title Description

A sequence of n items (n<=2000000) that shows the number of m before each item to its minimum within this interval. If the preceding number is less than M, then the 1th number starts, and if there are no previous numbers, the output is 0.

Input/output format

Input format:

The first line is two numbers n,m.
The second row, n a positive integer, is the given sequence number.

Output format:

n rows, a number AI of line I, is the minimum of the number of first m in the sequence I'm seeking.

Input/Output sample

Input Sample # #:

6 2

7 8 1 4 3 2

Sample # # of output:

0

7

7

1

1

3

Description

"Data Size"
m≤n≤2000000

Ideas

Monotone queue.

Topic is relatively bare, directly with a monotonous queue maintenance can be. The monotone queue is written for the first time and summarizes the following areas to note:

1, pay attention to the maintenance of what, the subject is a maintenance of a sequence of >=i-m+1, wherein the sequence satisfies a value monotonically increment.

2, pay attention to the boundary, especially if the initial situation to meet the ordinary situation.

3. Note the order in which the maintenance of the monotonic queue is added and taken, depending on the purpose of using the monotone queue.

It is also important to note that in the topic before I m, each value is counted on the current a so need to stagger one.

Code

1#include <iostream>2#include <cstdio>3 using namespacestd;4 5 Const intMAXN =2000000+Ten;6 7 intA[MAXN],QUE[MAXN],D[MAXN];8 intn,m,front,rear;9 TenInlineintRead_int () { One     CharC C=GetChar (); A      while(!isdigit (c)) c=GetChar (); -      -     intx=0; the      while(IsDigit (c)) { -x=x*Ten+c-'0'; -C=GetChar (); -     } +     returnx; - } +  A intMain () { atN=read_int (); m=read_int (); -a[0]=1<< -;//Boundary Not taken -Front=rear=1; -      for(intI=1; i<=n;i++) -     { -a[i]=read_int (); in          -         //Note Order to          while(Front<=rear && (que[front]< (i-m+1)) front++; +             -          while(Front<=rear && A[i]<=a[que[rear]]) rear--;//pay attention to maintaining a value of the smallest the          *que[++rear]=i; $ Panax Notoginsengd[i]=A[que[front]]; -          the     } +cout<<0<<"\ n"; A      for(intI=1; i<n;i++) cout<<d[i]<<"\ n"; the     return 0; +}

The minimum value of the M-interval in the valley 1440

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.