【POJ】2823 Sliding Window

单调队列。

  1 /* 2823 */
  2 #include <iostream>
  3 #include <sstream>
  4 #include <string>
  5 #include <map>
  6 #include <queue>
  7 #include <set>
  8 #include <stack>
  9 #include <vector>
 10 #include <deque>
 11 #include <algorithm>
 12 #include <cstdio>
 13 #include <cmath>
 14 #include <ctime>
 15 #include <cstring>
 16 #include <climits>
 17 #include <cctype>
 18 #include <cassert>
 19 #include <functional>
 20 #include <iterator>
 21 #include <iomanip>
 22 using namespace std;
 23 //#pragma comment(linker,"/STACK:102400000,1024000")
 24
 25 #define sti                set<int>
 26 #define stpii            set<pair<int, int> >
 27 #define mpii            map<int,int>
 28 #define vi                vector<int>
 29 #define pii                pair<int,int>
 30 #define vpii            vector<pair<int,int> >
 31 #define rep(i, a, n)     for (int i=a;i<n;++i)
 32 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 33 #define clr                clear
 34 #define pb                 push_back
 35 #define mp                 make_pair
 36 #define fir                first
 37 #define sec                second
 38 #define all(x)             (x).begin(),(x).end()
 39 #define SZ(x)             ((int)(x).size())
 40 #define lson            l, mid, rt<<1
 41 #define rson            mid+1, r, rt<<1|1
 42
 43 const int maxn = 1e6+5;
 44 int n, k;
 45 int a[maxn];
 46 int Q[maxn], P[maxn];
 47 int mn[maxn], mx[maxn];
 48
 49 void getMin() {
 50     int l = 1, r = 0;
 51     int i;
 52
 53     for (i=0; i<k-1; ++i) {
 54         while (l<=r && Q[r]>=a[i])
 55             --r;
 56         Q[++r] = a[i];
 57         P[r] = i;
 58     }
 59
 60     for (; i<n; ++i) {
 61         while (l<=r && Q[r]>=a[i])
 62             --r;
 63         Q[++r] = a[i];
 64         P[r] = i;
 65         while (P[l] < i-k+1)
 66             ++l;
 67         mn[i-k+1] = Q[l];
 68     }
 69 }
 70
 71 void getMax() {
 72     int l = 1, r = 0;
 73     int i;
 74
 75     for (i=0; i<k-1; ++i) {
 76         while (l<=r && Q[r]<=a[i])
 77             --r;
 78         Q[++r] = a[i];
 79         P[r] = i;
 80     }
 81
 82     for (; i<n; ++i) {
 83         while (l<=r && Q[r]<=a[i])
 84             --r;
 85         Q[++r] = a[i];
 86         P[r] = i;
 87         while (P[l] < i-k+1)
 88             ++l;
 89         mx[i-k+1] = Q[l];
 90     }
 91 }
 92
 93 void solve() {
 94     getMin();
 95     getMax();
 96     rep(i, 0, n-k+1) {
 97         if (i == n-k)
 98             printf("%d\n", mn[i]);
 99         else
100             printf("%d ", mn[i]);
101     }
102     rep(i, 0, n-k+1) {
103         if (i == n-k)
104             printf("%d\n", mx[i]);
105         else
106             printf("%d ", mx[i]);
107     }
108 }
109
110 int main() {
111     ios::sync_with_stdio(false);
112     #ifndef ONLINE_JUDGE
113         freopen("data.in", "r", stdin);
114         freopen("data.out", "w", stdout);
115     #endif
116
117     scanf("%d %d", &n, &k);
118     rep(i, 0, n)
119         scanf("%d", &a[i]);
120     solve();
121
122     #ifndef ONLINE_JUDGE
123         printf("time = %d.\n", (int)clock());
124     #endif
125
126     return 0;
127 }
时间: 2024-10-13 06:03:22

【POJ】2823 Sliding Window的相关文章

【原创】leetCodeOj --- Sliding Window Maximum 解题报告

天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an array nums, 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

POJ 题目2823 Sliding Window(RMQ,固定区间长度)

Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 46507   Accepted: 13442 Case 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

【LeetCode】239. Sliding Window Maximum

Sliding Window Maximum Given an array nums, 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 right by one position.

POJ 2823 Sliding Window 【单调队列】

题目链接:http://poj.org/problem?id=2823 题目大意:给出一组数,一个固定大小的窗口在这个数组上滑动,要求出每次滑动该窗口内的最大值和最小值. 这就是典型的单调队列,单调队列的作用就在此.单调队列的队首为区间内的最值,但是整个队列不用保持单调. 用两个队列分别处理最大值和最小值,在此说明一下最大值: 往队列中添加值num时,从队尾开始扫,直到遇到一个小于num的d值,将num插入d的后一位.之后的元素全部无效化(不管后面的元素就行).查找最大值的时候,从队首开始找,如

poj 2823 Sliding Window

poj 2823 Sliding Window 单调队列 单调队列是一个单调的状态(递增,或者递减) 所以需要维护这么一个状态 http://baike.baidu.com/link?url=ZcGM7Hzo8zVQUU6Oqqq18SlCMJ92ts3I1aXwQGDZw_NiDDlzIIV9GKlfs3X1fcHVppZHOU31geHZG4cOcRZOAK 固定 k 区间的单调 队列,求 最小值,如果 两个元素 A B ,如果 A 的 下标 比 B 小,但是 A 的 值 比 B 大,那么在

[ACM] poj 2823 Sliding Window(单调队列)

Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 36212   Accepted: 10723 Case 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

[ACM] poj 2823 Sliding Window (单调队列)

高一时,学校组织去韶山游玩,我没去,这次趁着五一,总算去了我心心念念的韶山.其实我知道所有的景点都是差不多的,可是因为电视剧<恰同学少年>,让我对毛泽东有了进一层的了解,所以,我一直都想去看看. 有两个同学一男一女是我理想的旅友,可是女生不想去,而男士回家了.所以,我独自一人去了. 准备工作:一小包饼干,一小包山楂片,两个苹果,一瓶水,帽子(防晒),墨镜(装酷) 早晨5:30起床了,洗漱完毕,吃完早餐,赶到公交车站牌那里,才6点过几分.公交车6:31才到,等了近半个小时(公交车上明明说是6:0

洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)

To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array is [1 3 -1 -3 5 3 6 7], and k = 3. 输入输出格式 输入格式: 输入一共有两行,第一行为n,k. 第二行为n个数(<INT_MAX). 输出格式: 输出共两行,第一行为每次窗口滑动的最小值

POJ 2823 Sliding Window 题解

POJ 2823 Sliding  Window 题解 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 slidi