/*
题意:
有N个数,每次从左到右选取M个数,第一行选取每个区间中的最小值输出,第二行选取最大值并输出。
*/
# include <stdio.h>
# include <string.h>
# include <algorithm>
# include <queue>
# include <vector>
using namespace std;
int a[1000010],maxx[1000010],minn[1000010];
struct cmp1
{
bool operator()(const int a1,const int a2)
{
return a[a1]>a[a2];//从小到大
}
};
struct cmp2
{
bool operator()(const int a1,const int a2)
{
return a[a1]<a[a2];
}
};
int main()
{
priority_queue<int,vector<int>,cmp1>q1;//小
priority_queue<int,vector<int>,cmp2>q2;
int n,i,k;
while(~scanf("%d%d",&n,&k))
{
for(i=1; i<=n; i++)
scanf("%d",&a[i]);
while(!q1.empty())
q1.pop();
while(!q2.empty())
q2.pop();
for(i=1; i<=k; i++)
{
q1.push(i);
q2.push(i);
}
int cot=0;
maxx[cot]=a[q2.top()];
minn[cot++]=a[q1.top()];
for(i=k+1 ; i<=n; i++)
{
q1.push(i);
q2.push(i);
while(i-q1.top()>=k)
q1.pop();
while(i-q2.top()>=k)
q2.pop();
maxx[cot]=a[q2.top()];
minn[cot++]=a[q1.top()];
}
for(i=0; i<cot; i++)
{
if(i==cot-1)
printf("%d\n",minn[i]);
else
printf("%d ",minn[i]);
}
for(i=0; i<cot; i++)
{
if(i==cot-1)
printf("%d\n",maxx[i]);
else
printf("%d ",maxx[i]);
}
}
return 0;
}
poj2823 Sliding Window (优先队列)
最新推荐文章于 2022-01-18 12:22:19 发布