http://poj.org/problem?id=2823 (题目链接)
题意
维护滑动窗口最大最小值。
Solution
sb单调队列
代码
// poj2823
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=1000010;
int n,m,a[maxn],ax[maxn],an[maxn]; int main() {
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
deque<int> qx,qn;
for (int i=1;i<=n;i++) {
while (!qx.empty() && i-qx.front()>=m) qx.pop_front();
while (!qn.empty() && i-qn.front()>=m) qn.pop_front();
while (!qx.empty() && a[qx.back()]<=a[i]) qx.pop_back();
while (!qn.empty() && a[qn.back()]>=a[i]) qn.pop_back();
qx.push_back(i);qn.push_back(i);
if (i>=m) ax[i-m+1]=a[qx.front()],an[i-m+1]=a[qn.front()];
}
for (int i=1;i<=n-m+1;i++) printf("%d ",an[i]);
puts("");
for (int i=1;i<=n-m+1;i++) printf("%d ",ax[i]);
return 0;
}