[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.
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
Sample Output
Source
POJ Monthly--2006.04.28, Ikki
单调队列题,由于本人智商比较捉鸡,于是用STL的优先队列来实现单调队列(单调队列也是优先队列的一种嘛)
代码如下:
Use C++ to compile. Or it'll TLE...
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 position | Minimum value | Maximum value |
---|---|---|
[1 3 -1] -3 5 3 6 7 | -1 | 3 |
1 [3 -1 -3] 5 3 6 7 | -3 | 3 |
1 3 [-1 -3 5] 3 6 7 | -3 | 5 |
1 3 -1 [-3 5 3] 6 7 | -3 | 5 |
1 3 -1 -3 [5 3 6] 7 | 3 | 6 |
1 3 -1 -3 5 [3 6 7] | 3 | 7 |
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; }
相关文章推荐
- 文件重命名小程序(适用于unix系c++)
- Android应用程序用户界面(五)
- Dijkstra算法求最短路径(java)
- CSS display:none和visibility:hidden区别
- Scroller的应用--滑屏实现
- hdu5325 Crazy Bobo
- dp转换px
- pg数据库配置无需交互式输入密码连接服务端
- IOS UICollectionView
- 特别篇:Hyper-v群集模拟实战演示
- java.sql.SQLException: ORA-01578: ORACLE 数据块损坏问题解决办法
- Pairing_heap(配对堆)
- 872 开会【排列组合】
- 全国大学生飞思卡尔智能车竞赛小记
- C语言qsort函数用法
- java.sql.SQLException: ORA-01578: ORACLE 数据块损坏问题解决办法
- QTreeView使用模
- outputStream
- eclipse git 配置 远程上传按钮无法使用问题
- [CLR via C#]委托