随笔分类 - 基础算法
包括排序、二分、高精度、前缀和与差分、双指针算法、位运算、离散化、区间合并等内容
摘要:AcWing803. 区间合并 题解 #include <iostream> #include <algorithm> #include <vector> using namespace std; typedef pair<int,int> PII; vector<PII> segs; void m
阅读全文
摘要:AcWing802.区间和 题解 本题要是一个很长的数轴,已经超过了数组的长度1e6,故我们需要使用离散化压缩空间 #include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace
阅读全文
摘要:离散化应用 离散化模板 vector<int> alls; sort(alls.begin(), alls.end()); alls.erase(unique(alls.begin(),alls.end()), alls.end()); //获取离散化后的映射 int find(int x) { i
阅读全文
摘要:AcWing2816.判断子序列 题解 即在另一个序列中,找到所有本序列的值,且具有相同的排列 #include <iostream> #include <cstdio> using namespace std; const int N = 1e5+10; int a[N], b[N]; int m
阅读全文
摘要:AcWing 800. 数组元素的目标和 题解 #include <iostream> #include <cstdio> using namespace std; const int N = 1e5+10; int a[N], b[N]; int main() { int n, m, x, i,
阅读全文
摘要:Acwing799.最长连续不重复子序列 题解 #include <iostream> #include <cstdio> using namespace std; const int N = 1e5+10; int a[N], s[N]; int main() { int n; scanf("%d
阅读全文
摘要:双指针模板 入门案例——统计单词 #include <iostream> #include <cstring> using namespace std; int main() { string s; getline(cin,s); for(int i = 0; i < s.size(); ++i)
阅读全文
摘要:AcWing 801. 二进制中1的个数 题解 用lowbit可以找到,最后一个1的位置。 通过lowbit计算每个数二进制1的个数,复杂度O(logn),n个数,一共O(nlogn) #include <iostream> #include <cstdio> const int N = 1e5+1
阅读全文
摘要:Acwing798.差分矩阵 题解 #include <iostream> #include <cstdio> using namespace std; const int N = 1e3+10; int s[N][N]; void insert(int x, int y, int xx, int
阅读全文
摘要:Acwing797.差分 题解 #include <iostream> #include <cstdio> using namespace std; const int N = 1e5+10; int b[N]; void insert(int l, int r, int c) { b[l] +=
阅读全文
摘要:Acwing796.子矩阵的和(二维前缀和) 题解 #include <iostream> #include <cstdio> using namespace std; const int N = 1e3+10; int s[N][N]; int main() { int n, m, q; scan
阅读全文
摘要:Acwing795.前缀和 题解 #include <iostream> #include <cstdio> using namespace std; const int N = 1e5+10; int a[N]; int main() { int n, m; scanf("%d%d",&n,&m)
阅读全文
摘要:Acwing794.高精度除法 题解 也是一个模拟笔算的过程 #include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> div(vector<int> &a, int b)
阅读全文
摘要:Acwing793 题解 一种简化笔算的过程,让大数的逐位去乘小数即可 #include <iostream> #include <vector> using namespace std; vector<int> mult(vector<int> &a, int b) { vector<int> c
阅读全文
摘要:Acwing790.数的三次方根 题解 做法一:通过二分运算到符合精度要求 #include <iostream> #include <cstdio> using namespace std; #define f 1e-8 int main() { double x; cin >> x; doubl
阅读全文
摘要:Acwing789.数的范围 题解 最左边界,每次 mid >= x 则 r = mid, 再找左边是否有符合要求的边界。 最右边界,每次 mid ⇐ x 则 l = mid, 再找右边是否有符合要求的边界。(注意当l = r-1时 ,(l+r)>> 1 == l 故我们需要 mid = (l +
阅读全文
摘要:Acwing792.高精度减法 题解 #include <iostream> #include <vector> using namespace std; bool cmp(vector<int> &a, vector<int> &b) { if(a.size() != b.size()) retu
阅读全文
摘要:Acwing791.高精度加法 题解 高精度加法 #include <iostream> #include <cstdio> #include <vector> using namespace std; vector<int> add(vector<int> &a, vector<int> &b)
阅读全文
摘要:Acwing788.逆序对的数量 题解 暴力做法:对每个字符串前或后二选一进行统计,复杂度O(n2)超时 归并排序:由于归并二分的特性,能够保证每个数在没排序前都能统计到(会分到长度为2的序列),同时排序完左右后并不影响右边对于左边的逆序对数,或左边对于右边的逆序对数,时间复杂度O(nlogn) #
阅读全文
摘要:Acwing787.归并排序 题解 归并排序:采用分治思想,将左右完成排序,再进行一个归并,用左右得出整体的结果 #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int N = 1
阅读全文