跳到正文

个人记录

各个算法标准代码

txw405约 21 分钟
原文

这是 2026年10月28日 存档的第 4 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年2月4日
收录
2026年7月31日
更新
2026年11月14日
篇幅
约 21 分钟

版本

  1. v5当前
  2. v4
  3. v3
  4. v2
更早的 1 个版本
  1. v1
一些很有用的知识:

2维vector数组定义:

  • 空二维 vector(先定义,后赋值) 适用于提前不知道行列数,后续动态添加数据的场景

vector<vector<int>>mat;

  • 指定「行数 + 列数」,默认初始化(最常用) 直接固定二维数组的行列数,数值类型默认初始化为 0

vector<vector<int>>mat(3,vector<int>(4));

  • 指定「行数 + 列数 + 自定义初始值」 统一设置所有元素的初始值

vector<vector<int>>mat(3,vector<int>(4,10));

各种算法标准代码:

并查集
C++
#include <bits/stdc++.h>
using namespace std;
int f[5005];//f[i]表示i的目前最远祖先
int findfather(int x){//找到x的最远祖先
	if(f[x]==x){
		return x;//直接返回
	}
	return f[x]=findfather(f[x]);//压缩路径
}
int main(){
	int n=1,m,ans,p;
	int x,y;
	cin>>n>>m>>p;//n->人数 m->联系总数 p->询问次数
	ans=n;//ans->当前集体总数
	for(int i=1;i<=n;i++){
		f[i]=i;//初始化
	}
	for(int i=1;i<=m;i++){
		cin>>x>>y;
		x=findfather(x);
		y=findfather(y);//找到各自的祖先
		if(x!=y){
			ans--;//集体合并导致总数减少
		}
		f[y]=x;//合并
	}
	for(;p>=1;p--){
		cin>>x>>y;//查询
		cout<<(findfather(x)==findfather(y)?"Yes":"No");
	}//输出查询结果
}
美国血统(求后序遍历)
C++
#include <bits/stdc++.h>
using namespace std;
void dfs(string bef,string mid){
	if(bef.empty()){//若先序为空则结束
		return;
	}
	char root=bef[0];//找根
	int key=mid.find(root);//找到根的中序遍历位置
	bef.erase(bef.begin());//销毁根节点,防止干扰
	dfs(bef.substr(0,key),mid.substr(0,key));//左子树
	dfs(bef.substr(key),mid.substr(key+1));//右子树
	cout<<root;//根(后序遍历特点)
	return;//防止死循环(也有心理安慰作用)
}
int main(){
	string bef;//bef->先序遍历
	string mid;//mid->中序遍历
	cin>>mid>>bef;
	dfs(bef,mid);
	return 0;
}
单调栈
C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
deque<ll>a,b;//a->单调递增栈 b->单调递减栈
deque<ll>f,g;//f->每个元素之后第1个大于它的数的下标 g->个元素之后第1个小于它的数的下标
vector<ll>num;//num->记录数字
int n,x;//n->数量 x->当前数字
void slove(){//处理每个元素之后第1个大于和小于它的数的下标
	for(int i=n;i>=1;i--){
		while(!a.empty()&&num[a.back()]<=num[i]){//维持递增
			a.pop_back();
		}
		if(a.empty()){//没有大于它的数
			f.push_front(0);//压入0
		}
		else{//否则压入第1个大于它的数的下标
			f.push_front(a.back());
		}
		a.push_back(i);//入栈
	}
	for(int i=n;i>=1;i--){//同理
		while(!b.empty()&&num[b.back()]>=num[i]){
			b.pop_back();
		}
		if(b.empty()){
			g.push_front(0);
		}
		else{
			g.push_front(b.back());
		}
		b.push_back(i);
	}
}
void out(){//输出每个元素之后第1个大于和小于它的数的下标
	while(!f.empty()){//输出每个元素之后第1个大于它的数的下标
		cout<<f.front()<<" ";
		f.pop_front();
	}
	cout<<"\n";
	while(!g.empty()){//输出每个元素之后第1个小于它的数的下标
		cout<<g.front()<<" ";
		g.pop_front();
	}
}
int main(){
	ios::sync_with_stdio(false);//输入输出加速
	cin.tie(nullptr);
	cin>>n;
	num.push_back(0);//使下标从1开始
	for(int i=1;i<=n;i++){//输入
		cin>>x;
		num.push_back(x);
	}
	slove();//处理
	out();//输出
	return 0;
}
单调队列
C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
deque<ll>a,b;//a->最小值队列(递增) b->最大值队列(递减)
vector<ll>num;//num->输入序列
deque<ll>ma,mi;//ma->最大值答案 mi->最小值答案
int n,k,x;//n->数量 k->窗口长度 x->当前数字
void push(int i){//i->入队下标
	a.push_back(i);//入队
	b.push_back(i);
	if(i>=k-1){//更新答案
		mi.push_back(num[a.front()]);
		ma.push_back(num[b.front()]);
	}
}
void pop(int i){//i->遍历到的位置
	while(!a.empty()&&a.front()<=i-k){//过期的出队
		a.pop_front();
	}
	while(!b.empty()&&b.front()<=i-k){//过期的出队
		b.pop_front();
	}
	while(!a.empty()&&num[a.back()]>=x){//比x大的出队,维持单调性
		a.pop_back();
	}
	while(!b.empty()&&num[b.back()]<=x){//比x小的出队,维持单调性
		b.pop_back();
	}
}
void slove(){//处理最小值和最大值
	for(int i=0;i<n;i++){
		cin>>x;//输入新数字
		num.push_back(x);//存储元素
		if(i==0){//队列为空
			push(i);//直接压入
			continue;//无需继续处理
		}
		pop(i);//否则弹出不符合要求的元素
		push(i);//压入
	}
}
void out(){
	while(!mi.empty()){//输出最小值
		cout<<mi.front()<<" ";
		mi.pop_front();
	}
	cout<<"\n";
	while(!ma.empty()){//输出最大值
		cout<<ma.front()<<" ";
		ma.pop_front();
	}
}
int main(){
	ios::sync_with_stdio(false);//输入输出加速
	cin.tie(nullptr);
	cin>>n>>k;//输入
	slove();//处理
	out();//输出
	return 0;
}
归并排序
C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n;//n->数量
ll x;//x->当前数字
vector<ll>a(100005);//a->需要排序的序列
//在所有的函数中,l->左边界 r->右边界 m->中间下标
void fastmerge(int l){//快速合并(虽然并不快)
	int r=l+1;
	int sma,lar;//sma->最小值 lar->最大值
	sma=min(a[l],a[r]);//找较小值和较大值
	lar=max(a[l],a[r]);
	a[l]=sma;//合并
	a[r]=lar;
	return;
}
void merge(int l,int m,int r){//合并
	deque<ll>list;//list->排序后的序列
	int i=l,j=m+1;//i->前面序列的当前下标 j->后面序列的当前下标
	while(i<=m&&j<=r){//比较
		if(a[i]<a[j]){//如果a[i]小于a[j]
			list.push_back(a[i]);//压入a[i]
			++i;//下一项
		}
		else{//否则
			list.push_back(a[j]);//压入a[j]
			++j;//下一项
		}
	}
	while(i<=m){//清空前面序列
		list.push_back(a[i]);
		++i;
	}
	while(j<=r){//清空后面序列
		list.push_back(a[j]);
		++j;
	}
	int s=0;//s->合并时的遍历下标
	while(!list.empty()){//合并
		a[s+l]=list.front();
		list.pop_front();
		s++;
	}
	return;
}
void mergesort(int l,int r){//归并排序
	if(l>=r){//如果项数<=1
		return;//返回
	}
	else if(r-l==1){//如果只有两个数
		fastmerge(l);//快速合并
		return;//返回
	}
	int m=(l+r)/2;//计算中间下标
	mergesort(l,m);//归并前面序列
	mergesort(m+1,r);//归并后面序列
	merge(l,m,r);//合并
	return;
}
void in(){//输入
	cin>>n;//输入项数
	for(int i=0;i<n;i++){
		cin>>x;//输入新数字
		a[i]=x;
	}
}
void out(){//输出
	for(int i=0;i<n;i++){
		cout<<a[i]<<" ";
	}
}
int main(){
	in();//输入
	mergesort(0,n-1);//归并整体
	out();//输出
	return 0;
}
链式前向星(无向图,无边权,求深度)
C++
#include <bits/stdc++.h>
using namespace std;
int n,d,elen=0,ans=0;//n->节点数量 d->深度上限 elen->当前边总数 ans->结果
int last[100005],depth[100005];//last[i]->从节点i开始目前最后一条边编号 depth[i]->节点i深度
struct edge{//链式前向星标准配置(无边权)
	int st,en,pre;//st->起点 en->终点 pre->节点st在节点en之前的一条边编号
}e[200005];//两倍大小适配无向图
void ins(int x,int y){//加入新边
	e[++elen].st=x;//编号加1
	e[elen].en=y;
	e[elen].pre=last[x];//更新pre
	last[x]=elen;//更新last
}
void bfs(){//广度优先搜索模板
	int x,y;
	deque<int>dq;
	dq.push_back(1);
	depth[1]=0;
	while(!dq.empty()){
		x=dq.front();
		dq.pop_front();
		for(int k=last[x];k;k=e[k].pre){//由后往前遍历
			y=e[k].en;//取到相邻节点
			if(depth[y]==-1){//如果没有赋值深度
				depth[y]=depth[x]+1;//赋值深度
				dq.push_back(y);//加入搜索队列
			}
		}
	}
}
int main(){
	memset(depth,-1,sizeof(depth));//赋值初始值
	cin>>n>>d;//输入
	int x,y;
	for(int i=1;i<n;i++){
		cin>>x>>y;//输入边两端
		ins(x,y);//无向图需要存储两条边
		ins(y,x);
	}
	bfs();//搜索得到深度
	for(int i=2;i<=n;i++){//筛选深度得出答案
		if(depth[i]<=d){
			++ans;
		}
	}
	cout<<ans;
	return 0;
}
堆(大根堆)
C++
#include<bits/stdc++.h>
using namespace std;
vector<int>h;//堆(大根堆)
int n,lenh;//n->操作次数 lenh->当前堆大小
void up(int x){//向上调整
	while(x>1&&h[x]<h[x>>1]){//当x合法且不满足大根堆性质时
		swap(h[x],h[x>>1]);//向上调整
		x>>=1;
	}
}
void down(int x){//向下调整
	int maxs;//maxs->最大子节点
	while(x<<1<=lenh){//当x合法时
		maxs=x<<1;//获取左子节点
		if(maxs+1<=n&&h[maxs+1]>h[maxs]){//选取更大的子节点
			maxs++;
		}
		if(h[maxs]<=h[x]){//当符合大根堆性质时结束
			break;
		}
		swap(h[x],h[maxs]);//向下调整
		x=maxs;
	}
}
int main(){
	int opt,x;
	scanf("%d",&n);
	h.resize(n+5,0);//初始化堆大小限制
	lenh=0;//初始化堆大小
	for(int i=1;i<=n;i++){
		scanf("%d",&opt);
		if(opt==1){//插入值为x的元素
			scanf("%d",&x);
			h[++lenh]=x;//开辟新空间并调整大小
			up(lenh);//向上调整
		}
		else if(opt==2){//删除堆顶元素
			swap(h[1],h[lenh]);//交换堆顶与最后一项
			h[lenh--]=0;//删除原空间并调整大小
			down(1);//向下调整
		}
		else if(opt==3){//查询堆顶元素
			printf("%d",h[1]);
		}
	}
	return 0;
}
对顶堆
C++
#include<bits/stdc++.h>
using namespace std;
vector<int>up;//大根堆
vector<int>dn;//小根堆
int len_up=0,len_dn=0;
int n,k;
void up_up(int x){//大根堆向上调整
	while(x>1&&up[x]<up[x>>1]){
		swap(up[x],up[x>>1]);
		x>>=1;
	}	
}
void up_dn(int x){//小根堆向上调整
	while(x>1&&dn[x]>up[x>>1]){
		swap(up[x],up[x>>1]);
		x>>=1;
	}
}
void down_up(int x){//大根堆向下调整
	int maxs;
	while(x<<1<=len_up){
		maxs=x<<1;
		if(maxs+1<=n&&up[maxs+1]>up[maxs]){
			maxs++;
		}
		if(up[maxs]<=up[x]){
			break;
		}
		swap(up[x],up[maxs]);
		x=maxs;
	}
}
void down_dn(int x){//小根堆向下调整
	int mins;
	while(x<<1<=len_dn){
		mins=x<<1;
		if(mins+1<=n&&dn[mins+1]<dn[mins]){
			mins++;
		}
		if(dn[mins]>=dn[x]){
			break;
		}
		swap(dn[x],dn[mins]);
		x=mins;
	}
}
void insert_up(int x){//大根堆插入
	up[++len_up]=x;
	up_up(len_up);
}
void insert_dn(int x){//小根堆插入
	dn[++len_dn]=x;
	up_dn(len_up);
}
void delete_up(){//大根堆删除
	swap(up[1],up[len_up]);
	up[len_up--]=0;
	down_up(1);
}
void delete_dn(){//小根堆删除
	swap(dn[1],dn[len_dn]);
	up[len_dn--]=0;
	down_dn(1);
}
void balance(){//维护对顶堆
	int x;
	if(len_dn==k){
		return;
	}
	else if(len_dn<k){
		while(len_dn!=k){
			x=up[1];
			delete_up();
			insert_dn(x);
		}
	}
	else{
		while(len_dn!=k){
			x=dn[1];
			delete_dn();
			insert_up(x);
		}
	}
}
void insert(int x){//在序列中插入
	if(x>=dn[1]){
		insert_dn(x);
	}
	else{
		insert_up(x);
	}
}
int main(){
	int op,x;
	scanf("%d%d",&n,&k);
	up.resize(n+5);
	dn.resize(n+5);
	for(int i=1;i<=n;i++){
		scanf("%d",&op);
		if(op==1){//插入
			scanf("%d",&x);
			insert(x);
		}
		else if(op==2){//查询
			printf("%d\n",dn[1]);
		}
		else if(op==3){//删除
			delete_dn();
		}
		else{//调整k值
			scanf("%d",&x);
			k+=x;
		}
		balance();//维护对顶堆
	}
}
左偏树(封装为类)
C++
#include<bits/stdc++.h>
using namespace std;
struct tnode{
	int value;
	int dist;
	tnode* left;
	tnode* right;
	tnode(int v):value(v),dist(0),left(nullptr),right(nullptr){}
};
struct heap{
	private:
		tnode* root;
		bool cmp(tnode* x,tnode* y){//compare x with y
			return x->value<y->value;
		}
		tnode* mergenode(tnode* x,tnode* y){//merge two heaps who use x and y for root
			if(!x)return y;
			if(!y)return x;
			if(cmp(y,x))swap(x,y);
			x->right=mergenode(x->right,y);
			int ld=(x->left)?x->left->dist:-1;
			int rd=(x->right)?x->right->dist:-1;
			if(ld<rd)swap(x->left,x->right);
			x->dist=(x->right)?x->right->dist+1:0;
			return x;
		}
		void free(tnode* x){//free the heap who use x for root
			if(!x)return;
			free(x->left);
			free(x->right);
			delete x;
		}
		heap(const heap&)=delete;//you mustn't copy
		heap& operator=(const heap&)=delete;//you mustn't use '=' to give a heap to another heap
	public:
		heap():root(nullptr){}//build heap
		~heap(){//delete heap
			free(root);
		}
		bool empty()const{//x->empty()
			return root==nullptr;
		}
		int top()const{//x->top()
			return (root==nullptr)?throw runtime_error("heap is empty"):root->value;
		}
		void merge(heap* other){//x->merge(heap* y)
			if(!other||other->empty())return;
			root=mergenode(root,other->root);
			other->root=nullptr;
			delete other;
		}
		void push(int k){//x->push(int k)
			tnode* newnode=new tnode(k);
			root=mergenode(root,newnode);
		}
		void pop(){//x->pop()
			if(empty())throw runtime_error("pop from empty heap");
			tnode* old=root;
			root=mergenode(root->left,root->right);
			delete old;
		}
		static heap* mergeheap(heap* x,heap* y){//z=heap::mergeheap(heap* x,heap* y)
			if(!x)return y;
			if(!y)return x;
			tnode* xr=x->root;
			tnode* yr=y->root;
			heap* z=new heap();
			z->root=mergenode(xr,yr);
			if(x)x->root=nullptr;
			if(y)y->root=nullptr;
			delete x;
			delete y;
			return z;
		}
};
int main(){
	
}