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;
    }
    

      

  • 相关阅读:
    深入理解Java:注解(Annotation)--注解处理器
    Java进阶之reflection(反射机制)——反射概念与基础
    JAVA 动态代理
    注解是建立在class文件基础上的东西,同C语言的宏有异曲同工的效果
    Android 进阶8:进程通信之 Binder 机制浅析
    Android Binder机制(一) Binder的设计和框架
    Android Service初解
    原生sql和 TP sql怎么关联?
    Laravel 修改默认日志文件名称和位置
    laravel asset()函数
  • 原文地址:https://www.cnblogs.com/xinsheng/p/3455198.html
Copyright © 2011-2022 走看看