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
  • 相关阅读:
    如何解决列表框控件宽度不够的问题
    在windows Forms程序里面实现文件上传
    TFS的一些信息
    百钱百鸡问题
    如何移动SQL SERVER的系统数据库
    数据分页技术
    如何在报表中直接使用数据库中存储的图片
    重新注册MSJetOledb 4.0引擎
    2007 Microsoft Office Servers 已知问题/自述文件
    使用For XML与XSL(XSLT)配套快速输出查询结果到Web页面
  • 原文地址:https://www.cnblogs.com/acvc/p/3601580.html
Copyright © 2011-2022 走看看