// 最常规的快速排序模板 voidQuickSort(int nums[], int l, int r){ if (l < r) { int pos = partition(nums, l, r); QuickSort(nums, l, pos - 1); QuickSort(nums, pos + 1, r); } } intpartition(int nums[], int left, int right){ int pivot = nums[left]; // 第一个数作为基准值 int low = left, high = right; while (low < high) { while (low < high && nums[high] >= pivot) high--; while (low < high && nums[low] <= pivot) low++; if (low < high) swap(nums[low], nums[high]); } swap(nums[left], nums[low]); return low; }
// 另外推荐一个简洁的快速排序模板 voidquickSort(int nums[], int l, int r){ if (l >= r) return; int i = l - 1, j = r + 1, x = nums[l + r >> 1]; while (i < j) { while (nums[++i] < x) ; while (nums[--j] > x) ; if (i < j) swap(nums[i], nums[j]); } quickSort(nums, l, j); quickSort(nums, j + 1, r); }
归并排序算法模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
voidmergeSort(int nums[], int l, int r){ if (l >= r) return; int mid = l + r >> 1; mergeSort(nums, l, mid); mergeSort(nums, mid + 1, r);
int k = 0, i = l, j = mid + 1; while (i <= mid && j <= r) { if (nums[i] <= nums[j]) tmp[k++] = nums[i++]; else tmp[k++] = nums[j++]; } while (i <= mid) tmp[k++] = nums[i++]; while (j <= r) tmp[k++] = nums[j++];
for (i = l, j = 0; i < r; i++, j++) nums[i] = tmp[j]; }
// 区间[l, r]被划分成[l, mid]和[mid + 1, r]时使用: intbsearch_1(int l, int r){ while (l < r) { int mid = l + r >> 1; // if 的判断条件是让 mid 落在满足你想要结果的区间内 if (check(mid)) r = mid; // check()判断mid是否满足性质 else l = mid + 1; } return l; }
// 区间[l, r]被划分成[l, mid - 1]和[mid, r]时使用: intbsearch_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; }
浮点数二分算法模板
1 2 3 4 5 6 7 8 9 10 11 12 13
boolcheck(double x){ /* ... */ } // 检查x是否满足某种性质
doublebsearch_3(double l, double r){ constdouble eps = 1e-6; // eps 表示精度,取决于题目对精度的要求 while (r - l > eps) { double mid = (l + r) / 2; if (check(mid)) r = mid; else l = mid; } return l; }
高精度加法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// C = A + B, A >= 0, B >= 0 // 这里ABC都是逆序排列的,如果是正序,修改i的方向即可 vector<int> add(vector<int> &A, vector<int> &B){ if (A.size() < B.size()) returnadd(B, A);
vector<int> C; int t = 0; for (int i = 0; i < A.size(); i++) { 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; }
高精度减法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// C = A - B, 满足A >= B, A >= 0, B >= 0 // 这里ABC都是逆序排列的,如果是正序,对应修改即可 vector<int> sub(vector<int> &A, vector<int> &B){ vector<int> C; int t = 0; for (int i = 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; }
// C = A * b, A >= 0, b >= 0 // 这里AC都是逆序排列的 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; }
高精度除以低精度
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// A / b = C ... r, A >= 0, b > 0 // 这里AC都是逆序排列的 vector<int> div(vector<int> &A, int b, int &r){ vector<int> C; r = 0; 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; }