NOTE: 主要是学习期间的代码整理,y总算法基础课
1. 快速排序
// 快速排序
void quick_sort(int q[], int l, int r)
{
if(l >= r) return;
int i = l - 1, j = r + 1, x = q[(l + r) >> 1];
while(i < j){
do i++; while(q[i] < x);
do j--; while(q[j] > x);
if(i < j) swap(q[i], q[j]);
}
quick_sort(q, l, j);
quick_sort(q, j + 1, r);
}
2. 归并排序
ll a[N], tmp[N];
ll res; // 逆序对个数
void merge_sort(ll a[], ll l, ll r)
{
if(l >= r) return ;
ll mid = (l + r) >> 1;
merge_sort(a, l, mid);
merge_sort(a, mid + 1, r);
ll k = 0, i = l, j = mid + 1;
while(i <= mid && j <= r){
if(a[i] <= a[j]) tmp[k++] = a[i++];
else tmp[k++] = a[j++], res += mid - i + 1;
}
while(i <= mid) tmp[k++] = a[i++];
while(j <= r) tmp[k++] = a[j++];
for(i = l, j = 0; i <= r; i++, j++) a[i] = tmp[j];
}
3. 二分
😉整数二分
bool check(int x) {/* ... */} // 检查x是否满足某种性质
// 区间[l, r]被划分成[l, mid]和[mid + 1, r]时使用:
int bsearch_1(int l, int r)
{
while (l < r)
{
int mid = l + r >> 1;
if (check(mid)) r = mid; // check()判断mid是否满足性质
else l = mid + 1;
}
return l;
}
// 区间[l, r]被划分成[l, mid - 1]和[mid, r]时使用:
int bsearch_2(int l, int r)
{
while (l < r)
{
int mid = l + r + 1 >> 1; //🆘🆘🆘
if (check(mid)) l = mid;
else r = mid - 1;
}
return l;
}
😉浮点数二分
bool check(double x){ }
double bsearch_3(double l, double r)
{
const double eps = 1e-6;
while(r - l > eps){
double mid = (l + r) / 2;
if(check(mid)) r = mid;
else l = mid;
}
return l;
}
4. 高精度
1️⃣高精度加法
#include<bits/stdc++.h>
using namespace std;
// C = A + B
vector<int> add(vector<int> &A, vector<int> &B)
{
vector<int> C;
int t = 0;
for(int i = 0; i < A.size() || i < B.size(); i++){
if(i < A.size()) t += A[i];
if(i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if(t) C.push_back(t);
return C;
}
int main()
{
string a, b;
vector<int>A, B;
cin >> a >> b;
for(int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
for(int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
auto C = add(A, B);
for(int i = C.size() - 1; i >= 0; i--) cout << C[i];
return 0;
}
2️⃣高精度减法
#include<bits/stdc++.h>
using namespace std;
bool cmp(vector<int> &A, vector<int> &B)
{
if(A.size() != B.size()) return A.size() > B.size();
else {
for(int i = A.size() - 1; i >= 0; i--){
if(A[i] != B[i]) return A[i] > B[i];
}
}
return true;
}
// C = A - B
vector<int> substract(vector<int> &A, vector<int> &B)
{
vector<int> C;
for(int i = 0, t = 0; i < A.size(); i++){
t = A[i] - t;
if(i < B.size()) t -= B[i];
C.push_back((t + 10) % 10);
if(t < 0) t = 1;
else t = 0;
}
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a, b;
cin >> a >> b;
vector<int> A, B, C;
for(int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
for(int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
if(cmp(A, B)){
C = substract(A, B);
}else{
C = substract(B, A);
cout << "-";
}
for(int i = C.size() - 1; i >= 0; i--) cout << C[i];
return 0;
}
3️⃣高精度乘法
#include<bits/stdc++.h>
using namespace std;
// C = A * b
vector<int> mul(vector<int> &A, int b)
{
vector<int> C;
int t = 0;
for(int i = 0; i < A.size() || t; i++){
if(i < A.size()) t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a;
int b;
cin >> a >> b;
vector<int> A;
for(int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
auto C = mul(A, b);
for(int i = C.size() - 1; i >= 0; i--) cout << C[i];
return 0;
}
4️⃣高精度除法
#include<bits/stdc++.h>
using namespace std;
// A / b = C ...... r
vector<int> div(vector<int> &A, int b, int &r)
{
vector<int> C;
for(int i = A.size() - 1; i >= 0; i--){
r = r * 10 + A[i];
C.push_back(r / b);
r %= b;
}
reverse(C.begin(), C.end());
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a;
int b, r = 0;
cin >> a >> b;
vector<int> A;
for(int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
auto C = div(A, b, r);
for(int i = C.size() - 1; i >= 0; i--) cout << C[i];
cout << endl << r;
return 0;
}
5. 前缀和
- 一维前缀和
s[i] = a[1] + a[2] + ... + a[n]
a[l] + .... + a[r] = s[r] - s[l - 1]
- 二维前缀和
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]
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
}
}
void solve()
{
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
ll res = s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];
cout << res << endl;
}
6. 差分
- 一维差分
给区间[l, r]中的每个数加上c:
B[l] += c, B[r + 1] -= c
- 二维差分
给以(x1, y1)为左上角,(x2, y2)为右下角的子矩阵中的所有元素加上c:
S[x1, y1] += c, S[x2 + 1, y1] -= c, S[x1, y2 + 1] -= c, S[x2 + 1, y2 + 1] += c
// 左上右下 加 c
b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
7. 位运算
求n的第k位数字: n >> k & 1
返回n的最后一位1:lowbit(n) = n & -n // eg: 6 * -6 = 2
8. 双指针
for (int i = 0, j = 0; i < n; i ++ )
{
while (j < i && check(i, j)) j ++ ;
// 具体问题的逻辑
}
常见问题分类:
(1) 对于一个序列,用两个指针维护一段区间
(2) 对于两个序列,维护某种次序,比如归并排序中合并两个有序序列的操作
9. 离散化
vector<int> alls; // 存储所有待离散化的值
sort(alls.begin(), alls.end()); // 将所有值排序
alls.erase(unique(alls.begin(), alls.end()), alls.end()); // 去除重复元素
// 二分求出 x 对应的离散化的值
int find(int x) // 找到第一个 >= x 的位置
{
// return lower_bound(alls.begin(), alls.end(), x) - alls.begin() + 1;
int l = 0, r = alls.size() - 1;
while(l < r){
int mid = l + r >> 1;
if(alls[mid] >= x) r = mid;
else l = mid + 1;
}
return r + 1; // 映射到 1, 2, ... , n
}
10. 区间合并
vector<PII> segs;
void merge(vector<PII> &segs)
{
vector<PII> res;
sort(segs.begin(), segs.end());
int st = -2e9, ed = -2e9;
for (auto seg : segs)
if (ed < seg.first)
{
if (st != -2e9) res.push_back({st, ed});
st = seg.first, ed = seg.second;
}
else ed = max(ed, seg.second);
if (st != -2e9) res.push_back({st, ed});
segs = res;
}