代码拉取完成,页面将自动刷新
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int N =100010;
int n;
int q[N] , hh = 0 , tt = -1;
//////栈内存的是滑窗的下标
void solo_queue(int a[] , int n , int k){
for(int i = 0 ; i < n ;++i){
int j = i + 1;
/// j < i + k
if(hh <= tt){
cout<<"出队元素:"<<a[q[hh++]]<<endl;
}
while(j < (i + k)&&a[j] < a[q[tt]]){
q[tt++] = j++ ;
cout<<"入队元素:"<<a[j]<<endl;
}
}
}
void yxc(int a[] , int n ,int k){
vector<int> res;
hh = 0 , tt = -1;
/////队首内存的是a[i --- i + k -1]的最大值下标
for(int i = 0 ; i < n ;++i){
/////出队:队非空 且 对应新窗口, 窗口左边已过最大值下标
if(hh <= tt && i - k + 1 > q[hh]){
cout<<"出队出滑窗的首元素(max):"<<i - k + 1<<" "<<q[hh]<<" "<<a[q[hh]]<<endl;
++hh;
}
///////保证队尾 大于 a[i]
while(hh <= tt && a[q[tt]] <= a[i] ){
cout<<"出队尾元素:"<<a[q[hh]]<<endl;
--tt;
}
q[++tt] = i;
cout<<"入队尾元素:"<<a[i]<<endl;
if (i - k + 1 >= 0 && hh <= tt){
res.push_back(a[q[hh]]);
cout<<"输出当前滑窗最大值:"<<a[q[hh]]<<endl;
}
}
for(auto i : res){
cout<<i<<" ";
}
printf("\n");
}
// 8 3
// 1 3 -1 -3 5 3 6 7
int main(){
int k ;
scanf("%d%d" , &n , &k);
int a[n];
for(int i = 0 ; i < n; ++i) scanf("%d" , &a[i]);
// solo_queue(a , n , k);
yxc(a , n , k);
printf("C 艹 mother fucker\n");
system("pause");
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。