POJ 2823 Sliding Window【线段树+单调队列】

 

Description

An array of sizen≤ 106is given to you. There is a sliding window of sizekwhich is moving from the very left of the array to the very right. You can only see theknumbers 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], andkis
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 integersnandkwhich are the lengths of the array and the sliding window. There arenintegers 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

code:

 

View Code
#include<stdio.h>
int data[1000010];
struct tnode
{
int left,right;
int min,max;
}t[1000010*4];
struct out
{
int min,max;
}dataout[1000010];
void creat(int st,int ed,int r)
{
int mid;
t[r].left=st;
t[r].right=ed;
if(st==ed)
{
t[r].min=t[r].max=data[st];
return ;
}
mid=(st+ed)>>1;
creat(st,mid,r*2);
creat(mid+1,ed,r*2+1);

t[r].min=t[r*2].min;
if(t[r].min>t[r*2+1].min)
t[r].min=t[r*2+1].min;
t[r].max=t[r*2].max;
if(t[r].max<t[r*2+1].max)
t[r].max=t[r*2+1].max;
}
void search(int st,int ed,int & min,int & max,int r=1)
{
int mid;
int min2,max2;
if(st==t[r].left&&ed==t[r].right)
{
min=t[r].min;
max=t[r].max;
return ;
}
mid=(t[r].left+t[r].right)>>1;
if(mid>=ed)
search(st,ed,min,max,r*2);
else if(mid<st)
search(st,ed,min,max,r*2+1);
else
{
search(st,mid,min,max,r*2);
search(mid+1,ed,min2,max2,r*2+1);
if(min>min2)
min=min2;
if(max<max2)
max=max2;
}
}
int main()
{
int i,n,k;
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
scanf("%d",&data[i]);
creat(1,n,1);
for(i=1;i<=n-k+1;i++)
search(i,i+k-1,dataout[i].min,dataout[i].max,1);

printf("%d",dataout[1].min);
for(i=2;i<=n-k+1;i++)
printf(" %d",dataout[i].min);
putchar('\n');

printf("%d",dataout[1].max);
for(i=2;i<=n-k+1;i++)
printf(" %d",dataout[i].max);
putchar('\n');
return 0;
}

 

单调队列1:

View Code
#include<stdio.h>
#include<string.h>
int front1=1,front2=1,rear1=1,rear2=1;
int a[1100010];
int q1[1100010];
int q2[1100010];
int an[1100010];
int main()
{
int n,m,i;
scanf("%d%d",&n,&m);
if(m==1)
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
printf("%d ",a[i]);
}
printf("\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
else
{
scanf("%d",&a[1]);
q1[1]=q2[1]=1;
for(i=2;i<m;i++)
{
scanf("%d",&a[i]);
while(a[q1[rear1]]>=a[i]&&front1<=rear1)
rear1--;
q1[++rear1]=i;
while(a[q2[rear2]]<=a[i]&&front2<=rear2)
rear2--;
q2[++rear2]=i;
}
for(i=m;i<=n;i++)
{
scanf("%d",&a[i]);

while(a[q1[rear1]]>=a[i]&&front1<=rear1)
rear1--;
q1[++rear1]=i;
printf("%d ",a[q1[front1]]);
if(q1[front1]==i-m+1)
front1++;

while(a[q2[rear2]]<=a[i]&&front2<=rear2)
rear2--;
q2[++rear2]=i;
an[i]=a[q2[front2]];
if(q2[front2]==i-m+1)
front2++;
}
printf("\n");
for(i=m;i<=n;i++)
printf("%d ",an[i]);
printf("\n");
}
return 0;
}

 


posted @ 2012-03-15 00:34  'wind  阅读(192)  评论(0编辑  收藏  举报