zoukankan      html  css  js  c++  java
  • POJ 2823 Sliding Window

    Sliding Window
    Time Limit: 12000MSMemory Limit: 65536K
    Total Submissions: 35486Accepted: 10492
    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 -13
     1 [3  -1  -3] 5  3  6  7 -33
     1  3 [-1  -3  5] 3  6  7 -35
     1  3  -1 [-3  5  3] 6  7 -35
     1  3  -1  -3 [5  3  6] 7 36
     1  3  -1  -3  5 [3  6  7]37

    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
    PS:单调队列水体。
    妈蛋poj还卡scanf,G++超时,换c++输入会快些

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int MAX = 1e6+10;
     6 int a[MAX],b[MAX],pos1[MAX],pos2[MAX];
     7 int ans1[MAX],ans2[MAX];
     8 int main()
     9 {
    10     int x,n,k; int cur1,cur2;
    11     scanf("%d %d",&n,&k);
    12     int rear=0,front=0; cur1=cur2=0;
    13     int rear2=0,front2=0;
    14     for(int i=0;i<n;i++)
    15     {
    16         scanf("%d",&x);
    17         while(front<rear&&a[rear-1]>x) rear--;
    18         a[rear]=x; pos1[rear++]=i;
    19         while(front2<rear2&&b[rear2-1]<x) rear2--;
    20         b[rear2]=x; pos2[rear2++]=i;
    21         if(i>=k-1)
    22         {
    23             ans1[cur1++]=a[front];
    24             ans2[cur2++]=b[front2];
    25             if(i-pos1[front]+1>=k) front++;
    26             if(i-pos2[front2]+1>=k) front2++;
    27         }
    28     }
    29     for(int i=0;i<cur1;i++)
    30     {
    31         if(i) printf(" %d",ans1[i]);
    32         else printf("%d",ans1[i]);
    33     }
    34     printf(" ");
    35     for(int i=0;i<cur2;i++)
    36     {
    37         if(i) printf(" %d",ans2[i]);
    38         else printf("%d",ans2[i]);
    39     }
    40     printf(" ");
    41 
    42     return 0;
    43 }
    mycode
  • 相关阅读:
    【bzoj2821】作诗(Poetize)
    ZOJ-2112-Dynamic Rankings(线段树套splay树)
    POJ- 2104 hdu 2665 (区间第k小 可持久化线段树)
    hust-1024-dance party(最大流--枚举,可行流判断)
    hdu-3046-Pleasant sheep and big big wolf(最大流最小割)
    POJ-3294-Life Forms(后缀数组-不小于 k 个字符串中的最长子串)
    POJ-Common Substrings(后缀数组-长度不小于 k 的公共子串的个数)
    POJ-2774-Long Long Message(后缀数组-最长公共子串)
    POJ-3693-Maximum repetition substring(后缀数组-重复次数最多的连续重复子串)
    spoj-694-Distinct Substrings(后缀数组)
  • 原文地址:https://www.cnblogs.com/acvc/p/3601580.html
Copyright © 2011-2022 走看看