luogu 1440 for the minimum interval within the m

Title Description

Comprising a number of columns (n ​​<= 2000000) n items, obtains the number m of each of the front to a minimum in this range it. When the number is less than m in front of the number of items from a first start, if there is no front number 0 is output.

Input Format

The first line of the two numbers n, m.

Second row, n-positive integer, for the given number of columns.

Output Format

n rows, a number ai i-th row, to the minimum required number of m before the i-th sequence.

Sample input and output

Input # 1
6 2
7 8 1 4 3 2
Output # 1
0
7
7
1
1
3 

Description / Tips

[Data] scale

m≤n≤2000000

to i 3 × 1 0 ^ 7

analysis

Monotonous queue

Internal queues monotonically increasing monotone

Code

 1 /**************************
 2 User:Mandy.H.Y
 3 Language:c++
 4 Problem:luogu1440
 5 Algorithm:
 6 **************************/
 7 #include<bits/stdc++.h>
 8 
 9 using namespace std;
10 
11 const int maxn = 2e6 + 5;
12 
13 int n,l,r,m;
14 int q[maxn],a[maxn];
15 
16 template<class T>inline void read(T &x){
17     x = 0;bool flag = 0;char ch = getchar();
18     while(!isdigit(ch)) flag |= ch == '-',ch = getchar();
19     while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
20     if(flag) x = -x;
21 }
22 
23 template<class T>void putch(const T x){
24     if(x > 9) putch(x / 10);
25     putchar(x % 10 | 48);
26 }
27 
28 template<class T>void put(const T x){
29     if(x < 0) putchar('-'),putch(-x);
30     else putch(x);
31 }
32 
33 void file(){
34     freopen("1440.in","r",stdin);
35     freopen("1440.out","w",stdout);
36 }
37 
38 void readdata(){
39     read(n);read(m);
40 }
41 
42 void work(){
43     for(int i = 1;i <= n; ++ i){
44         
45         read(a[i]);
46         
47         while(l < r && i - q[l] > m) l++;
48         
49         if(l >= r) puts("0");
50         else put(a[q[l]]),putchar('\n');
51         
52         while(l < r && a[q[r - 1]] >= a[i]) r--;//>= 
53         q[r++] = i;
54     }
55 }
56 
57 int main(){
58 //    file();
59     readdata();
60     work();
61     return 0;
62 }
View Code

 

Guess you like

Origin www.cnblogs.com/Mandy-H-Y/p/11417077.html