Ai
1 Star 0 Fork 0

ashley/Algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
solo_queue.cpp 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
ashley 提交于 2021-09-21 14:36 +08:00 . DST
#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");
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/ashleyZZ/algorithm.git
git@gitee.com:ashleyZZ/algorithm.git
ashleyZZ
algorithm
Algorithm
master

搜索帮助