跳到正文

个人记录

部分算法模板

sbl_xh约 26 分钟
原文
文章信息与版本

文章信息

发布
2026年2月8日
收录
2026年2月9日
更新
2026年11月22日
篇幅
约 26 分钟

版本

  1. v3当前
  2. v2
  3. v1
模板
佛洛依得
C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr int N = 2e2+10,M = 5*N,K = 5000;
constexpr int inf = 0x3f3f3f3f;
int n,m,a[N];
int dis[N][N];
int main(){
    cin >> n >> m;
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n;j++){
            dis[i][j]=inf;
            if(i==j) dis[i][j]=0;
        }
    }
    for(int i = 1,x,y,w;i<=m;i++){
        cin >> x >> y >> w;
        dis[x][y]=min(dis[x][y],w);
        dis[y][x]=min(dis[y][x],w);
    }
    for(int k = 1;k<=n;k++){
        for(int i = 1;i<=n;i++){
            for(int j = 1;j<=n;j++){
                dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
            }
        }
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n;j++){
            cout << dis[i][j] << ' ';//代表i到j的步数
        }
        cout << endl;
    }
    return 0;
}
迪街丝特拉
普通版
C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr int N = 1e5+5,M = 5*N,K = 5000;
ll n,m,s,a[N];
vector<pair<int,int> > g[N];
ll dis[N];
int ok[N];

void djstl(){
    for(int i = 1;i<=n;i++) dis[i]=9223372036854775807;
    dis[s]=0;
    for(int i = 1;i<=n;i++){
        ll minn=9223372036854775807,u=-1;
        for(int j = 1;j<=n;j++){
            if(ok[j]==0&&minn>dis[j]){
                minn=dis[j];
                u=j;
            }
        }
        ok[u]=1;
        for(int j = 0;j<g[u].size();j++){
            ll v=g[u][j].first,w=g[u][j].second;
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
            }
        }
    }
    return ;
}
int main(){
    cin >> n >> m >> s;
    for(int i = 1,u,v,w;i<=m;i++){
        cin >> u >> v >> w;
        g[u].push_back({v,w});
    }
    djstl();
    for(int i = 1;i<=n;i++){
        if(dis[i]!=9223372036854775807)cout << dis[i] << ' ';//1能到达i
        else cout << 2147483647 << ' ';//1无法到达i
    }
    return 0;
}
升级版
C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
priority_queue<pair<ll,ll>,vector<pair<ll,ll> >,greater<pair<ll,ll> > > q;
vector<pair<ll,ll> > g[100005];
ll n,m,s,ans,dis[100005];
bool ok[100005];
void djstl(){
    for(int i = 1;i<=n;i++){
        dis[i]=1e18;
    }
    dis[1]=0;
    q.push({0,s});
    while(q.size()){
        int u = q.top().second;
        q.pop();
        if(ok[u]) continue;
        ok[u]=1;
        for(int j = 0;j<g[u].size();j++){
            ll v=g[u][j].second,w=g[u][j].first;
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                q.push({dis[v],v});
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m >> s;
    for(int i = 1,u,v,w;i<=m;i++){
        cin >> u >> v >> w;
        g[u].push_back({w,v});
    }
    djstl();
    for(int i = 1;i<=n;i++){
        if(dis[i]!=1e18)cout << dis[i] << ' ';//1能到i
    }
    return 0;
}
十八马
仅输出是否可到达点i
C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define pii pair<int,int>
#define piii pair<int,pii>
using namespace std;
constexpr int N = 1e5+5,M = 5*N,K = 5000;
vector<pii> g[M];
ll n,m,S;
ll dis[M],cnt[M];
bool vis[M];
bool spfa(){
    for(ll i = 1;i<=n;i++) {
        dis[i]=1e18;
        vis[i]=0;
        cnt[i]=0;
    }
    dis[S]=0;
    queue<ll> q;
    q.push(S);
    vis[S]=1;
    cnt[S]=1;
    while(q.size()){
        ll u = q.front();
        q.pop();
        vis[u]=0;
        for(int i = 0;i<g[u].size();i++){
            ll v=g[u][i].first,w=g[u][i].second;
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                if(vis[v]==1) continue;
                q.push(v);
                cnt[v]++;
                vis[v]=1;
                if(cnt[v]>n) return 0;
            }
        }
    }
    return 1;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
    cin >> n >> m >> S;
    for(int i = 1,u,v,w;i<=m;i++){
        cin >> u >> v >> w;
        g[u].push_back({v,w});
    }
    spfa();
    
    for(int i = 1;i<=n;i++){
        if(dis[i]!=1e18) cout << dis[i];//能从1到i
        else cout << INT_MAX;//不能
        cout << ' ';
    }
	return 0;
}
是否能达到一个负环
C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define pii pair<int,int>
#define piii pair<int,pii>
using namespace std;
constexpr int N = 2005,M = 5*N,K = 5000;
ll n,m,k;
ll dis[M],cnt[M];
bool vis[M];
vector<pii> g[M];
bool spfa(){
    for(ll i = 1;i<=n;i++) {
        dis[i]=1e18;
        vis[i]=0;
        cnt[i]=0;
    }
    dis[k]=0;
    queue<ll> q;
    q.push(k);
    vis[k]=1;
    cnt[k]=1;
    while(q.size()){
        ll u = q.front();
        q.pop();
        vis[u]=0;
        for(auto f:g[u]){
            ll y=f.first,w=f.second;
            if(dis[y]>dis[u]+w){
                dis[y]=dis[u]+w;
                if(vis[y]==1) continue;
                q.push(y);
                cnt[y]++;
                vis[y]=1;
                if(cnt[y]>=n) return 1;
            }
        }
    }
    return 0;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
    int t;cin >> t;
    while(t--){
        cin >> n >> m;k=1;
        for(int i = 1;i<=n;i++) g[i].clear();
        for(int i = 1,u,v,w;i<=m;i++){
            cin >> u >> v >> w;
            g[u].push_back({v,w});
            if(w>=0) g[v].push_back({u,w});
        }
        if(spfa()) cout << "YES";
        else cout << "NO";
        cout << endl;
    }
    
	return 0;
}
单dio栈
右边第一个比AiA_i大的数
C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr int N = 1e5+5,M = 5*N,K = 5000;
stack<int> s;
int n,a[4000000],ans[4000000];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin >> n;
    for(int i = 1;i<=n;i++){
    	cin >> a[i];
	}
	for(int i = 1;i<=n;i++){
		while(s.empty()==0&&a[s.top()]<a[i]){
			ans[s.top()]=i;
			s.pop();
		}
		s.push(i);
	}
	for(int i = 1;i<=n;i++) {
		cout << ans[i] << ' ';
	}
    return 0;
}
左边第一个比AiA_i大的数
C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr ll N = 1e5+5,M = 5*N,K = 5000;
stack<ll> s;
ll n,a[4000000],ans[4000000];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin >> n;
    for(ll i = 1;i<=n;i++){
    	cin >> a[i];
        ans[i]=-1;
	}
	for(ll i = n;i>=1;i--){
		while(s.empty()==0&&a[s.top()]>a[i]){
			ans[s.top()]=i;
			s.pop();
		}
		s.push(i);
	}
    if(n>1000){
        ll out=ans[2];
        for(ll i = 3;i<=n;i++) out^=ans[i];
        cout << out;
        return 0;
    }
	for(ll i = 1;i<=n;i++) {
		cout << ans[i] << ' ';
	}
    return 0;
}
左右第一个比AiA_i大的数
C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr int N=1e5+5,M=5*N,K=5000;
stack<ll> sa,sb;
ll n,a[M],ans[M],asn[M];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for(ll i = 1;i<=n;i++){
		cin >> a[i];
		ans[i]=-1;
		asn[i]=-1;
	}
	for(ll i = n;i>=1;i--){
		while(sa.empty()==0&&a[sa.top()]>=a[i]){
    		ans[sa.top()]=i;
			sa.pop();
		}
		sa.push(i);
	}
	for(ll i = 1;i<=n;i++){
		while(sb.empty()==0&&a[sb.top()]>=a[i]){
    		asn[sb.top()]=i;
			sb.pop();
		}
		sb.push(i);
	}
	for(int i = 1;i<=n;i++){
		cout << ans[i] << ' ' << asn[i] << endl;
	}
	return 0;
}
单dio队列(deque)
滑动窗口的最小值和最大值
C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define pii pair<int,int>
#define piI pair<int,pii>
#define pII pair<pii,pii>
using namespace std;
constexpr int N=1e5+5,M=5*N,K=5000;
int n,m,a[1000006];
deque<ll> q,p;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i<=n;i++) cin >> a[i];
	for(int i = 1;i<=m;i++) {
		while(q.size()&&a[q.back()]>=a[i]) q.pop_back();
		q.push_back(i);
	}
    cout << a[q.front()] << ' ';
	for(int i = m+1;i<=n;i++){
		while(q.size()&&a[q.back()]>=a[i]) q.pop_back();
		q.push_back(i);
		while(q.size()&&q.front()+m-1<i) q.pop_front();
		cout << a[q.front()] << ' ';
	}
	cout << endl;
    for(int i = 1;i<=m;i++) {
		while(p.size()&&a[p.back()]<=a[i]) p.pop_back();
		p.push_back(i);
	}
    cout << a[p.front()] << ' ';
	for(int i = m+1;i<=n;i++){
		while(p.size()&&a[p.back()]<=a[i]) p.pop_back();
		p.push_back(i);
		while(p.size()&&p.front()+m-1<i) p.pop_front();
		cout << a[p.front()] << ' ';
	}
	return 0;
}
滑动窗口的总值
C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define pii pair<int,int>
#define piI pair<int,pii>
#define pII pair<pii,pii>
using namespace std;
constexpr int N=1e5+5,M=5*N,K=5000;
deque<ll> q;
ll n,ans;
ll a[2000006],s[2000006];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
    for(int i = 1;i<=n;i++){
        cin >> a[i];
        a[n+i]=a[i];
    }
    for(int i = 1;i<=n*2;i++) s[i]=s[i-1]+a[i];
    for(int i = 1;i<=n;i++){
        while(q.size()&&s[q.back()]>=s[i]) q.pop_back();
        q.push_back(i);
    }
    if(s[q.front()]-s[0]>=0) ans++;
    for(int i = n+1;i<n*2;i++){
        while(q.size()&&s[q.back()]>=s[i]) q.pop_back();
        q.push_back(i);
        while(q.size()&&i-q.front()>=n) q.pop_front();
        if(s[q.front()]-s[i-n]>=0) ans++;
    }
    cout << ans;
	return 0;
}
滑动窗口所有最大值数
C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr unsigned long long N = 1e6+5;
unsigned long long a[N],q[N];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	unsigned long long n,k;cin >> n >> k;
	for(unsigned long long i = 1;i<=n;i++)cin >> a[i];
	unsigned long long h = 1,t = 0;
	for(unsigned long long i = 1;i<=n;i++){
		while(h<=t&&a[q[t]]<=a[i])t--;
		q[++t] = i;
		if(i>=k){
			unsigned long long l = i-k+1;
			while(h<=t&&q[h]<l) h++;
			cout <<  t-h+1 << endl;
		}
	}
	return 0;
}
在一滑动窗口中修改最少的数使这个滑动窗口每个数字都有11个
C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
constexpr int N=1e5+5,M=5*N,K=5000;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	string s;cin >> s;
	int n = s.size();
	long long ans = 1e18;
	for(int i = 0;i<=n-10;i++){
		int a[10];
		for(int j = 0;j<10;j++)a[j] = s[i+j]-'0';
		if(i==0){
			int cnt = 1e9;
			for(int d = 1;d<=9;d++){
				int cru = abs(a[0]-d);
				int b[9];
				for(int j = 1;j<10;j++)b[j-1] = a[j];
				sort(b,b+9);
				int t[9],idx = 0;
				for(int k = 0;k<10;k++)if(k!=d)t[idx++] = k;
				int sum = 0;
				for(int k = 0;k<9;k++)sum+=abs(b[k]-t[k]);
				cru+=sum;
				if(cru<cnt)cnt = cru;
			}
			if(cnt<ans)ans = cnt;
		}else{
			int f[10] = {0};
			for(int j = 0;j<10;j++)f[a[j]]++;
			int cur = 0,cru = 0;
			for(int d = 0;d<10;d++){cur+=f[d]-1;cru+=abs(cur);}
			if(cru<ans)ans = cru;
		}
	}
	cout<<ans<<endl;
	return 0;
}
单dio队列deque pro
绝对差不超过mm的最长滑动窗口
C++
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define piI pair<int,pii>
#define endl '\n'
using namespace std;
constexpr int N=1e5+5,M=5*N,K=5000;
int n,k,a[N];
deque<ll> q,p;
ll ans=1;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >>k;
    for(int i = 1;i<=n;i++){
        cin >> a[i];
    }
    int l=1,r=1;
    while(l<=n&&r<=n){
        while(q.size()&&a[r]>=a[q.back()]) q.pop_back();
        q.push_back(r);
        while(p.size()&&a[r]<=a[p.back()]) p.pop_back();
        p.push_back(r);
        while(q.size()&&q.front()<l) q.pop_front();
        while(p.size()&&p.front()<l) p.pop_front();
        while(a[q.front()]-a[p.front()]>=k){
            l++;
            while(q.size()&&q.front()<l) q.pop_front();
            while(p.size()&&p.front()<l) p.pop_front();
        }
        if(ans<r-l+1) ans=r-l+1;
        r++;
    }
    cout << ans;
    return 0;
}
绝对差不超过mm的最短子序列长度
C++
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define piI pair<int,pii>
#define endl '\n'
using namespace std;
constexpr int N=1e5+5,M=5*N,K=5000;
int n,k,a[N];
deque<ll> q,p;
ll ans=1;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >>k;
    for(int i = 1;i<=n;i++){
        cin >> a[i];
    }
    int l=1,r=1;
    while(l<=n&&r<=n){
        while(q.size()&&a[r]>=a[q.back()]) q.pop_back();
        q.push_back(r);
        while(p.size()&&a[r]<=a[p.back()]) p.pop_back();
        p.push_back(r);
        while(q.size()&&q.front()<l) q.pop_front();
        while(p.size()&&p.front()<l) p.pop_front();
        while(a[q.front()]-a[p.front()]>=k){
            l++;
            while(q.size()&&q.front()<l) q.pop_front();
            while(p.size()&&p.front()<l) p.pop_front();
        }
        if(ans>r-l+1) ans=r-l+1;
        r++;
    }
    cout << ans;
    return 0;
}
原文