双向广搜 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 10 + 10; int n; string A, B; string a[N], b[N]; queue<string> qa, qb; map<string,int> da, db; int extend(queue<string> &q, map&
https://www.acwing.com/problem/content/description/3543/ 输入一系列整数,利用所给数据建立一个二叉搜索树,并输出其前序、中序和后序遍历序列。 输入格式 第一行一个整数 n,表示输入整数数量。 第二行包含 n 个整数。 输出格式 共三行,第一行输出前序遍历序列,第二行输出中序遍
带条件的 BFS 最短路 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 2e5 + 10; int n, m; int d[N]; int bfs(int x) { queue<int> q; q.push(x); memset(d, -1, sizeof d); d[x] = 0; while (q
BFS 记录距离 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 1000 + 10; #define x first #define y second int n, m; char g[N][N]; int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; int dy[] = {-1, 1, -2, 2, -2, 2,
BFS + 记录方案 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 1000 + 10; #define x first #define y second int n; int g[N][N]; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; pair<int,int> ne[N][N
需要处理输入的 Flood Fill 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 1000 + 10; #define x first #define y second int n, m; int g[N][N]; bool st[N][N]; int dx[] = {0, -1, 0, 1}; int dy[] = {-1, 0, 1,
Flood Fill 问题 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 1000 + 10; #define x first #define y second int n, m; char g[N][N]; bool st[N][N]; void bfs(int x, int y) { queue<pair<int,int>
周赛传送门 4500. 三个元素 本题可以学习的地方: 1.用map来存值和下标,map会自动排序 2.输出map中的元素可以先用vector存起来,然后输出vector中的元素 #include <iostream> #include <map> #include <vector> using namespace std; int n; map<int, int> pos; int main() { s
单调性 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 1e6 + 10; int n, m, op; int a[N]; void solve() { scanf("%d", &m); double sum = 0; int k = 0; while (m --) { scan
线段树 区间修改(加,乘),区间查询(求和) 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 5e5 + 10; int n, m, p; LL w[N]; struct Node { int l, r; LL sum, add, mul; } tr[N << 2]; void pushup(int u) { tr
PTA真的老喜欢出这种链表题欸 也好,涨知识了哦呦 e存储当前地址的值 ne存储当前节点地址的下一个地址 #include<bits/stdc++.h> using namespace std; const int N=200200; int e[N],ne[N]; int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int
算法思路 模拟除法竖式。 流程 从最高位开始处理,上一次的余数 \(\times 10\) 再加上当前位上的数字(被除数,我们令它为 \(A\)); 往答案数组中压入 \(\lfloor \frac{A}{B} \rfloor\)(这里 \(B\) 表示除数),然后将余数更新为 \(A~mod~B\); 重复以上操作。 代码 #include <iostream> #inclu
前缀和算法 前缀和是指数组中前 \(i\) 项的和,通常用 \(sum_i\) 表示。 我们设 \(a\) 是原数组:\(sum_i = a_1 + a_2 + a_3 + ... + a_i\)。 算法的好处 前缀和算法一般用来处理区间和问题。 如下图: 在 \(O(n)\) 的时间复杂度内算出前缀和,就可以每次用 \(O(1)\) 来查询一段区间和
实数二分模板题 实数二分与整数二分差不多,但要注意精度。 首先,我们知道,答案在 \(-10000 \sim 10000\) 之间。 如何判断在区间内能否二分呢?那就需要运用到二分的二段性了。 我们可以把这个区间分成两部分: 左区间 $ < \sqrt[3]{n}$; 右区间 $ \geq \sqrt[3]{n}$。 具体步骤: 找中间
观察题目 第一眼看题:这不就是大淼题 \(\text {A + B}\) 吗? 再一看,看到数据范围 \(1 \leq 整数长度 \leq 100000\),很显然,\(C++\) 中自带的数据类型肯定不行。 怎么办? 算法思路 观察到题目给出的整数长度数组存的下,因此我们可以先读入两个字符串,然后转成数组。 还记得小学一年级老师
算法思路 与高精度加法大致相同,同样运用了“列竖式”的思想。 当然,加法中的“进位操作”要改成减法中的“退位操作”。 具体过程如下: 从最低位开始,用被减数的这一位减去减数的这一位; 判断是否构够减,若不够减(即减数的这一位大于被减数的这一位),则向高一位借一(被减数高一位减一,当前
题目链接:https://www.acwing.com/problem/content/803/ 位运算 n 的二进制表示中第 k 位是几? 假设 n=15=(1111)2 ①先把第 k 位移到最后一位 n>>k ②看个位是几 x&1 两步合起来可以这样表示:n>>k&1 位运算“&”可以判断变量 x 的奇偶性: x&1 = 0(偶数) x&1 = 1(奇数)
题目链接:https://www.acwing.com/problem/content/800/ 要睡觉了今早要早起,今晚再写关于二位差分的内容吧 放AC代码 1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[1005][1005],b[1005][1005];//a前缀和数组,b差分数组 4 int n,m,q; 5 6 void insert(int
一.本周学习进度 1.本周复习了背包模型+单调队列优化DP 2.打了两场牛客 3.打了一把Atcoder+两把cf 二.本周cf和atcoder情况 1.Atcoder261 2.cf809div2 3.cfedu132 三.下周学习计划 1.开始狂刷DP章节 2.尽量多补一
BFS + 试除法判定质数 #include <bits/stdc++.h> using namespace std; typedef long long LL; int T; LL x; LL ans; bool check(LL t) { for (LL i = 2; i <= t / i; i++) { if (t % i == 0) return false; } return true; } LL bfs(LL x) { queue<LL>
看了很多的博客,终于对Nim游戏中的异或操作有些认识。。。 首先对于Nim游戏,需要明确两点,一点是如果剩下全是0,则是必败态。一点是如果有两个完全相同的状态,则它们合起来的状态是一个必胜态,即后手能完全模仿先手在对称的堆中进行操作。这就可以通过异或来操作 对于本题最简单的Nim游
线段树 区间修改,区间查询(和) 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 5e5 + 10; int n, m; LL w[N]; struct Node { int l, r; LL sum, add; } tr[N << 2]; void pushup(int u) { tr[u].sum = tr[u
线段树 单点修改,区间查询(最大值) 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 2e5 + 10; int m, p; struct Node { int l, r; int v; } tr[N * 4]; void pushup(int u) { tr[u].v = max(tr[u << 1].v,
链表模拟 + 队列模拟 可以用队列模拟,维护未弹出的数据和顺序。 也可以直接按题目要求维护循环队列,只需要单链表就够了。 队列: #include <bits/stdc++.h> using namespace std; const int N = 60; int T; int n; int ne[N]; int main() { cin >> T; while (T--) { cin >> n;
树状数组 + 二分 复杂度 \(n \cdot log^{2}(n)\) 点击查看代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int N = 2e5 + 10; int n; int h[N]; int tr[N]; int ans[N]; int lowbit(int x) { return x & -x; } void add(int x, i