随笔分类 - 算法提高课
摘要:一、介绍 功能 快速求前缀和 O(logn) 修改某一个数 O(logn) 原理 c[x]:以x结尾的长度lowbit(x)的所有数的和 父节点找所有子节点(求和操作):c[x] = a[x] + c[x-1] + ... + c[lowbit(x-1)],x为偶数时,每一次去掉最后一个1;x为奇数
阅读全文
摘要:一、并查集 1250. 格子游戏 思路 O(mlog(n)) 将图中的每个点看作并查集的结点,每个被画的边看作合并相邻的点的操作 将图中所有点按行或列优先,从1~n*m进行编号 每次进行合并时,当两个结点属于一个集合时,说明找到了一个封闭的圈 题解 #include <iostream> #incl
阅读全文
摘要:一、迭代加深 适用场景:某些分支的层数特别深,但答案在比较浅的层数里 170. 加成序列 剪枝一:优先枚举较大的数 减少搜索层数 剪枝二:排除等效冗余 前面任意两个数的和可能相等,对于每个结点,开一个bool数组记录是否枚举过 #include <iostream> #include <cstrin
阅读全文
摘要:一、DFS之连通性模型 1112. 迷宫 #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 110; int T,n; char g[N][N]; int sx
阅读全文
摘要:一、双向广搜 190. 字串变换 #include <iostream> #include <cstring> #include <algorithm> #include <unordered_map> #include <queue> using namespace std; const int
阅读全文
摘要:求树的直径 1072. 树的最长路径 dfs #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 10010,M = 2*N; int h[N],e[M],w[M
阅读全文
摘要:区间DP 282. 石子合并 #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int N = 305,INF = 0x3f3f3f3f; int f[N][N],a[N],s[N]
阅读全文
摘要:一、基于棋盘式(连通性)的状态压缩问题 1064. 小国王 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<vector> using namespace std; typedef
阅读全文
摘要:状态机的特点:描述的是过程,而不是结果。将一个点扩展成一个过程 1049. 大盗阿福 DP考虑方式: 用状态机思想考虑: #include <iostream> #include <cstring> #include <algorithm> using namespace std; const in
阅读全文
摘要:一、01背包模型 423. 采药 二维朴素做法 #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int M = 1010,N = 105; int t[N],w[M],n,m
阅读全文
摘要:提高课第一章 动态规划:数字三角形、最长上升子序列、状态机模型、状态压缩DP、区间DP、树形DP 1.1数字三角形模型 1018. 最低通行费 #include <iostream> #include <cstring> #include <algorithm> using namespace st
阅读全文