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

    Sliding Window

    Time Limit: 12000MS
    Memory Limit: 65536K

    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 position
    Minimum value
    Maximum 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
     
       1: #include <iostream>
       2: #include <cstdio>
       3: #include <cstring>
       4: #include <algorithm>
       5: using namespace std;
       6: #define lson l,m,rt<<1
       7: #define rson m+1,r,rt<<1|1
       8: typedef long long ll;
       9: const int maxn=1111111;
      10: int a[maxn<<2],b[maxn<<2];
      11: int small[maxn],big[maxn],cnt=0;
      12:  
      13: void PushUp(int rt)
      14: {
      15:     a[rt]=max(a[rt<<1],a[rt<<1|1]);
      16:     b[rt]=min(b[rt<<1],b[rt<<1|1]);
      17: }
      18:  
      19: void build(int l,int r,int rt)
      20: {
      21:     if(l==r){
      22:         scanf("%d",&a[rt]);
      23:         b[rt]=a[rt];
      24:         return;
      25:     }
      26:     int m=(l+r)>>1;
      27:     build(lson); build(rson);
      28:     PushUp(rt);
      29: }
      30:  
      31: void query(int L,int R,int l,int r,int rt,int &ma,int &mi)
      32: {
      33:     if(L<=l&&R>=r)
      34:     {
      35:         ma=a[rt];
      36:         mi=b[rt];
      37:         return;
      38:     }
      39:     int m=(r+l)>>1;
      40:     if(L<=m&&R>m)
      41:     {
      42:         int x1,y1,x2,y2;
      43:         query(L,R,lson,x1,y1);
      44:         query(L,R,rson,x2,y2);
      45:         ma=max(x1,x2);
      46:         mi=min(y1,y2);
      47:     }
      48:     else if(L<=m)
      49:         query(L,R,lson,ma,mi);
      50:     else
      51:         query(L,R,rson,ma,mi);
      52: }
      53:  
      54: void show(int n,int e[])
      55: {
      56:     for(int i=0; i<n ; i++){
      57:         if(i) printf(" ");
      58:         printf("%d",e[i]);
      59:     }
      60:     printf("
    ");
      61: }
      62:  
      63: void run(int n,int k)
      64: {
      65:     cnt=0;
      66:     build(1,n,1);
      67:     for(int i=k; i<=n; i++)
      68:     {
      69:         query(i-k+1,i,1,n,1,big[cnt],small[cnt]);
      70:         cnt++;
      71:     }
      72:     show(cnt,small);
      73:     show(cnt,big);
      74: }
      75:  
      76: int main()
      77: {
      78:     int n,k;
      79:     while(scanf("%d%d",&n,&k)!=EOF)
      80:         run(n,k);
      81:     return 0;
      82: }
     
  • 相关阅读:
    基于maven+ssm的增删改查之测试相关ssm环境是否成功
    基于maven+ssm的增删改查之使用mybatis逆向工程生成相关文件
    基于maven+ssm的增删改查之spring+springmvc+mybatis环境搭建
    基于maven+ssm的增删改查之maven环境的搭建
    maven之在eclipse中创建maven项目
    maven之相关概念
    Unrecognised tag: 'snapshotPolicy'
    maven之第一个maven程序
    由字符串反转(使用递归)引申出来一道Java面试题
    Java 面试题问与答:编译时与运行时
  • 原文地址:https://www.cnblogs.com/zyx1314/p/3606064.html
Copyright © 2011-2022 走看看