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

    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

     
     
    正解:单调队列
    解题报告:
      单调队列裸题。发现提交用G++就会TLE,换成 C++就迷之AC了。。。
     
     
     1 //It is made by jump~
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <algorithm>
     8 #include <ctime>
     9 #include <vector>
    10 #include <queue>
    11 #include <map>
    12 #include <set>
    13 #ifdef WIN32   
    14 #define OT "%I64d"
    15 #else
    16 #define OT "%lld"
    17 #endif
    18 using namespace std;
    19 typedef long long LL;
    20 const int MAXN = 2000011;
    21 int n,k;
    22 int ans[MAXN],ans2[MAXN];
    23 struct node{
    24     int val,id;
    25 }a[MAXN];
    26 struct que{
    27     int head,tail;
    28     node dui[MAXN];
    29 }a1,a2;
    30 
    31 inline int getint()
    32 {
    33        int w=0,q=0;
    34        char c=getchar();
    35        while((c<'0' || c>'9') && c!='-') c=getchar();
    36        if (c=='-')  q=1, c=getchar();
    37        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
    38        return q ? -w : w;
    39 }
    40 
    41 inline void work(){
    42     n=getint(); k=getint();
    43     for(int i=1;i<=n;i++) a[i].val=getint(),a[i].id=i;
    44     a1.head=a2.head=1;
    45     for(int i=1;i<=n;i++) {
    46     if(i>k) {
    47         if(i-a1.dui[a1.head].id>=k) a1.head++;       
    48         if(i-a2.dui[a2.head].id>=k) a2.head++;
    49     }
    50     while(a1.tail>=a1.head && a1.dui[a1.tail].val<=a[i].val) a1.tail--;
    51     while(a2.tail>=a2.head && a2.dui[a2.tail].val>=a[i].val) a2.tail--;
    52     a1.dui[++a1.tail]=a[i]; a2.dui[++a2.tail]=a[i];
    53     if(i>=k) ans[i]=a1.dui[a1.head].val,ans2[i]=a2.dui[a2.head].val; 
    54     }
    55     for(int i=k;i<=n;i++) printf("%d ",ans2[i]); printf("
    ");
    56     for(int i=k;i<=n;i++) printf("%d ",ans[i]);
    57 }
    58 
    59 int main()
    60 {
    61   work();
    62   return 0;
    63 }
  • 相关阅读:
    Map Rectifier
    倡议“开源GIS团队”成员集思广言
    动力火车终于明白
    搜索操作被google拒绝
    请教ArcGIS server 9.2做的题目的部署问题
    总结基于ArcGIS Server 9.2 Dot Net ADF的WebGIS项目部署问题
    读:『博客家园』 [博客人生]结婚是因为无路可逃
    [Gdaldev] gdalwarp/gcps and ERROR 1: Failed to compute polynomial equations of desired order
    [Gdaldev]用GCPs纠正影像的完整代码(多项式纠正)
    转:将 Oracle Spatial 与 Google Earth 集成
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5732894.html
Copyright © 2011-2022 走看看