poj 2823 Sliding Window

本文介绍了一种使用线段树解决滑动窗口最大最小值问题的方法。具体实现包括构建线段树、查询最大最小值等步骤。适用于数组长度不超过10^6的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 33094 Accepted: 9831
Case Time Limit: 5000MS

Description

An array of size n ≤ 10 6 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

Source

 
 1 //43796K    8766MS    C++    1712B    2013-10-13 21:42:22
 2 /*
 3 
 4     题意:
 5         有n个数字,求每连续的m个中的最大最小数,并分别输出
 6     
 7     线段树:
 8         线段树小变形,不过我的做法好像耗的时空有点大= =、 
 9         query()中加了一个flag判断询问的是最大值还是最小值 
10 
11 */
12 #include<stdio.h>
13 #include<string.h>
14 #include<stdlib.h>
15 #define N 1000005
16 #define inf 0x7ffffff
17 struct node{
18     int l;
19     int r;
20     int nmax;
21     int nmin;
22 }tree[4*N];
23 int p[N],n,m;
24 int ansmax[N];
25 int ansmin[N];
26 int Max(int a,int b)
27 {
28     return a>b?a:b;
29 }
30 int Min(int a,int b)
31 {
32     return a<b?a:b;
33 }
34 void build(int l,int r,int id)
35 {
36     tree[id].l=l;
37     tree[id].r=r;
38     if(l==r){
39         tree[id].nmax=p[l];tree[id].nmin=p[l];
40         return;
41     }
42     int mid=(l+r)/2;
43     build(l,mid,2*id);
44     build(mid+1,r,2*id+1);
45     tree[id].nmax=Max(tree[2*id].nmax,tree[2*id+1].nmax);
46     tree[id].nmin=Min(tree[2*id].nmin,tree[2*id+1].nmin);
47 }
48 int query(int l,int r,int id,bool flag)
49 {
50     if(tree[id].l>r || tree[id].r<l){
51         return flag?inf:-inf;
52     }
53     if(tree[id].l>=l && tree[id].r<=r){
54         return flag?tree[id].nmax:tree[id].nmin;
55     }
56     int mid=(tree[id].l+tree[id].r)/2;
57     if(mid>=r) return query(l,r,2*id,flag);
58     else if(mid<l) return query(l,r,2*id+1,flag);
59     else{
60         if(flag)
61             return Max(query(l,mid,2*id,flag),query(mid+1,r,2*id+1,flag));
62         else return Min(query(l,mid,2*id,flag),query(mid+1,r,2*id+1,flag));
63     }
64 }
65 int main(void)
66 {
67     while(scanf("%d%d",&n,&m)!=EOF)
68     {
69         for(int i=1;i<=n;i++) 
70             scanf("%d",&p[i]);
71         build(1,n,1);
72         for(int i=0;i<=n-m;i++){
73             ansmax[i]=query(i+1,i+m,1,true);
74             ansmin[i]=query(i+1,i+m,1,false);
75         }
76         for(int i=0;i<=n-m;i++)
77             printf(i==n-m?"%d\n":"%d ",ansmin[i]);
78         for(int i=0;i<=n-m;i++)
79             printf(i==n-m?"%d\n":"%d ",ansmax[i]);
80     }
81     return 0;
82 }

 

转载于:https://www.cnblogs.com/GO-NO-1/articles/3367221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值