随笔分类 -  基础算法

包括排序、二分、高精度、前缀和与差分、双指针算法、位运算、离散化、区间合并等内容.
摘要:#离散化 vector<int> alls; // 存储所有待离散化的值 sort(alls.begin(), alls.end()); // 将所有值排序 alls.erase(unique(alls.begin(), alls.end()), alls.end()); // 去掉重复元素 // 阅读全文
posted @ 2022-12-01 16:46 csai_H 阅读(26) 评论(0) 推荐(0) 编辑
摘要:#位运算的基本操作 ##1.求n的二进制表示中第k位是几 (1)先把第k位移到最后一位 n >> k; (2)看个位是几 n & 1; #include <bits/stdc++.h> using namespace std; int main(){ int n = 10; for(int k = 阅读全文
posted @ 2022-11-30 13:51 csai_H 阅读(25) 评论(0) 推荐(0) 编辑
摘要:#双指针算法 ##大致格式如下: for(int i = 0; i < n; i++){ while(j < i && check(i, j)) j++; //每道题目的具体逻辑 } ##核心思想: for(int i = 0; i < n; i++){ for(int j = 0; j < n; 阅读全文
posted @ 2022-11-29 21:47 csai_H 阅读(30) 评论(0) 推荐(0) 编辑
摘要:#归并排序模板(merge_sort) 归并排序要点: 基于分治的思想 确定分界点 mid = (l + r)/2; 递归排序左右两边 归并——合二为一(归并排序的核心) 过程分析: 当我们要排序一个数组时,归并排序首先把这个数组分成两半,然后想办法把左、右两边数组排序,再把他们归并起来。当我们对左 阅读全文
posted @ 2022-11-29 19:10 csai_H 阅读(88) 评论(0) 推荐(0) 编辑
摘要:#高精度除法模板(大 除 小)https://www.acwing.com/problem/content/796/ 注意要点: 商C[0]先存的是高位,main函数倒着输出,所以要reverse 商的结果可能存在前导0 #include <bits/stdc++.h> using namespac 阅读全文
posted @ 2022-11-29 16:30 csai_H 阅读(54) 评论(0) 推荐(0) 编辑
摘要:#高精度减法模板https://www.acwing.com/problem/content/794/ 减法注意要点: 对于 t = A[i] - B[i] - t; 可以拆为 t = A[i] - t如果B[i]合法,再t -= B[i] 这么两步来做 相减后t的处理 ,把 t >= 0 和 t 阅读全文
posted @ 2022-11-29 12:49 csai_H 阅读(26) 评论(0) 推荐(0) 编辑
摘要:#高精度乘法模板(大*小) #include <bits/stdc++.h> using namespace std; vector<int> mul(vector<int> &A, int b){ vector<int> C; int t = 0; //进位t,别忘记初始化 for(int i = 阅读全文
posted @ 2022-11-29 10:15 csai_H 阅读(33) 评论(0) 推荐(0) 编辑
摘要:#高精度乘法模板(大 * 大)https://www.acwing.com/problem/content/795/ #include <bits/stdc++.h> using namespace std; vector<int> mul(vector<int> &A, vector<int> & 阅读全文
posted @ 2022-11-29 09:38 csai_H 阅读(40) 评论(0) 推荐(0) 编辑
摘要:#高精度加法模板 高精度加法模板 #include <bits/stdc++.h> using namespace std; vector<int> add(vector<int> &A, vector<int> &B){ if(A.size() < B.size()) return add(B, 阅读全文
posted @ 2022-11-28 09:51 csai_H 阅读(63) 评论(0) 推荐(0) 编辑
摘要:#二维差分 始终记住对b[i][j]修改会影响a数组中从a[i][j]及往后的每一个数。 a[][]数组是b[][]数组的前缀和数组,那么b[][]是a[][]的差分数组 考虑如何构造差分数组b[i][j],使得a数组中a[i][j]是b数组左上角(1,1)到右下角(i,j)所包围矩形元素的和。 b 阅读全文
posted @ 2022-11-28 09:19 csai_H 阅读(117) 评论(0) 推荐(0) 编辑
摘要:#快排模板快排分析 ##分治的思想 确定分界点x: q[l], q[r], q[(l+r)/2]随机; 调整区间,使得x左边区间的数 <= x,右边区间的数 >= x;(快排的核心所在) 递归处理左右两段 #include <bits/stdc++.h> using namespace std; c 阅读全文
posted @ 2022-11-28 08:59 csai_H 阅读(26) 评论(0) 推荐(0) 编辑
摘要:##算法:子矩阵的和 核心公式: S[i, j] = 第i行j列格子左上部分所有元素的和 以(x1, y1)为左上角,(x2, y2)为右下角的子矩阵的和为: S[x2, y2] - S[x1 - 1, y2] - S[x2, y1 - 1] + S[x1 - 1, y1 - 1] 由图得,蓝色面积 阅读全文
posted @ 2022-11-25 11:15 csai_H 阅读(481) 评论(0) 推荐(0) 编辑
摘要:##算法:前缀和 #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int a[N], s[N]; //s[N]前缀和数组 s[i] = a[1] + a[2] + a[3] + ...+ a[i] int m 阅读全文
posted @ 2022-11-25 10:58 csai_H 阅读(27) 评论(0) 推荐(0) 编辑
摘要:#差分http://www.acwing.com/problem/content/799/ 类似数学中的求导和积分,差分可以看成前缀和的逆运算 首先给定一个原数组a:a[1],a[2],a[3]...a[n]; 然后我们构造一个数组b:b[1],b[2],b[3],...b[i]; 使得a[i]=b 阅读全文
posted @ 2022-11-24 22:24 csai_H 阅读(111) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示