zoukankan      html  css  js  c++  java
  • 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 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  -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

    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

    思路:

    1. 单调队列

    2. 单调队列里存储原数组的下标

    3. 假如是最大单调队列, 每次加入元素前, 待加入元素与队尾元素做比较, 假如待加入元素较大, 那么删除当前队尾元素, 重复比较, 直到可以加入.

    4. 操作背后隐藏的原理是: 窗口每次移动都会引入一个新元素, 剔除一个旧元素. 窗口中已经存在的小于新元素的都要被踢出

    总结:

    1. ST, ED 初始化为 1 比较好

    3. 看 discuss 说, ST 算法会超时

    代码:

    #include <iostream>
    using namespace std;
    
    const int MAXN = 1000010;
    int a[MAXN];
    int window[MAXN];
    int n, k;
    /*
     *	当心 k == 1 的情况
     */
    void printMin() {
    	memset(window, 0, sizeof(window));
    	int head = 1, tail = 1;	// 初始化为 0
    	for(int i = 1; i < k; i ++) {
    		while(tail >= head && a[i] < a[window[tail]])	// 队列不为空, 且待加入元素小于队尾元素
    			tail--;
    		window[++tail] = i;
    	}
    	for(int i = k; i <= n; i ++) {
    		if(tail >= head && i-window[head] >= k)	// 窗口移动, 删除最左边的元素
    			head++;
    		while(tail >= head && a[i] < a[window[tail]])
    			tail--;
    		window[++tail] = i;
    		printf("%d ", a[window[head]]);
    	}
    	printf("
    ");
    }
    
    void printMax() {
    	memset(window, 0, sizeof(window));
    	int head = 1, tail = 1;
    	for(int i = 1; i < k; i ++) {
    		while(tail >= head && a[i] > a[window[tail]])
    			tail--;
    		window[++tail] = i;
    	}
    	for(int i = k; i <= n; i ++) {
    		if(tail >= head && i-window[head] >= k)
    			head++;
    		while(tail >= head && a[i] > a[window[tail]])
    			tail--;
    		window[++tail] = i;
    		printf("%d ", a[window[head]]);
    	}
    	printf("
    ");
    }
    int main() {
    	freopen("E:\Copy\ACM\测试用例\in.txt", "r", stdin);
    	
    	while(cin>>n >> k) {
    		memset(a, 0, sizeof(a));
    		
    		for(int i = 1; i <= n; i ++) {
    			scanf("%d", &a[i]);
    		}
    		// mainFunc
    		printMin();
    		printMax();
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    ASP.NET多线程下使用HttpContext.Current为null解决方案
    Pig性能优化
    重温设计模式之前言
    MVC4+WebApi+Redis Session共享练习(下)
    C#中对象的输出
    CYQ.Data 支持WPF相关的数据控件绑定.Net获取iis版本
    java版微信公众平台自定义菜单创建代码实现
    android动画特效之解决解决移动后闪烁现象,解决输入法弹出后位置回复原状,解决两个动画叠加
    VMware 11安装Mac OS X 10.10 及安装Mac Vmware Tools(超详细),以及动态调整虚拟机硬盘大小
    三个API:开启、关闭、关闭线程重定向
  • 原文地址:https://www.cnblogs.com/xinsheng/p/3455198.html
Copyright © 2011-2022 走看看