快速排序
确定分界点:
x = q[(l + r) / 2]
,l
,r
调整范围左边大于x
,右边小于x
递归处理左边和右边
#include <iostream>
using namespace std;
const int N = 100010;
int q[N];
int n;
void quick_sort(int q[], int l, int r){
if (l >= r) return;
int x = q[l + r >> 1], i = l - 1, j = 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);//左右两边分别递归
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i ++){
scanf("%d", &q[i]);
}
quick_sort(q, 0, n - 1);
for(int i = 0; i < n; i ++)
printf("%d ", q[i]);
return 0;
}
应用:
#include <iostream>
using namespace std;
const int N = 100010;
int n, k;
int q[N];
int quick_sort_k(int q[], int l, int r, int k){
if( l >= r) return q[l];
int x = q[l + r >> 1];
int i = l - 1, j = 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]);
}
if(j - l + 1 >= k) return quick_sort_k(q, l, j, k);//如果k在l到j中
else return quick_sort_k(q, j + 1, r, k - (j - l + 1));
}
int main()
{
cin >> n >> k;
for(int i = 0; i < n; i ++) cin >> q[i];
cout << quick_sort_k(q, 0, n - 1, k) << endl;
return 0;
}
归并排序
确定分界点
mid = (l + r) / 2
,[L, R]
=>[L, mid]
,[mid + 1, R]
递归排序[L, mid]
和[mid + 1, R]
归并,双指针表示剩余部分中最小元素的位置,左右两个序列合并成一个有序序列
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N], tmp[N];
void merge_sort(int q[], int l, int r)
{
if (l >= r) return;
int mid = l + r >> 1;
merge_sort(q, l, mid), merge_sort(q, mid + 1, r);
int k = 0, i = l, j = mid + 1;
while (i <= mid && j <= r)
if (q[i] <= q[j]) tmp[k ++ ] = q[i ++ ];
else tmp[k ++ ] = q[j ++ ];
while (i <= mid) tmp[k ++ ] = q[i ++ ];
while (j <= r) tmp[k ++ ] = q[j ++ ];
for (i = l, j = 0; i <= r; i ++, j ++ ) q[i] = tmp[j];
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
merge_sort(a, 0, n - 1);
for (int i = 0; i < n; i ++ ) printf("%d ", a[i]);
return 0;
}
应用:
此时
q[i]
>q[j]
, 则q[i]
后面的数都大于q[j]
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int a[N], tmp[N];
LL merge_sort(int q[], int l, int r)
{
if (l >= r) return 0;
int mid = l + r >> 1;
LL res = merge_sort(q, l, mid) + merge_sort(q, mid + 1, r);
int k = 0, i = l, j = mid + 1;
while (i <= mid && j <= r)
if (q[i] <= q[j]) tmp[k ++ ] = q[i ++ ];
else
{
res += mid - i + 1;
tmp[k ++ ] = q[j ++ ];
}
while (i <= mid) tmp[k ++ ] = q[i ++ ];
while (j <= r) tmp[k ++ ] = q[j ++ ];
for (i = l, j = 0; i <= r; i ++, j ++ ) q[i] = tmp[j];
return res;
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
cout << merge_sort(a, 0, n - 1) << endl;
return 0;
}
二分
模板
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;
}
check(mid)
检查mid
是否满足某种性质
如果需要查找的边界是区间的右端点,满足该条件为true
,此时l = mid
,false
为r = mid - 1(l + r + 1)
如果需要查找的边界是区间的左端点,满足该条件为true
,此时r = mid
,false
为l = mid + 1
应用:
#include <iostream>
using namespace std;
const int N = 100010;
int n, m;
int q[N];
int main()
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
while(m --){
int x;
scanf("%d", &x);
int l = 0, r = n - 1;
while (l < r){
int mid = l + r >> 1;
if(q[mid] >= x) r = mid;
else l = mid + 1;
}
if(q[l] != x) cout << "-1 -1"<< endl;
else{
cout << l <<' ';
int l = 0, r = n - 1;
while(l < r){
int mid = (l + r + 1) >> 1;
if(q[mid] <= x) l = mid;
else r = mid - 1;
}
cout << l << endl;
}
}
return 0;
}
浮点数二分
bool check(double x) {/* ... */} // 检查x是否满足某种性质
double bsearch_3(double l, double r)
{
const double eps = 1e-6; // eps 表示精度,取决于题目对精度的要求
while (r - l > eps)
{
double mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid;
}
return l;
}
应用
#include <iostream>
using namespace std;
int main()
{
double n;
cin >> n;
double l = -10000, r = 10000;
while(r - l > 1e-8){
double mid = (l + r) / 2;
if(mid * mid * mid >= n) r = mid;
else l = mid;
}
printf("%lf\n", l);
return 0;
}
高精度计算
高精度加法
// C = A + B, A >= 0, B >= 0
vector<int> add(vector<int> &A, vector<int> &B)
{
if (A.size() < B.size()) return add(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;
}
高精度减法
// C = A - B, 满足A >= B, A >= 0, B >= 0
vector<int> sub(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;
}
高精度乘低精度
// C = A * b, A >= 0, b >= 0
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;
}
高精度除以低精度
// A / b = C ... r, A >= 0, b > 0
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;
}