zoukankan      html  css  js  c++  java
  • [POJ2823]Sliding Window

    题目链接:http://poj.org/problem?id=2823

    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

     
     
    单调队列题,由于本人智商比较捉鸡,于是用STL的优先队列来实现单调队列(单调队列也是优先队列的一种嘛)
     
    代码如下:
     
     1 #include <iostream>
     2 #include <queue>
     3 #include <cstdio>
     4 #include <cstring>
     5 
     6 using namespace std;
     7 
     8 const int maxn = 1000010;
     9 int n, k;
    10 int num[maxn];
    11 int mmin[maxn];
    12 int mmax[maxn];
    13 typedef struct bigg {
    14     bool operator()(const int a, const int b) {
    15         return num[a] < num[b];
    16     }
    17 }bigg;
    18 
    19 typedef struct smal {
    20     bool operator()(const int a, const int b) {
    21         return num[a] > num[b];
    22     }
    23 }smal;
    24 
    25 priority_queue<int, vector<int>, bigg> upstream;
    26 priority_queue<int, vector<int>, smal> downstream;
    27 int cntu, cntd;
    28 int main() {
    29     scanf("%d %d", &n, &k);
    30     if(k > n) {
    31         k = n;
    32     }
    33     cntu = 0;
    34     cntd = 0;
    35     for(int i = 1; i <= n; i++) {
    36         scanf("%d", &num[i]);
    37     }
    38     for(int i = 1; i <= k; i++) {
    39         upstream.push(i);
    40         downstream.push(i);
    41     }
    42     mmin[cntd++] = num[downstream.top()];
    43     mmax[cntu++] = num[upstream.top()];
    44     for(int i = k + 1; i <= n; i++) {
    45         upstream.push(i);
    46         downstream.push(i);
    47         while(i - downstream.top() >= k) {
    48             downstream.pop();
    49         }
    50         mmin[cntd++] = num[downstream.top()];
    51         while(i - upstream.top() >= k) {
    52             upstream.pop();
    53         }
    54         mmax[cntu++] = num[upstream.top()];
    55     }
    56     for(int i = 0; i < cntd; i++) {
    57         cout << mmin[i] << " ";
    58     }
    59     cout << endl;
    60     for(int i = 0; i < cntu; i++) {
    61         cout << mmax[i] << " ";
    62     }
    63     cout << endl;
    64 }

    Use C++ to compile. Or it'll TLE...

      1 #include <algorithm>
      2 #include <iostream>
      3 #include <iomanip>
      4 #include <cstring>
      5 #include <climits>
      6 #include <complex>
      7 #include <fstream>
      8 #include <cassert>
      9 #include <cstdio>
     10 #include <bitset>
     11 #include <vector>
     12 #include <deque>
     13 #include <queue>
     14 #include <stack>
     15 #include <ctime>
     16 #include <set>
     17 #include <map>
     18 #include <cmath>
     19 
     20 using namespace std;
     21 
     22 inline bool scan_d(int &num) {
     23     char in;bool IsN=false;
     24     in=getchar();
     25     if(in==EOF) return false;
     26     while(in!='-'&&(in<'0'||in>'9')) in=getchar();
     27     if(in=='-'){ IsN=true;num=0;}
     28     else num=in-'0';
     29     while(in=getchar(),in>='0'&&in<='9'){
     30             num*=10,num+=in-'0';
     31     }
     32     if(IsN) num=-num;
     33     return true;
     34 }
     35 
     36 typedef struct Node {
     37     int idx;
     38     int val;
     39     Node() {}
     40     Node(int ii, int vv) : idx(ii), val(vv) {}
     41 }Node;
     42 const int maxn = 1000010;
     43 int n, k;
     44 int a[maxn];
     45 Node q[maxn];
     46 int front, tail;
     47 
     48 int main() {
     49 //    freopen("in", "r", stdin);
     50     while(~scanf("%d %d", &n, &k)) {
     51         for(int i = 0; i < n; i++) {
     52             scan_d(a[i]);
     53         }
     54         front = 0,  tail = 0;
     55         q[tail++] = Node(0, a[0]);
     56         int ans[maxn], cnt = 0;
     57         for(int i = 1; i < k; i++) {
     58             while(front < tail && q[tail-1].val >= a[i]) {
     59                 tail--;
     60             }
     61             q[tail++] = Node(i, a[i]);
     62         }
     63         ans[cnt++] = q[front].val;
     64         for(int i = k; i < n; i++) {
     65             while(front < tail && q[tail-1].val >= a[i]) {
     66                 tail--;
     67             }
     68             while(front < tail && i - k + 1 > q[front].idx) {
     69                 front++;
     70             }
     71             q[tail++] = Node(i, a[i]);
     72             ans[cnt++] = q[front].val;
     73         }
     74         for(int i = 0; i < cnt; i++) {
     75             printf("%d ", ans[i]);
     76         }
     77         puts("");
     78         front = 0, tail = 0;
     79         q[tail++] = Node(0, a[0]);
     80         cnt = 0;
     81         for(int i = 1; i < k; i++) {
     82             while(front < tail && q[tail-1].val <= a[i]) {
     83                 tail--;
     84             }
     85             q[tail++] = Node(i, a[i]);
     86         }
     87         ans[cnt++] = q[front].val;
     88         for(int i = k; i < n; i++) {
     89             while(front < tail && q[tail-1].val <= a[i]) {
     90                 tail--;
     91             }
     92             while(front < tail && i - k + 1 > q[front].idx) {
     93                 front++;
     94             }
     95             q[tail++] = Node(i, a[i]);
     96             ans[cnt++] = q[front].val;
     97         }
     98         for(int i = 0; i < cnt; i++) {
     99             printf("%d ", ans[i]);
    100         }
    101         puts("");
    102     }
    103     return 0;
    104 }
  • 相关阅读:
    codevs 3160 最长公共子串
    bzoj1593 [Usaco2008 Feb]Hotel 旅馆
    bzoj1230 [Usaco2008 Nov]lites 开关灯
    洛谷P1558 色板游戏
    洛谷P2253 好一个一中腰鼓!
    洛谷P2345 奶牛集会
    TopCoder SRM420 Div1 500pt RedIsGood
    洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
    洛谷P1455 搭配购买
    洛谷P2398 GCD SUM
  • 原文地址:https://www.cnblogs.com/kirai/p/4769233.html
Copyright © 2011-2022 走看看