
acwing
csdn_ggboy
这个作者很勤快,但是什么都没有留下
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
算法提高课第三章负环
spfa算法可以求出图中是否存在负环 一般求解负环有两种方法 统计每个点入队的次数,如果有的点入队次数大于等n,那么存在负环 统计到每个点的最短路径长度,如果长度大于等于n,那么存在负环 spfa算法的几个注意点 求解是否存在负环与dist初始值无关 负环求最短路 正环求最长路 将队列换成栈有机会很快找到环路 根据经验,在更新了很多次路径以后没有跳出循环,可以认为存在环。 01规划问题,一般是求分数的最大值或者最小值,可以利用二分的思想转化为求解图上的环路问题 904. 虫洞 - AcWin.原创 2021-12-22 23:19:50 · 127 阅读 · 0 评论 -
算法提高课第二章IDA*
在迭代加深的基础上,使用预估函数检查是否满足层数要求 180. 排书 - AcWing题库 #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 15; int n; int q[N]; int w[5][N]; int f() { int tot = 0; for(int i = 0; i + 1 < n.原创 2021-12-18 19:29:49 · 134 阅读 · 0 评论 -
算法提高课补充内容
1273. 天才的记忆 - AcWing题库 st表求区间最值 #include <iostream> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int N = 200010, M = 18; int n, m; int w[N]; int f[N][M]; void init() { for(int j = 0; j <原创 2021-12-16 19:52:27 · 120 阅读 · 0 评论 -
Acwing基础课刷题
数的三次方根转载 2021-11-25 20:07:55 · 3427 阅读 · 0 评论 -
算法提高课第一章状态机模型
划定每一个状态 分析状态转移方程 1049. 大盗阿福 - AcWing题库 #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 110000; int a[N]; int dp[N][2]; int n; int main() { int t; cin >> t; while(t --) .原创 2021-11-25 17:03:20 · 129 阅读 · 0 评论 -
算法提高课第三章拓扑排序
164. 可达性统计 - AcWing题库 求解拓扑序 按照拓扑序求解状态(状态定义为bitset<N>) AcWing 164. 可达性统计(2种简单写法) - AcWing #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <bitset> #include <queue> using na原创 2021-11-24 21:26:46 · 89 阅读 · 0 评论 -
算法提高课第三章最小生成树及其扩展应用
任意一颗最小生成树可以包含权值最小的一条边 生成树可以包含连接各个联通块的权值最小的边 无向图才可以使用最小生成树算法 1146. 新的开始 - AcWing题库 建立一个超级发电站(虚拟源点),将点权转化为边权。 #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 330; int n; int dist[N], w[N][N..原创 2021-11-24 18:24:15 · 521 阅读 · 0 评论 -
算法提高课第五章树状数组
树状数组和差分树状数组 快速求前缀和 修改某个数 241. 楼兰图腾 - AcWing题库 #include <iostream> #include <cstring> #include <algorithm> #define int long long using namespace std; const int N = 200010; int n; int a[N]; int tr[N]; int g[N], l[N]; int lowbit(int x.原创 2021-11-20 21:33:30 · 490 阅读 · 0 评论 -
算法提高课第四章并查集
并查集有两个功能:合并集合、查询祖宗节点 优化方式:路径压缩,按秩合并 记录每个集合的大小,绑定到根节点。记录每个点到根节点的距离,绑定到子节点 并查集的高级应用:扩展并查集、带权并查集 1250. 格子游戏 - AcWing题库 #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 40010; int n, m; int f[N.原创 2021-11-20 17:30:22 · 2544 阅读 · 0 评论 -
算法提高课第一章上升子序列模型
基本模型:使用动态规划解决最长上升子序列问题, 核心是用以iii为结尾的序列来表示所有满足要求的集合 1017. 怪盗基德的滑翔翼 - AcWing题库 #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 110; int dp1[N], dp2[N];//以i结尾的上升子序列的长度 int a[N]; int n; int ma.原创 2021-11-19 21:41:56 · 85 阅读 · 0 评论 -
算法提高课第一章数位dp
减法求出区间答案,用树的思想来思考原创 2021-11-19 11:38:17 · 100 阅读 · 0 评论 -
算法提高课第一章树形dp
1072. 树的最长路径 - AcWing题库 给定一棵树,树中包含 nnn 个结点(编号111~nnn)和 n−1n−1n-1 条无向边,每条边都有一个权值。 现在请你找到树中的一条最长路径。 换句话说,要找到一条路径,使得使得路径两端的点的距离最远。 注意:路径中可以只包含一个点。 输入格式 第一行包含整数 nnn。 接下来 n−1n−1n-1 行,每行包含三个整数 ai,bi,ciai,bi,cia_i,b_i,c_i,表示点 aiaia_i 和 bibib_i 之间存在一条权值为 cicic_i 的原创 2021-11-17 17:50:55 · 348 阅读 · 0 评论 -
算法提高课第一章单调队列优化dp
一些dp需要求类似于滑动窗口的问题,这时需要使用单调队列优化。原创 2021-11-18 11:42:09 · 220 阅读 · 0 评论 -
算法提高课第一章区间dp
环形区间dp 区间dp记录方案数 区间dp和高精度 二维区间dp原创 2021-11-16 10:32:36 · 225 阅读 · 0 评论 -
算法提高课第一章状态压缩dp
棋盘式(基于连通性)的dp 小国王 #include <iostream> #include <cstring> #include <algorithm> #include <vector> #define int long long using namespace std; const int N = 12, M = 1 << 10, K = 110; int n, m; vector<int> state;//合法状态 int c原创 2021-11-15 16:30:13 · 469 阅读 · 0 评论 -
算法提高课第一章背包模型
多种多样的背包问题 采药 01背包 装箱问题 01背包 AcWing 1022. 宠物小精灵之收服 多重背包,一题三解。 有一些精灵球,皮卡丘有一定的体力,在保证收服尽可能多的小精灵的情况下,使得皮卡丘的体力剩余最多 皮卡丘的体力必须大于1,不然就裂开 首先多收服小精灵,其次是少消耗体力 多价值评价 很容易想到,小精灵的价值大,体力的价值小,而且消耗体力是减少价值。这样题目就变成了裸的多重背包 根据题目范围:可以设定小精灵的价值为10000,消耗体力的价值为-1,这要求得的最大价值就是一个混合体,解原创 2021-11-11 21:42:28 · 149 阅读 · 0 评论 -
算法提高课第一章数字三角形模型
有一种图,从起点到终点只能向右或者向左,这种图后边的状态不会影响前边的状态,可以用dp求解。 AcWing 1015. 摘花生 AcWing 1018. 最低通行费 #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 110; int a[N][N]; int n; int dp[N][N]; int main() { m.原创 2021-11-11 21:26:55 · 142 阅读 · 0 评论 -
算法提高课第三章Floyd算法及拓展应用
floyd算法可以解决的问题: 最短路 传递闭包 找最小环 恰好经过k条边的最短路(倍增) //初始化 for(int i = 1; i <= n; i ++ ) for(int j = 1; j <= n; j ++ ) if(i != j) dist[i][j] = inf; 集合:所有从i出发,最终走到j,且中间只经过节点编号不超过k的所有路径 属性:路径长度的最小值 最短路 AcWing 1125. 牛的旅行 #include <iostream> #include原创 2021-11-10 09:40:57 · 258 阅读 · 0 评论 -
算法提高课第三章单源最短路
将一个背景复杂的问题抽象为单源最短路问题。 算法复杂度方面,迪杰斯特拉算法朴素版O(n2)O(n^2)O(n2),堆优化迪杰斯特拉O(mlogn)O(mlogn)O(mlogn),spfa O(nm)/O(m)O(nm) / O(m)O(nm)/O(m),floyd O(n3)O(n^3)O(n3)原创 2021-11-04 18:46:19 · 101 阅读 · 0 评论 -
算法提高课第二章双向dfs
双向dfs不是同时从两个方向dfs,而是二分搜索的一种方式。对于一个很大的集合, 可以先搜索他的后半段,并且存储起来,然后搜索后半段,过程中利用存储的数据,使用二分等方法降低复杂度。 AcWing 171. 送礼物 #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int N = 46; int.原创 2021-11-03 19:03:03 · 291 阅读 · 0 评论 -
算法提高课第二章迭代加深
在进行dfs时,有时候答案所在的层数不大,但是因为深搜而搜索多余的层数。迭代加深的思想是通过逐步增大搜索层数的方法,避免过深的搜索。因为dfs的搜索过程是指数增长的的,所以搜索多次在最坏情况下不会增加时间复杂度。 170. 加成序列 #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 110; int depth; int n; .原创 2021-11-03 18:57:26 · 165 阅读 · 0 评论 -
算法基础课第二章并查集
简单并查集 (1)朴素并查集: int p[N]; //存储每个点的祖宗节点 // 返回x的祖宗节点 int find(int x) { if (p[x] != x) p[x] = find(p[x]); return p[x]; } // 初始化,假定节点编号是1~n for (int i = 1; i <= n; i ++ ) p[i] = i; // 合并a和b所在的两个集合: p[f原创 2021-10-31 17:47:58 · 113 阅读 · 0 评论 -
算法基础课第二章KMP
KMP算法用于进行字符串匹配。 暴力的方法是每次失配就从模式串的头部进行匹配,但是因为有前后缀匹配的情况,不必每次回到头部,通过一个next数组, 可以在失配时让主串i去匹配next[j],这样能很快的进行匹配。 如何使用next数组 void KMP(string s, string p) { int j, i = 0; while(i < p.size()) { if(j == -1 || s[j] == p[i])//j == -1 说明第一个字符也不匹配,这样j归0, i增加原创 2021-10-31 17:01:33 · 223 阅读 · 0 评论 -
acwing算法高课背包dp
文章目录背包问题01 背包完全背包多重背包多重背包二进制优化分组背包混合背包背包方案数依赖背包背包方案 背包问题 01 背包 for(int i = 1; i <= n; i ++) { cin >> v >> w; for(int j = m; j >= v; j --)//体积从大到小,这样物品i仅使用了一次 dp[j] = max(dp[j], dp[j - v] + w; } cout << dp[m]; 完全背包 for(int i = 1原创 2021-10-30 21:21:04 · 286 阅读 · 0 评论 -
算法基础课第二章哈希表
字符串哈希原创 2021-10-30 21:20:10 · 117 阅读 · 0 评论 -
acwing基础课第一章离散化
树状数组 241. 楼兰图腾 #include <iostream> #include <cstring> #include <algorithm> using namespace std; #define int long long const int N = 200010; typedef long long LL; int n; int y[N], tr[N]; int dp1[N], dp2[N]; int lowbit(int x) { return x原创 2021-10-30 21:19:14 · 244 阅读 · 0 评论 -
acwing基础课第一章排序
文章目录快速排序归并排序 快速排序 寻找基准数 交换左右的数字使左边的数字小,右边的数字大 以基准数字分割左右区间继续寻找 void q_sort(int x[],int l,int r) { if(l>=r)return; int index=x[(r+l)>>1]; int i=l-1,j=r+1; while(i<j) { do i++;while(x[i]<index);//避免进入死循环 do j--;while(x[j]>index);原创 2021-10-30 21:16:50 · 186 阅读 · 0 评论 -
acwing算法基础课
提高课笔记原创 2021-10-30 21:12:35 · 7905 阅读 · 0 评论 -
算法提高课第二章DFS之连通性模型
DFS在求解连通性时有很好的应用,而且代码会比BFS简洁 DFS求解有两种,图内连通和图外连通。图内联通是图内的两个点连通,每个点只需遍历一次,而图外连通是指求解图状态改变的连通,如求解最短路径,每个点可能遍历多次,这时需要恢复现场,进行回溯 AcWing 1112. 迷宫 #include <iostream> #include <cstring> #include <algorithm> using namespace std; int sa, sb, ea,.原创 2021-10-29 19:39:53 · 213 阅读 · 0 评论 -
算法提高课第二章A*
在进行最短路搜索时,会有大量无效的搜索。A*使用一个估价函数,估计当前点到终点的最短距离,在估计值小于等于真实值却大于等于0的情况下,通过BFS可以更快的找到最短路径。 求第K短路 #include <iostream> #include <cstring> #include <algorithm> #include <queue> #define x first #define y second using namespace std; typedef .原创 2021-10-29 19:32:02 · 122 阅读 · 0 评论 -
算法提高第二章课双向广搜
在BFS搜索的过程中,状态的扩展是指数级增长的,为了减少状态数,我们可以从起点和终点双向BFS搜索,且在一个方向的状态数少的情况下多去搜这个方向。在两个方向同时搜到某个状态数结束 注意,不是每个方向一个点一个点的去搜,而是每次搜索一层,这样才保证不会有遗漏的最短路径 AcWing 190. 字串变换 #include <iostream> #include <cstring> #include <algorithm> #include <queue> #i.原创 2021-10-29 19:26:51 · 190 阅读 · 0 评论 -
算法提高课第二章双端队列广搜
什么时候会用到双端队列呢,BFS可以看做是一种特殊边权全为1的迪杰斯塔拉算法,BFS的队列满足两端性和单调性,这样就保证了它求解最短路的准确性。当边权为0和1时,可以使用双端队列,0放队头,1放队尾,这样也保证了他的单调性,因此可以使用BFS求解这种图的最短路 非常像迪杰斯特拉算法,每个点可能入队不止一次,但是第一次出队即可确定到该点最短距离。下一次出队便不需要从这个点再去扩展了,因为第一次出队时扩展的已经是最短路径了 AcWing 175. 电路维修 #include <iostream>.原创 2021-10-29 19:08:44 · 317 阅读 · 0 评论 -
算法提高课第二章最小步数模型
1107. 魔板 #include <iostream> #include <cstring> #include <algorithm> #include <queue> #include <map> #define x first #define y second #define ST pair<string, int> using namespace std; string start_st = "12345678"; string原创 2021-10-29 19:01:43 · 119 阅读 · 0 评论 -
算法提高课第二章多源BFS
有这样一个情景,对于很多源点,求得离他最近的目标的最短路径,称为多源最短路。这时可以创建一个虚拟源点,以边权为0连接所有的源点,这样就把多源最短路转化为了单源最短路 AcWing 173. 矩阵距离 #include <iostream> #include <cstring> #include <algorithm> #include <queue> using namespace std; int n, m; queue<pair<int, .原创 2021-10-29 18:57:28 · 173 阅读 · 0 评论 -
算法提高课第二章最短路模型
BFS第一次扩展到终点时即可求得最短路 AcWing 1076. 迷宫问题 #include <iostream> #include <cstring> #include <algorithm> #include <queue> using namespace std; int bx[4] = {0,0,1,-1}; int by[4] = {1,-1,0,0}; int n; struct node { int px; int py; }.原创 2021-10-29 18:52:59 · 151 阅读 · 0 评论 -
算法提高课第二章Flood Fill
对于染色问题,可以采用BFS求解 BFS的优点是:每个点只会遍历一次,第一次遇到终点时即可求得最短路 1097. 池塘计数 #include <iostream> #include <cstring> #include <queue> #include <algorithm> using namespace std; char a[1010][1010]; bool st[1010][1010]; int bx[8] = {1,-1,0,0,1,1,-1,.原创 2021-10-29 18:50:00 · 133 阅读 · 0 评论