洛谷 P2032 扫描
思路
单调队列例题,有多倍经验哦~~
多倍经验传送门:
代码
//知识点:单调队列
/*
By:Loceaner
*/
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for( ; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
return x * f;
}
const int N = 2e6 + 11;
int n, k, q[N], a[N];
void getmax() {
int head = 0, tail = 0;
for(int i = 1; i < k; i++) {
while(head <= tail && a[q[tail]] <= a[i]) tail--;
q[++tail] = i;
}
for(int i = k; i <= n; i++) {
while(head <= tail && a[q[tail]] <= a[i]) tail--;
q[++tail] = i;
while(q[head] <= i - k) head++;
cout << a[q[head]] << '
';
}
}
int main() {
n = read(), k = read();
for(int i = 1; i <= n; i++) a[i] = read();
getmax();
return 0;
}