#include <iostream>
#include <queue>
using namespace std;
//输入一个数组和一个窗口,输出一个数组代表移动窗口的最大值
struct node
{
int val;
int index;
node(int v, int i):val(v),index(i){}
};
struct cmp
{
bool operator()(const node& n1, const node& n2)
{
return n1.val < n2.val;
}
};
void maxVal(int A[], int n, int w)
{
priority_queue<node, vector<node>, cmp> que;
int re[w];
for(int i=0; i<w; i++)
{
que.push(node(A[i], i));
}
for(int i=w; i<n; i++)
{
node t=que.top();
re[i-w]=t.val;
if(t.index<=i-w)
que.pop();
que.push(node(A[i],i));
}
re[n-w]=que.top().val;
for(int i=0; i<=n-w; ++i)
cout<<re[i]<<endl;
}
int main()
{
int a[]={8,1,7,3,9,7,3};
maxVal(a, 7, 3);
return 0;
}
#include <queue>
using namespace std;
//输入一个数组和一个窗口,输出一个数组代表移动窗口的最大值
struct node
{
int val;
int index;
node(int v, int i):val(v),index(i){}
};
struct cmp
{
bool operator()(const node& n1, const node& n2)
{
return n1.val < n2.val;
}
};
void maxVal(int A[], int n, int w)
{
priority_queue<node, vector<node>, cmp> que;
int re[w];
for(int i=0; i<w; i++)
{
que.push(node(A[i], i));
}
for(int i=w; i<n; i++)
{
node t=que.top();
re[i-w]=t.val;
if(t.index<=i-w)
que.pop();
que.push(node(A[i],i));
}
re[n-w]=que.top().val;
for(int i=0; i<=n-w; ++i)
cout<<re[i]<<endl;
}
int main()
{
int a[]={8,1,7,3,9,7,3};
maxVal(a, 7, 3);
return 0;
}