zoukankan      html  css  js  c++  java
  • poj 2823

    Time Limit: 12000MS   Memory Limit: 65536K
    Total Submissions: 55797   Accepted: 16048
    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
    

    Source

    这道题算是单调队列的模板吧,单调队列解决的是有一个长度为n的数列,有数据一个长度为k的窗口,求窗口在滑动的时候每个窗口里的极值。一般的方法会忽略到上个窗口所在位置的数据,造成浪费。利用单调对列,窗口每进行一次滑动,就将新加入窗口的数字进行判断,查看他在队列的位置。以区间最大值为例子,每次查找他在队列中比他小的数据,把他们全部删去,因为所要的是最大值,他们在该区域上已经没有任何价值。然后新加入窗口的数放到尾部。这样每次,只要查询最大值就可以了。

    #include <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    //typedef long long LL;
    //typedef __int64 Int;
    typedef pair<int, int> PAI;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-5;
    const double PI = acos(-1.0);
    const int MOD = 1e9 + 7;
    const int MAXN = 1e6 + 10;
    int minn[MAXN], maxx[MAXN], a[MAXN];
    int n, k, loc[MAXN], que[MAXN];
    void get_min() {
        int head = 1, tail = 0;
        for (int i = 0; i < k - 1; i++) {
            //严格递减
            while (head <= tail && a[i] <= que[tail]) tail--;
            que[++tail] = a[i];
            loc[tail] = i;
        }
        for (int i = k - 1; i < n; i++) {
            while (head <= tail && a[i] <= que[tail]) tail--;
            que[++tail] = a[i];
            loc[tail] = i;
             while (loc[head] < i - k + 1) head++;
            minn[i - k + 1] = que[head];
        }
    }
    void get_max() {
        int head = 1, tail = 0;
        for (int i = 0; i < k - 1; i++) {
            //严格递减
            while (head <= tail && a[i] >= que[tail]) tail--;
            que[++tail] = a[i];
            loc[tail] = i;
        }
        for (int i = k - 1; i < n; i++) {
            while (head <= tail && a[i] >= que[tail]) tail--;
            que[++tail] = a[i];
            loc[tail] = i;
             while (loc[head] < i - k + 1) head++;
            maxx[i - k + 1] = que[head];
        }
    }
    void solve() {
        get_max(); get_min();
        for (int i = 0; i < n - k; i++) {
            printf("%d ", minn[i]);
        }
        printf("%d
    ", minn[n - k]);
        for (int i = 0; i < n - k; i++) {
            printf("%d ", maxx[i]);
        }
        printf("%d
    ", maxx[n - k]);
    }
    int main() {
        while (scanf("%d%d", &n, &k) != EOF) {
            for (int i = 0; i < n; i++) {
                scanf("%d", &a[i]);
            }
            solve();
        }
        return 0;
    }
    /*
    8 3
    1 3 -1 -3 5 3 6 7
    */



  • 相关阅读:
    linux安装maven
    Jenkins 改成中文语言显示
    appium怎么按下系统按键?如按下返回键、home键等等
    Jenkins+TestNG+gitlab+maven持续集成
    问题一:使用AndroidDriver而非原来的AppiumDriver的原因
    appium教程
    问题二:appium 搞定权限弹框的一个小办法
    问题三:Appium 的 UIAutomator2 模式下使用 sendKeys 出现错误
    TestNG执行顺序控制
    idea Mac 快捷键
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770766.html
Copyright © 2011-2022 走看看