〇 快速排序
void quick_sort( int q[] , int l , int r)
{
if(l > r) return;
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] );
}
quick_sort( q , l , j );
quick_sort( q , j + 1 , r );
}
〇 归并排序
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;
int 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,k = 0 ; i <= r ; i++,k++)
tmp[k] = q[i]
}
〇 二分
int bsearch_l(int l,int r)
{
while(l < r)
{
int mid = l + r >> 1;
if(check(mid)) r = mid;
else l = mid + 1;
}
return l;
}
int bsearch_r(int l,int r)
{
while(l < r)
{
int mid = l + r + 1 >> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
return l;
}
〇 高精度加法
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(1);
return C;
}
〇 高精度减法
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;
}
〇 高精度乘法
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;
}
return C;
}
〇 高精度除法
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;
}
〇 一维前缀和
for(int i = 1;i <= n;i++)
s[i] = s[i - 1] + a[i];
//可用于快速计算数组a中某区间的和
//要计算 a[l] 到 a[r] 的和
//即计算 s[r] - s[l - 1];
〇 二维前缀和
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] + a[i][j] + a[i - 1][j - 1];
//可用于快速求矩阵中以 ( x1 , y1 ) , ( x2 , y2 )为对角线的矩形区域的和
//和即 s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];
〇 一维差分
void insert(int l,int r,int c)
{
b[l] += c;
b[r + 1] -= c;
}
... ...
for(int i = 1;i <= n;i++) insert( i , i , a[i]);
//可快速完成将 [ l , r ] 区间的元素均加上 c 的操作
//执行 insert 函数即可
//对 数组b 做前缀和,即可得到 数组a
〇 二维差分
void insert(int x1,int y1,int x2,int y2,int c)
{
b[x1][y1] += c;
b[x2 + 1][y1] -= c;
b[x1][y2 + 1] -= c;
b[x2 + 1][y2 + 1] += c;
}
... ...
for(int i = 1;i <=n ;i++)
for(int j = 1;j <= n;j++)
insert( i , j , i , j , a[i][j]);
〇 离散化
vector<int> alls;
sort(alls.begin() , alls.end());
alls.erase(unique(alls.begin() , alls.end()) , alls.end());
int find(int x)
{
int l = 0,r = alls.size() - 1;
while(l < r)
{
int mid = l + r >> 1;
while(l < r)
{
if(alls[mid] >= x) r = mid;
else l = mid + 1;
}
}
return r + 1;
}
〇 区间合并
typedef pair<int,int> PII;
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;
}
欢迎订阅专栏,数据结构实验,期末大作业,前端后端,算法都有哦,想我发哪个方面的资源或文章可以私信我,免费的哦