0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:25:24 +00:00

P2032 扫描

https://www.luogu.com.cn/record/166777151
This commit is contained in:
Baoshuo Ren 2024-07-18 08:30:26 +08:00
parent 24213fc267
commit 5037bd1813
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

40
Luogu/P2032/P2032.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <iostream>
#include <deque>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 2e6 + 5;
int n, k, a[N];
std::deque<int> q; // 双端队列
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
// 首先,代码使用一个 while 循环来维护一个单调递减的队列 q。
// 在每次循环中,代码检查当前元素 a[i] 是否大于队列中最后一个元素 a[q.back()]。
// 如果是,则将队列中最后一个元素出队,直到队列为空或者当前元素不大于队列中最后一个元素。
// 这样可以确保队列中的元素始终保持单调递减的顺序。
// 想一想:每次入队的元素都比前面的元素小,那么整个队列就一定是单调递减的。
while (!q.empty() && a[q.back()] < a[i]) q.pop_back();
q.push_back(i);
// 然后,代码检查队列 q 的第一个元素是否在当前窗口内。
// 如果队列的第一个元素的索引小于等于 i - k则说明该元素已经不在当前窗口内需要将其出队。
// (其实不会出现小于的情况)
// 这样可以确保队列中的元素始终是当前窗口内的元素。
if (q.front() <= i - k) q.pop_front();
if (i >= k) cout << a[q.front()] << endl;
}
return 0;
}