codevs4373 窗口==poj2823 Sliding Window

Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 53676   Accepted: 15399
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  -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

4373 窗口

 

 时间限制: 1 s
 空间限制: 256000 KB
 题目等级 : 黄金 Gold
 
 
 
 
 
题目描述 Description

给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表:

Window position Min value  Max 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

你的任务是找出窗口在各位置时的max value, min value.

输入描述 Input Description

第1行n,k,第2行为长度为n的数组

 

 

输出描述 Output Description

2行

第1行每个位置的min value

第2行每个位置的max value

 

样例输入 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

 

数据范围及提示 Data Size & Hint

数据范围:20%: n<=500; 50%: n<=100000;100%: n<=1000000;

分类标签 Tags 点此展开 

 
暂无标签
 

 

 2017-03-27

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=1e6+10;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,m,a[N],q[N],id[N];
int main(){
    n=read();m=read();
    for(int i=1;i<=n;i++) a[i]=read();
    int h=0,t=0;
    for(int i=1;i<m;i++){
        while(h<t&&i-id[h]>=m) h++;
        while(h<t&&q[t-1]>a[i]) t--;
        q[t]=a[i];id[t++]=i;
    }
    for(int i=m;i<=n;i++){
        while(h<t&&i-id[h]>=m) h++;
        while(h<t&&q[t-1]>a[i]) t--;
        q[t]=a[i];id[t++]=i;
        if(h<t) printf("%d ",q[h]);
    }
    putchar('\n');
    h=0,t=0;
    for(int i=1;i<m;i++){
        while(h<t&&i-id[h]>=m) h++;
        while(h<t&&q[t-1]<a[i]) t--;
        q[t]=a[i];id[t++]=i;
    }
    for(int i=m;i<=n;i++){
        while(h<t&&i-id[h]>=m) h++;
        while(h<t&&q[t-1]<a[i]) t--;
        q[t]=a[i];id[t++]=i;
        if(h<t) printf("%d ",q[h]);
    }
    return 0;
}

 

AC代码(裸的单调队列)c++提交
#include<cstdio>
using namespace std;
inline int read(){
    register int x=0;bool f=1;
    register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return f?x:-x;
}
const int N=1e6+10;
int n,k,a[N],num[N],q[N];
int main(){
    n=read();k=read();
    for(int i=1;i<=n;i++) a[i]=read();
    int l=1,r=1;
    for(int i=1;i<k;i++){
        for(;l<=r&&i-num[l]>=k;l++);
        for(;l<=r&&a[i]<q[r];r--);
        q[++r]=a[i];num[r]=i;
    }
    for(int i=k;i<=n;i++){
        for(;l<=r&&i-num[l]>=k;l++);
        for(;l<=r&&a[i]<q[r];r--);
        q[++r]=a[i];num[r]=i;
        printf("%d ",q[l]);
    }
    putchar('\n');
    l=1,r=1;
    for(int i=1;i<k;i++){
        for(;l<=r&&i-num[l]>=k;l++);
        for(;l<=r&&a[i]>q[r];r--);
        q[++r]=a[i];num[r]=i;
    }
    for(int i=k;i<=n;i++){
        for(;l<=r&&i-num[l]>=k;l++);
        for(;l<=r&&a[i]>q[r];r--);
        q[++r]=a[i];num[r]=i;
        printf("%d ",q[l]);
    }
    return 0;
}

 

posted @ 2016-07-25 18:02  神犇(shenben)  阅读(666)  评论(0编辑  收藏  举报