跳到正文

个人记录

2025/7/16暑假集训笔记

song071812约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年7月16日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 7 分钟

版本

  1. v1当前

数据结构

1.单调栈

C++
int sta[N+1],top,a[N + 1],n;//sta存下标,a存数值,top表示栈顶元素下标
void process() {
	for(int i=1;i<=n;i++){
		while(top&&a[sta[top]]<=a[i])top--;
			a[++top]=i;//直接用a数组来存下标(比这个数大的第一个数的下标)(或者说是索引)
	}
}
C++
int sta[N + 1], top, a[N + 1], n, d[N + 1], e[N + 1];//sta:a数组里元素的下标;a:全部的元素
    void process() {
    for (int i = 1; i <= n; i++) {
        while (top && a[sta[top]] <= a[i]){
           e[sta[top]] = i, top--;
        }//这个while循环是来把元素入栈的
        a[++top] = i;
        if (top) d[i] = sta[top];
    }
 }

单调栈这块还是不太懂…

单调队列

C++
int q[N + 1], top, a[N + 1], n, l[M + 1], r[M + 1], m;
void process() {
    int head = 0, tail = 0, R = 0;
    for (int i = 1; i <= m; i++) {
        while (R < r[i]) {
            R++;
            while (tail- head && a[q[tail- 1]] <= a[R])
            tail--;
            q[tail++] = R;
        }
        while (tail- head && q[head] < l[i]) head++;
    // ... (此时队列中元素即为 S(l[i],r[i]) 中的元素
    // 从小到大排列后的结果)
    }
}

STL

上午还有一些STL的知识:

代码
//STL 全称标准模板库(Standard Template Library),属于
C++ 标准库。
//STL 内含一些封装好的常用算法和数据结构,意在避免编写
代码过程中重复而枯燥的造轮子工作。
......
等等等等......(可以查看课件file:///C:/Users/Administrator/Desktop/2025.7.16%20%20DAY3/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%BA%8C.pdf)
上面是网址

图论

图

1.什么是图?

  • 图 : 图是由顶点和顶点之间边的组成,是一种抽象的表达形式

  • 顶点:类似于每个路口和每条断头路的终点。

  • 边:类似于地图上的路

  • 单向边与双向边:类似于单行道和双行道

  • 度数:与之相邻的边数

  • 入度:过来的边数量

  • 出度:出去的边数量

2.拓扑排序(老师写的)

C++
#include<bits/stdc++.h>
using namespace std;
const int N = 1000005;
vector<int> g[N]; // graph
int n; 
int cnt[N]; // 有几条边指向自己 
bool in_tp[N];
vector<int> tp;

void tuopu(int i){
	tp.push_back(i);
	in_tp[i]=true; // 加入了
	// 删除从 i 所有的出边
	for(auto j: g[i]){
		cnt[j]--; // 因为我们把边删了
		if(cnt[j] == 0) // 因为我们删边而释放的点 
			tuopu(j);
	}
}

int main(){
	cin >> n;
	for(int i=1;i<=n;i++){
		while(true){
			int x;
			scanf("%d",&x);
			if(x == 0)
				break;
			g[i].push_back(x); // i --> x
			cnt[x]++; // 增加一条指向x的边 
		}
	}
	
	// 自己造点数据 
//	n = 10000000;
//	for(int i=2;i<=n;i++){
//		g[i].push_back(i/2),cnt[i/2]++; // i --> x
//		if(i/3 != 0)
//			g[i].push_back(i/3),cnt[i/3]++; // i --> x
//		if(i-8 >= 1)
//			g[i].push_back(i-8),cnt[i-8]++; // i --> x
//	}

	// 如何判断,当前点是否存在别的点指向自己?
	// cnt[y] == 0
	for(int i=1;i<=n;i++)
		if(!in_tp[i] && cnt[i] == 0)
			tuopu(i);
//	printf("OK");
	for(auto v: tp)
		printf("%d ", v);
	return 0;
} 

3.拓扑排序(朴素)(老师写的)

C++
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
vector<int> g[N]; // graph
int n; 
int cnt[N]; // 有几条边指向自己 
bool in_tp[N];
vector<int> tp;
int main(){
	cin >> n;
	for(int i=1;i<=n;i++){
		while(true){
			int x;
			scanf("%d",&x);
			if(x == 0)
				break;
			g[i].push_back(x); // i --> x
			cnt[x]++; // 增加一条指向x的边 
		}
	}
	// 如何判断,当前点是否存在别的点指向自己?
	// cnt[y] == 0
	while(tp.size() != n){
		for(int i=1;i<=n;i++){
			if(!in_tp[i] && cnt[i] == 0){
				// 一个可以被加入拓扑的点
				tp.push_back(i);
				in_tp[i]=true; // 加入了
				// 删除从 i 所有的出边
				for(auto j: g[i])
					cnt[j]--; // 因为我们把边删了
//				g[i].clear();  // 这个其实不用 
			}
		}
	} 
	for(auto v: tp)
		printf("%d ", v);
	return 0;
} 

树

  • 在计算机中,树是倒着长的

  • 树的存储和图一样,只是需要额外记录每个节点的”父亲”是谁

  • 名词解释:

    根节点 叶节点 子树

    有根树 无根树

    父亲 祖先 兄弟 后代

    无根树变有根树(随意确定一个点为根)


下课了

原文