您的位置:首页 > 其它

[POJ2823]Sliding Window

2015-08-29 15:42 337 查看
题目链接:http://poj.org/problem?id=2823

Description

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
Window positionMinimum valueMaximum value
[1 3 -1] -3 5 3 6 7 -13
1 [3 -1 -3] 5 3 6 7 -33
1 3 [-1 -3 5] 3 6 7 -35
1 3 -1 [-3 5 3] 6 7 -35
1 3 -1 -3 [5 3 6] 7 36
1 3 -1 -3 5 [3 6 7]37
Your task is to determine the maximum and minimum values in the sliding window at each position.

Input

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Source

POJ Monthly--2006.04.28, Ikki

单调队列题,由于本人智商比较捉鸡,于是用STL的优先队列来实现单调队列(单调队列也是优先队列的一种嘛)

代码如下:

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 1000010;
int n, k;
int num[maxn];
int mmin[maxn];
int mmax[maxn];
typedef struct bigg {
bool operator()(const int a, const int b) {
return num[a] < num[b];
}
}bigg;

typedef struct smal {
bool operator()(const int a, const int b) {
return num[a] > num[b];
}
}smal;

priority_queue<int, vector<int>, bigg> upstream;
priority_queue<int, vector<int>, smal> downstream;
int cntu, cntd;
int main() {
scanf("%d %d", &n, &k);
if(k > n) {
k = n;
}
cntu = 0;
cntd = 0;
for(int i = 1; i <= n; i++) {
scanf("%d", &num[i]);
}
for(int i = 1; i <= k; i++) {
upstream.push(i);
downstream.push(i);
}
mmin[cntd++] = num[downstream.top()];
mmax[cntu++] = num[upstream.top()];
for(int i = k + 1; i <= n; i++) {
upstream.push(i);
downstream.push(i);
while(i - downstream.top() >= k) {
downstream.pop();
}
mmin[cntd++] = num[downstream.top()];
while(i - upstream.top() >= k) {
upstream.pop();
}
mmax[cntu++] = num[upstream.top()];
}
for(int i = 0; i < cntd; i++) {
cout << mmin[i] << " ";
}
cout << endl;
for(int i = 0; i < cntu; i++) {
cout << mmax[i] << " ";
}
cout << endl;
}


Use C++ to compile. Or it'll TLE...

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>

using namespace std;

inline bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<'0'||in>'9')) in=getchar();
if(in=='-'){ IsN=true;num=0;}
else num=in-'0';
while(in=getchar(),in>='0'&&in<='9'){
num*=10,num+=in-'0';
}
if(IsN) num=-num;
return true;
}

typedef struct Node {
int idx;
int val;
Node() {}
Node(int ii, int vv) : idx(ii), val(vv) {}
}Node;
const int maxn = 1000010;
int n, k;
int a[maxn];
Node q[maxn];
int front, tail;

int main() {
//    freopen("in", "r", stdin);
while(~scanf("%d %d", &n, &k)) {
for(int i = 0; i < n; i++) {
scan_d(a[i]);
}
front = 0,  tail = 0;
q[tail++] = Node(0, a[0]);
int ans[maxn], cnt = 0;
for(int i = 1; i < k; i++) {
while(front < tail && q[tail-1].val >= a[i]) {
tail--;
}
q[tail++] = Node(i, a[i]);
}
ans[cnt++] = q[front].val;
for(int i = k; i < n; i++) {
while(front < tail && q[tail-1].val >= a[i]) {
tail--;
}
while(front < tail && i - k + 1 > q[front].idx) {
front++;
}
q[tail++] = Node(i, a[i]);
ans[cnt++] = q[front].val;
}
for(int i = 0; i < cnt; i++) {
printf("%d ", ans[i]);
}
puts("");
front = 0, tail = 0;
q[tail++] = Node(0, a[0]);
cnt = 0;
for(int i = 1; i < k; i++) {
while(front < tail && q[tail-1].val <= a[i]) {
tail--;
}
q[tail++] = Node(i, a[i]);
}
ans[cnt++] = q[front].val;
for(int i = k; i < n; i++) {
while(front < tail && q[tail-1].val <= a[i]) {
tail--;
}
while(front < tail && i - k + 1 > q[front].idx) {
front++;
}
q[tail++] = Node(i, a[i]);
ans[cnt++] = q[front].val;
}
for(int i = 0; i < cnt; i++) {
printf("%d ", ans[i]);
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: