数组实现链表,栈(单调)和队列(单调)
链表
单链表
例题
算法题中用数组实现链表,时间费用低
// head存储链表头,e[]存储节点的值,ne[]存储节点的next指针,idx表示当前用到了哪个节点
int head, e[N], ne[N], idx;
// 初始化
void init()
{
head = -1;
idx = 0;
}
//在链中加入元素
void add(int k,int x)
{
e[idx]=x;
ne[idx]=ne[k];
ne[k]=idx;
idx++;
}
// 在链表头插入一个数a
void insert(int a)
{
e[idx] = a, ne[idx] = head, head = idx ++ ;
}
//删除操作
void del(int k)
{
ne[k] = ne[ne[k]];
}
// 将头结点删除,需要保证头结点存在
void remove()
{
head = ne[head];
}
双链表
已知一个元素可以查询到前后邻接元素,不用像单链表那样去遍历整个数组了
// e[]表示节点的值,l[]表示节点的左指针,r[]表示节点的右指针,idx表示当前用到了哪个节点
int e[N], l[N], r[N], idx;
// 初始化
void init()
{
//0是左端点,1是右端点
r[0] = 1, l[1] = 0;
idx = 2;
}
// 在节点a的右边插入一个数x
void insert(int a, int x)
{
e[idx] = x;
l[idx] = a, r[idx] = r[a];
l[r[a]] = idx, r[a] = idx ++ ;
}
// 删除节点a
void remove(int a)
{
l[r[a]] = l[a];
r[l[a]] = r[a];
}
栈
数组栈
不过我习惯用STL
// tt表示栈顶
int stk[N], tt = 0;
// 向栈顶插入一个数
stk[ ++ tt] = x;
// 从栈顶弹出一个数
tt -- ;
// 栈顶的值
stk[tt];
// 判断栈是否为空
if (tt > 0)
{
}
单调栈
常见模型:找出每个数左边离它最近的比它大/小的数
int tt = 0;
for (int i = 1; i <= n; i ++ )
{
while (tt && check(stk[tt], i)) tt -- ;
stk[ ++ tt] = i;
}
用的STL,找到每个数左边距离最近比他小的数
#include <iostream>
#include <stack>
using namespace std;
int n;
int main() {
cin >> n;
stack<int> st;
for (int i = 1; i <= n; i ++ ) {
int x;
cin >> x;
while (!st.empty() && st.top() > x)
st.pop();
if (!st.empty())
cout << st.top() << ' ';
else
cout << -1 << ' ';
st.push(x);
}
return 0;
}
队列
数组队列
// hh 表示队头,tt表示队尾
int q[N], hh = 0, tt = -1;
// 向队尾插入一个数
q[ ++ tt] = x;
// 从队头弹出一个数
hh ++ ;
// 队头的值
q[hh];
// 判断队列是否为空
if (hh <= tt)
{
}
单调队列
例题
滑动窗口
常见模型:找出滑动窗口中的最大值/最小值
int hh = 0, tt = -1;
for (int i = 0; i < n; i ++ )
{
while (hh <= tt && check_out(q[hh])) hh ++ ; // 判断队头是否滑出窗口
while (hh <= tt && check(q[tt], i)) tt -- ;
q[ ++ tt] = i;
}
数组维护
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e6+10;
int a[N],q[N];
int main()
{
int n,k;
cin >> n >> k;
int hh=0,tt=-1;
for (int i = 0; i < n; i ++ ) cin >> a[i];
for (int i = 0; i < n; i ++ )
{
if(i-k+1>q[hh]) hh++;
while(hh<=tt&&a[q[tt]]>=a[i]) tt--;
q[ ++ tt] = i;
if(i >= k-1) cout << a[q[hh]] << ' ';
}
cout << endl;
hh=0,tt=-1;
for (int i = 0; i < n; i ++ )
{
if(i-k+1>q[hh]) hh++;
while(hh<=tt&&a[q[tt]]<=a[i]) tt--;
q[ ++ tt] = i;
if(i >= k-1) cout << a[q[hh]] << ' ';
}
}
STL要用双端队列
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
using namespace std;
const int N = 1e6+10;
int a[N];
int n,m;
int main()
{
cin >> n >> m;
deque<int> q;
for (int i = 0; i < n; i ++ ) cin >> a[i];
for (int i = 0; i < n; i ++ )
{
if(i - m + 1 > q.front()) q.pop_front();
while(!q.empty() && a[q.back()] >= a[i]) q.pop_back();
q.push_back(i);
if(i >= m - 1) cout << a[q.front()] << ' ';
}
cout << endl;
q.clear();
for (int i = 0; i < n; i ++ )
{
if(i - m + 1 > q.front()) q.pop_front();
while(!q.empty() && a[q.back()] <= a[i]) q.pop_back();
q.push_back(i);
if(i >= m - 1) cout << a[q.front()] << ' ';
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】