基础算法
快速排序模板
- 分治思想,将中间值赋为x 分成两份
- 双指针i , j向中间扫
- 递归思想
void quick_sort(int q[], int l ,int r)
{
int i = l - 1, j = r + 1, x = q[(l + r) / 2];
if(l >= r) return;
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 l = 0 , r = n - 1;//数的范围从0-n
while(l < r)
{
int mid = l + r >> 1;
if(check(mid)) //判断mid在哪个区间
r = mid;
else
l = mid + 1;
}
return l;
int l = 0, r = n - 1;
while(l < r)
{
int mid = l + r + 1 >> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
return l;
浮点数二分
const double eps = 10e6;//eps表示精度,若精度为10e4,则多2位
while(r - l > eps)
{
double mid = (l + r) / 2;
if(check(mid)) r = mid;
else l = mid;
}
return l ;
高精度加法
#include<iostream>
#include<vector>
using namespace std;
vector<int> add(vector<int> &A,vector<int> &B)//用引用无需拷贝,更快
{
vector<int> C;
int t = 0;//进位
if(A.size() < B.size()) return add(B, A);//让长的在上面
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(1);//如果超过十,向前进一
return C;
}
int main()
{
string a, b;//用字符串进行读数据
cin >> a >> b;
vector<int> A, B;
for(int i = a.size() - 1; i >= 0; i--)
A.push_back(a[i] - '0');//减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;
}
高精度减法
#include<iostream>
#include<vector>
using namespace std;
//判断A>=B
bool cmp(vector<int> &A, vector<int> &B)
{
if(A.size() != B.size())
return A.size() > B.size();
for(int i = A.size() - 1; i >= 0; i --)
{
if(A[i] != B[i])
return A[i] > B[i];
}
return true;
}
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;
}
while(C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a, b;
cin >> a >> b;
vector<int> 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');
if(cmp(A, B))
{
auto C = sub(A, B);
for(int i = C.size() - 1; i >= 0; i --)
cout << C[i];
}
else
{
auto C = sub(B, A);
cout << "-" ;
for(int i = C.size() - 1; i >= 0; i --)
cout << C[i];
}
return 0;
}
高精度乘法
#include<iostream>
#include<vector>
using namespace std;
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()
{
vector<int> A;
int b;
string a;
cin >> a >> b;
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;
}
高精度乘法
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
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;
}
int main()
{
vector<int> A;
int b, r;
string a;
cin >> a >> b;
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;
}
前缀和
作用:求一数组从 L到R 的和
for(int i = 1; i <= n; i ++)
S[i] = S[i - 1] + a[i];
a[l] + ... + a[r] = S[r] - S[l - 1];
二维前缀和
//S[i][j]是以第i行j列格子左上角的所有元素和
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
S[i][j] = S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1] + a[i][j];
//以x1,y1为左上角,x2,y2为右下角的子矩阵的和:
S[x2][y2] - S[x1 - 1][y2] - S[x2][y1 -1] + S[x1 - 1][y1 - 1];
一维差分
//给区间l到r中的每个数加上c
void insert(int l, int r, int c)
{
b[l] += c;
b[r + 1] -= c;
}
二维差分
//x1,y1为左上角 x2,y2为右下角的子矩阵中所有元素加上c
void insert(int x1, int y1, int x2, int y2, int c)
{
b[x1][y1] += c;
b[x2 + 1][y1] -= c;//角标为2加上1
b[x1][y2 + 1] -= c;
b[x2 + 1][y2 + 1] += c;
}
位运算
//n二进制数的第k位数:
cout << (n >> k & 1) << endl;
//返回n二进制数的最后一位1:
int lowbit(int n)
{
return n & -n;
}
离散化
vector<int> alls;//存储待离散化的值
sort(alls.begin(), alls.end());//排序
alls.erase(unique(alls.begin(), alls.end()), alls.end());//去重
//二分查找x对应离散化后的值
int find(int x)//找到第一个大于x的位置
{
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;
}
区间合并
将所有存在交集的区间合并
typedef pair<int, int> P;
vector<P> segs;
void merge(vector<P> &segs)
{
vector<P> 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;
}