zoukankan      html  css  js  c++  java
  • [poj2823]sliding windows(单调队列模板题)

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

    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

    这道题很明显用单调队列。每到一个新的区间,都判断当前队列的首位是否已不在这个区间,如果不在,就把队首出队。加入新元素。每加入一个元素,都从队列尾往前遍历,如果当前结单大于或小于(要写两个队列)要放进的结点,就从后面把这个元素出队。这个操作比较骚,所以我们写出来的东西不是严谨的队列,而是一个有栈性质的队列。这就有点骚了,所以我们必须手写一个这样的奇怪的玩意。

    第一次的提交的时候tle了,看来poj对我的恶意不小啊。然后抱着侥幸心理加了一个读优,然后就AC了,23333。

    不多bb我放代码了,arr存数组,有两个队列max_queue和min queue对应max tail等。

    #include<iostream>
    #include<cctype>
    #include<cstdio>
    using namespace std;
    int arr[1000000];
    int max_queue[1000000], min_queue[1000000], max_tail, min_tail, max_head = 1, min_head = 1;
    int N, K;
    inline int  read()
    {
    	int X = 0, w = 0; char ch = 0;
    	while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    	while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    	return w ? -X : X;
    }
    bool max_empty()
    {
    	if (max_tail == max_head - 1)
    		return true;
    	else
    		return false;
    }
    bool min_empty()
    {
    	if (min_tail == min_head - 1)
    		return true;
    	else
    		return false;
    }
    void min_push(int a)
    {
    	//if(!min_empty()) 
    	while (arr[a] <= arr[min_queue[min_tail]] && !min_empty())
    		min_tail--;
    	min_tail++;
    	min_queue[min_tail] = a;
    }
    void max_push(int a)
    {
    	while (arr[a] > arr[max_queue[max_tail]] && !max_empty())
    		max_tail--;
    	max_tail++;
    	max_queue[max_tail] = a;
    }
    int min_top()
    {
    	return min_queue[min_head];
    }
    int max_top()
    {
    	return max_queue[max_head];
    }
    void min_pop()
    {
    	min_head++;
    }
    void max_pop()
    {
    	max_head++;
    
    }
    int main()
    {
    	cin >> N >> K;
    	for (int i = 1; i <= N; i++)
    	{
    		arr[i]=read();
    	}
    	for (int i = 1; i <= K; i++)
    	{
    		min_push(i);
    	}
    	cout << arr[min_top()] << " ";
    	for (int i = 1; i <= N - K; i++)
    	{
    		if (min_top() == i)
    			min_pop();
    		min_push(i + K);
    		cout << arr[min_top()] << " ";
    	}
    	cout << endl;
    	for (int i = 1; i <= K; i++)
    	{
    		max_push(i);
    	}
    	cout << arr[max_top()] << " ";
    	for (int i = 1; i <= N - K; i++)
    	{
    		//cout<<"max "<<max_top()<<" ";
    		if (max_top() == i)
    			max_pop();
    		max_push(i + K);
    		cout << arr[max_top()] << " ";
    	}
    	//system("pause");
    }


  • 相关阅读:
    Python Django :HTTP生命周期
    Docker简介及Centos 7 安装[初步配置]
    python:Django 简介。
    python :Django url /views /Template 文件介绍
    python:异常处理及程序调试
    python之正则表达式【re】
    Day 21 python :面向对象 类的相关内置函数 /单例模式 /描述符
    Day 22: 软件开发目录设计规范
    Day 20: 面向对象【多态,封装,反射】字符串模块导入/内置attr /包装 /授权
    SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)
  • 原文地址:https://www.cnblogs.com/sherrlock/p/9525787.html
Copyright © 2011-2022 走看看