zoukankan      html  css  js  c++  java
  • POJ

    滑动窗口的最值问题

    http://poj.org/problem?id=2823

    链接:https://ac.nowcoder.com/acm/problem/51001
    来源:牛客网

    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 32768K,其他语言65536K
    64bit IO Format: %lld

    题目描述

    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 position Minimum value Maximum 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. 

    输入描述:

    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.

    输出描述:

    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.

    示例1

    输入

    8 3
    1 3 -1 -3 5 3 6 7

    输出

    -1 -3 -3 -3 3 3
    3 3 5 5 6 7
     1 #include <bits/stdc++.h>
     2 typedef long long LL;
     3 #define pb push_back
     4 #define mst(a) memset(a,0,sizeof(a))
     5 const int INF = 0x3f3f3f3f;
     6 const double eps = 1e-8;
     7 const int mod = 1e9+7;
     8 const int maxn = 1e6+10;
     9 using namespace std;
    10 
    11 int a[maxn];
    12 vector<int> ans1,ans2;
    13 deque<int> qe1,qe2;
    14 
    15 int main()
    16 {
    17     #ifdef DEBUG
    18     freopen("sample.txt","r",stdin); //freopen("data.out", "w", stdout);
    19     #endif
    20     
    21     int n,m;
    22     scanf("%d %d",&n,&m);
    23     for(int i=1;i<=n;i++)
    24         scanf("%d",&a[i]);
    25     for(int i=1;i<=n;i++)
    26     {
    27         //维护最小值队列qe1
    28         if(!qe1.empty()&&qe1.front()<=i-m) qe1.pop_front();
    29         if(qe1.empty()) qe1.push_back(i);
    30         else
    31         {
    32             if(a[i]<=a[qe1.front()])
    33             {
    34                 qe1.clear();
    35                 qe1.push_back(i);
    36             }
    37             else
    38             {
    39                 if(qe1.size()==1) qe1.push_back(i);
    40                 else
    41                 {
    42                     if(a[i]>=a[qe1.back()]) qe1.push_back(i);
    43                     else
    44                     {
    45                         int t = qe1.back();
    46                         while(a[i]<a[t])
    47                         {
    48                             qe1.pop_back();
    49                             t = qe1.back();
    50                         }
    51                         qe1.push_back(i);
    52                     }
    53                 }
    54             }
    55         }
    56         //维护最大值队列qe2
    57         if(!qe2.empty()&&qe2.front()<=i-m) qe2.pop_front();
    58         if(qe2.empty()) qe2.push_back(i);
    59         else
    60         {
    61             if(a[i]>=a[qe2.front()])
    62             {
    63                 qe2.clear();
    64                 qe2.push_back(i);
    65             }
    66             else
    67             {
    68                 if(qe2.size()==1) qe2.push_back(i);
    69                 else
    70                 {
    71                     if(a[i]<=a[qe2.back()]) qe2.push_back(i);
    72                     else
    73                     {
    74                         int t = qe2.back();
    75                         while(a[i]>a[t])
    76                         {
    77                             qe2.pop_back();
    78                             t = qe2.back();
    79                         }
    80                         qe2.push_back(i);
    81                     }
    82                 }
    83             }
    84         }
    85         
    86         if(i>=m)
    87         {
    88             ans1.push_back(a[qe1.front()]);
    89             ans2.push_back(a[qe2.front()]);
    90         }
    91     }
    92 
    93     for(int i=0;i<=n-m;i++)
    94         printf(i==n-m?"%d
    ":"%d ",ans1[i]);
    95     for(int i=0;i<=n-m;i++)
    96         printf(i==n-m?"%d
    ":"%d ",ans2[i]);
    97     
    98     return 0;
    99 }

    -

  • 相关阅读:
    1.1、html概述和基本结构
    几个常用的产品原型设计工具
    Redis 3. 与python交互
    Redis 2.2、主从配置
    Redis 2.1、发布订阅
    Redis 1.6、zset
    Redis 1.5、set
    Redis 1.4、list
    php实现手机拍照上传头像功能
    php获取文件mime类型Fileinfo等方法
  • 原文地址:https://www.cnblogs.com/jiamian/p/13219184.html
Copyright © 2011-2022 走看看