POJ2823_Sliding Window_优先队列||线段树
来源:互联网 发布:兄贵音源软件 编辑:程序博客网 时间:2024/05/18 05:05
Sliding Window
Time Limit: 12000MS Memory Limit: 65536KTotal Submissions: 58278 Accepted: 16702Case Time Limit: 5000MS
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 -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
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
8 31 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 33 3 5 5 6 7
大致题意:
给出一个含有n个元素的数组,一个整数m。相当于有一个长度为m的框,通过这个框,每次只能看到数组中连续的m个数。现在这个框从数组的最左边开始一格一格地往右移动,要求输出每个状态上框中数字的最大值和最小值。
大体思路:
一开始是用优先队列做的。
后来直觉上觉得线段树可能效率更高一点,就又写了一版,结果发现被自己坑了。。。
优先队列版:
记忆中两个优先队列的名字max和min可能是写反了,懒得检查和改正了。。。
#include<cstdio>#include<queue>struct node{int v,p;};struct __min{bool operator () (node x,node y) {return x.v<y.v;}};struct __max{bool operator () (node x,node y){return x.v>y.v;}};int n,k,tt;node t;int Max [1000100];int Min [1000100];std::priority_queue<node,std::vector<node>,__min>_min;std::priority_queue<node,std::vector<node>,__max>_max;int main (){//freopen("in.txt","r",stdin);scanf("%d%d",&n,&k);int i=0,l=0;for(;i<k;i++){scanf("%d",&tt);t.v=tt,t.p=i;_min.push(t);_max.push(t);}Max[l]=_max.top().v;Min[l]=_min.top().v;l++;for(;i<n;i++){scanf("%d",&tt);t.v=tt,t.p=i;_max.push(t);_min.push(t);while(_max.top().p<=i-k) _max.pop();Max[l]=_max.top().v;while(_min.top().p<=i-k) _min.pop();Min[l]=_min.top().v;l++;}printf("%d",Max[0]);for(i=1;i<l;i++)printf(" %d",Max[i]);printf("\n");printf("%d",Min[0]);for(i=1;i<l;i++)printf(" %d",Min[i]);return 0;}
线段树版:
#include<cstdio>#define limit 2147483646#define Max(x,y) x>y ? x : y#define Min(x,y) x<y ? x : ystruct win{int min, max;};win s [4*1000010];win a [1000010];int n, m;void Buildtree(int p, int l, int r){if(l==r){scanf("%d",&s[p].min);s[p].max = s[p].min;return;}int mid = ( l + r ) >> 1;Buildtree(2*p, l, mid);Buildtree(2*p+1, mid+1, r);s[p].max = Max(s[2*p].max,s[2*p+1].max);s[p].min = Min(s[2*p].min,s[2*p+1].min);}win Find (int p, int l, int r, int x, int y){if(l>=x && r<=y){return s[p];}int mid = ( l + r ) >> 1;win d;d.max = -limit, d.min = limit;if(x <= mid) d = Find(2*p, l, mid, x, y);if(y >mid){win q = Find(2*p+1, mid+1, r, x, y);if(d.max < q.max) d.max = q.max;if(d.min > q.min) d.min = q.min;}return d;}int main (){scanf("%d%d", &n, &m);Buildtree(1, 1, n);int k = 0;for(int i=1; i+m-1<=n; i++){win t = Find(1, 1, n, i, i+m-1);a[k].min = t.min, a[k].max = t.max, k++;}printf("%d",a[0].min);for(int i=1; i<k; i++)printf(" %d",a[i].min);printf("\n");printf("%d",a[0].max);for(int i=1; i<k; i++)printf(" %d",a[i].max);printf("\n");return 0;}
0 0
- POJ2823_Sliding Window_优先队列||线段树
- POJ_2482_Stars in Your Window_线段树、离散化处理
- HDOJ 5360 优先队列&简单线段树
- HDU 5361 (优先队列 线段树)
- hdu 2430 优先队列/线段树
- [ifrog 1072 Capture] 优先队列/线段树
- Window_纪中_1326_单调队列
- POJ--2823[Sliding Window] 线段树或优先队列
- HDOJ 4302 —— 线段树 或 优先队列
- POJ2823 Sliding Window(线段树 ST算法 优先队列)
- CF #278 div2 D Strip 线段树+优先队列
- hdu 4006 The kth great number(线段树 || 优先队列)
- HDU 4302 Holedox Eating(线段树/优先队列/multiset)
- poj 2274 线段树+堆(优先队列)
- hdu 4302 Holedox Eating(优先队列模拟或线段树)
- [BZOJ2006][[NOI2010]超级钢琴][优先队列+线段树]
- Window_纪中1326_水_单调队列
- HDU - 4302 :Holedox Eating ,线段树、树状数组+二分,优先队列
- ZOJ
- C++输入输出流缓冲区
- jieba词性和句法分析树标注集
- [生存志] 第130节 司马著史记
- Python位运算符
- POJ2823_Sliding Window_优先队列||线段树
- iOS开发零基础教程之真机调试流程
- Linux系统升级bash安装 ---待更新
- android开发中Canvas.save()、Canvas.restore()的区别与联系
- 删除Vector 结构体元素相同的,保留后面一个
- 自定义ToolBar与ToolBar的封装,使ToolBar的Title居中
- Vim简明教程【CoolShell】
- 使用echarts插件画折线图,及echarts配置项解释
- 【ORACLE】Oracle 11.2.0.1数据泵导入导出操作,报ORA-39083,ORA-14102