使用双端队列
int main(void)
{
int arr[12] = { 5,4,3,6,5,4,7,8,4,3,4,5 };
int w = 3; //设定窗口为3
int index = 0;
int* ret = new int[12 - 3 + 1]; //出现12-3+1个窗口
memset(ret, 0, sizeof(int) * 10); //初始化
int R = 0;
deque<int> window;
for (int i = R; i < 12; i++)
{
while (!window.empty() && arr[window.back()] <= arr[i]) //使队列成递减
{
window.pop_back();
}
window.push_back(i);
if (i - w == window.front()) //如果离开窗口则弹出
{
window.pop_front();
}
if (i >= w - 1) //满足窗口大小开始收集
{
ret[index++] = arr[window.front()];
}
}
for (int i = 0; i < 12-3+1; i++)
{
cout << ret[i] << ' ';
}
return 0;
}