• 单调队列&单调栈 基础


    参考博客 https://www.cnblogs.com/tham/p/8038828.html

    例题  poj 2823

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

    题意 给出一个序列 n个数 求每个长度为k的子串的最大值和最小值
    解析 如果用尺取写的话 失匹的话就要花费o(k)的复杂度去维护最大值 最小值 总时间复杂度o(n*k) 数据卡的死就肯定超时
        我们发现维护最大最最小值的时候 发现有很多重复的比较 所以我们可以维护一个最值数组 使它严格单调 这样直接取第一个元素就好了(单调队列)。
        
    AC代码(c++)
     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 #include <iostream>
     6 #include <sstream>
     7 #include <algorithm>
     8 #include <string>
     9 #include <queue>
    10 #include <map>
    11 #include <vector>
    12 using namespace std;
    13 const int maxn = 1e6+50;
    14 const int inf = 0x3f3f3f3f,mod = 998244353;
    15 const double epx = 1e-10;
    16 typedef long long ll;
    17 struct node
    18 {
    19     int x,y;   //x值 y下标
    20 }v[maxn];
    21 int a[maxn],mn[maxn],mx[maxn];
    22 int n,m;
    23 void getmin()
    24 {
    25     int head=1,tail=0;
    26     for(int i=1;i<m;i++)
    27     {
    28         while(head<=tail&&a[i]<=v[tail].x)tail--;
    29         v[++tail].x=a[i],v[tail].y=i;
    30     }
    31     for(int j=m;j<=n;j++)
    32     {
    33         while(head<=tail&&a[j]<=v[tail].x)tail--;
    34         v[++tail].x=a[j],v[tail].y=j;
    35         while(j-v[head].y>=m)head++;
    36         mn[j]=v[head].x;
    37     }
    38 }
    39 void getmax()
    40 {
    41     int head=1,tail=0;
    42     for(int i=1;i<m;i++)
    43     {
    44         while(head<=tail&&a[i]>=v[tail].x)tail--;
    45         v[++tail].x=a[i],v[tail].y=i;
    46     }
    47     for(int j=m;j<=n;j++)
    48     {
    49         while(head<=tail&&a[j]>=v[tail].x)tail--;
    50         v[++tail].x=a[j],v[tail].y=j;
    51         while(j-v[head].y>=m)head++;   
    52         mx[j]=v[head].x;
    53     }
    54 }
    55 int main()
    56 {
    57     scanf("%d %d",&n,&m);
    58     for(int i=1;i<=n;i++)
    59         scanf("%d",&a[i]);
    60     getmax();
    61     getmin();
    62     for(int i=m;i<=n;i++)
    63     {
    64         printf(i!=n?"%d ":"%d
    ",mn[i]);
    65     }
    66     for(int i=m;i<=n;i++)
    67     {
    68         printf(i!=n?"%d ":"%d
    ",mx[i]);
    69     }
    70 }


  • 相关阅读:
    RAM调优之日志分析
    HDU Always Cook Mushroom (极角排序+树状数组)
    并非全部的程序猿都适合做技术管理
    HTTP Header具体解释
    Linux 通配符
    寻找正在连接中的网络连接
    hdu 1052 田忌赛马
    linux上电自启动应用程序具体解释
    C++ 中的 const 类型变量
    FileUtil
  • 原文地址:https://www.cnblogs.com/stranger-/p/8998555.html
Copyright © 2020-2023  润新知