1.快速排序
AcWing 785. 快速排序
#include <iostream>
using namespace std;
const int N = 1e5 + 1;
int cnt[N];
// 有等号 + 无等号 + 有等号 是最好的选择 101
void quick_sort(int l,int r){
if(l >= r) return; //可以无视等号
int pivot = cnt[l],i = l,j = r;
while(i < j){
while(cnt[j] >= pivot && i < j) j--; //可以无视 >=
cnt[i] = cnt[j];
while(cnt[i] <= pivot && i < j) i++;
cnt[j] = cnt[i];
}
cnt[i] = pivot;
quick_sort(l,i-1);
quick_sort(i+1,r);
}
int main(){
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> cnt[i];
quick_sort(1,n);
for(int i = 1; i <= n; i++) cout << cnt[i] << " ";
cout << endl;
return 0;
}
AcWing 786. 第k个数
#include <iostream>
using namespace std;
const int N = 1e5 + 1;
int cnt[N];
int quick_sort(int l,int r,int k){
int pivot = cnt[l],i = l,j = r;
while(i < j){
while(cnt[j] >= pivot && i < j) j--;
cnt[i] = cnt[j];
while(cnt[i] <= pivot && i < j) i++;
cnt[j] = cnt[i];
}
cnt[i] = pivot;
if(i == k) return pivot;
if(i < k) return quick_sort(i+1,r,k);
else return quick_sort(l,i-1,k);
}
int main(){
int n,k; cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> cnt[i];
cout << quick_sort(1,n,k) << endl;
return 0;
}
2.归并排序
AcWing 787. 归并排序
#include <iostream>
using namespace std;
const int N = 1e5 + 11;
int cnt[N],tmp[N];
void merge_sort(int l,int r){
if(l >= r) return;
int mid = (l+r)/2;
merge_sort(l,mid);
merge_sort(mid+1,r);
int k = 0,i = l,j = mid+1;
while(i <= mid && j <= r){
if(cnt[i] <= cnt[j]) tmp[k++] = cnt[i++];
else tmp[k++] = cnt[j++];
}
while(i <= mid) tmp[k++] = cnt[i++];
while(j <= r) tmp[k++] = cnt[j++];
k = 0;
for(int q = l; q <= r; q++,k++){
cnt[q] = tmp[k];
}
}
int main(){
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> cnt[i];
merge_sort(1,n);
for(int i = 1; i <= n; i++) cout << cnt[i] << " ";
cout << endl;
return 0;
}
AcWing 788. 逆序对的数量
#include <iostream>
using namespace std;
const int N = 1e5 + 1;
int cnt[N],tmp[N];
long long res = 0;
void merge_sort(int l,int r){
if(l >= r) return;
int mid = l + (r-l)/2;
merge_sort(l,mid);
merge_sort(mid+1,r);
int k = 0,i = l,j = mid+1;
while(i <= mid && j <= r){
if(cnt[i] <= cnt[j]){
tmp[k++] = cnt[i++];
}else{
res += mid-i+1; //有待深究,宏观来看 --> 美
tmp[k++] = cnt[j++];
}
}
while(i <= mid) tmp[k++] = cnt[i++];
while(j <= r) tmp[k++] = cnt[j++];
for(int q = l,k = 0; q <= r;q++,k++){
cnt[q] = tmp[k];
}
}
int main(){
res = 0;
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> cnt[i];
merge_sort(1,n);
cout << res << endl;
return 0;
}
3.二分
我的想法
// 如果想要最左边的一个2,那么就先砍掉比2小的数,l = mid+1;
// 然后 if(cnt[mid] >= 2) r = mid; 那么可以让右边缩小,保留最左边的
void solve1(){
int cnt[] = {1,2,2,3,3,4};
int l = 0,r = 5;
while(l < r){
int mid = l + (r-l)/2;
if(cnt[mid] < 2) l = mid+1;
else r = mid;
}
cout << l << endl;
}
//最右边的一个2
void solve2(){
int cnt[] = {1,2,2,3,3,4};
int l = 0,r = 5;
while(l < r){
int mid = (l+r+1)/2; // 当l+1=r 防止死循环
if(cnt[mid] > 2) r = mid-1;
else l = mid;
}
cout << l << endl;
}
AcWing 789. 数的范围
lower_bound()
upper_bound()
#include <iostream>
using namespace std;
const int N = 1e5 + 1;
int cnt[N];
// 1 2 2 3 3 4
int main(){
int n,q; cin >> n >> q;
for(int i = 0; i < n; i++) cin >> cnt[i];
while(q--){
int k; cin >> k;
int l = 0,r = n-1;
while(l < r){
int mid = l + (r-l)/2;
if(cnt[mid] < k) l = mid+1;
else r = mid;
}
if(cnt[l] != k) cout << "-1 -1" << endl;
else {
cout << l << " ";
l = 0, r = n-1;
while(l < r){
int mid = (l+r+1) >> 1;
if(cnt[mid] > k) r = mid-1;
else l = mid;
}
cout << l << endl;
}
}
return 0;
}
AcWing 790. 数的三次方根
#include <iostream>
using namespace std;
int main(){
double a; cin >> a;
double l = -100,r = 100;
while(r-l > 0.0000001){
double mid = (l+r)/2; //一定要用double
if(mid*mid*mid > a) r = mid;
else l = mid;
}
printf("%6lf\n",l);
return 0;
}
4.高精度
AcWing 791. 高精度加法
#include <iostream>
#include <vector>
using namespace std;
int main(){
string a,b; cin >> a >> b;
vector<int> v1,v2,v3;
for(int i = a.length()-1; i >= 0; i--) v1.push_back(a[i]-'0');
for(int i = b.length()-1; i >= 0; i--) v2.push_back(b[i]-'0');
int t = 0,i = 0,j = 0;
while(i <= v1.size()-1 || j <= v2.size()-1){
if(i <= v1.size()-1) t += v1[i++];
if(j <= v2.size()-1) t += v2[j++];
v3.push_back(t%10);
t/=10;
}
if(t != 0) v3.push_back(t);
for(int i = v3.size()-1; i >= 0; i--) cout << v3[i];
return 0;
}
AcWing 792. 高精度减法
注意两数相同的时候,输出-0
#include <iostream>
#include <vector>
using namespace std;
bool cmp(vector<int>& v1,vector<int>& v2){
if(v1.size() == v2.size()){
for(int i = v1.size()-1; i >= 0; i--){
if(v1[i] == v2[i]) continue;
return v1[i] > v2[i];
}
}else{
return v1.size() > v2.size();
}
}
vector<int> sub(vector<int>& v1,vector<int>& v2){
vector<int> res;
int t = 0;
for(int i = 0; i < v1.size(); i++){
v1[i] -= t;
if(i < v2.size()) v1[i] -= v2[i];
res.push_back((v1[i]+10)%10);
if(v1[i] < 0) t = 1;
else t = 0;
}
while(res.size() > 1 && res.back() == 0) res.pop_back();
return res;
}
int main(){
string s1,s2; cin >> s1 >> s2;
vector<int> v1,v2,v3;
for(int i = s1.length()-1; i >= 0; i--) v1.push_back(s1[i]-'0'); //个位排在前面
for(int i = s2.length()-1; i >= 0; i--) v2.push_back(s2[i]-'0');
if(cmp(v1,v2)){
v3 = sub(v1,v2);
}else{
cout << "-";
v3 = sub(v2,v1);
}
for(int i = v3.size()-1; i >= 0; i--) cout << v3[i];
return 0;
}
AcWing 793. 高精度乘法
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
const int N = 1e5 + 1;
vector<int> v1,v2;
int c[N];
vector<int> mul(){
vector<int> c(v1.size()+v2.size()+11,0);
for(int i = 0; i < v1.size(); i++){
for(int j = 0; j < v2.size(); j++){
c[i+j] += v1[i]*v2[j]; //注意这里 +=
if(c[i+j] > 9) c[i+j+1] += c[i+j]/10;
c[i+j] %= 10;
}
}
//去掉后置0
while(c.size() > 1 && c.back() == 0) c.pop_back();
return c;
}
int main(){
string a,b; cin >> a >> b;
for(int i = a.length()-1; i >= 0; i--) v1.push_back(a[i]-'0');
for(int i = b.length()-1; i >= 0; i--) v2.push_back(b[i]-'0');
vector<int> v3 = mul();
for(int i = v3.size()-1; i >= 0; i--) cout << v3[i];
return 0;
}
AcWing 794. 高精度除法
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
deque<int> res;
int main(){
string a; int b; cin >> a >> b;
vector<int> v1;
for(int i = 0; i < a.length(); i++) v1.push_back(a[i] - '0');
int t = 0;
for(int i = 0; i < v1.size(); i++){
t = t*10 + v1[i];
if(t < b){
res.push_back(0);
}else{
res.push_back(t/b);
t = t%b;
}
}
while(res.size() > 1 && res.front() == 0) res.pop_front();
for(int i = 0; i < res.size(); i++) cout << res[i];
cout << endl;
cout << t << endl;
return 0;
}
5. 前缀和与差分
AcWing 795. 前缀和
#include <iostream>
using namespace std;
const int N = 1e5 + 11;
int a[N];
int main(){
int n,m; cin >> n >> m;
a[0] = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
a[i] += a[i-1];
}
while(m--){
int l,r; cin >> l >> r;
cout << a[r]-a[l-1] << endl;
}
return 0;
}
AcWing 796. 子矩阵的和(二维前缀和)
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e3 + 11;
int cnt[N][N];
int qzh[N][N];
int main(){
int n,m,q; cin >> n >> m >> q;
memset(qzh,0,sizeof(qzh));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> cnt[i][j];
qzh[i][j] = cnt[i][j] + qzh[i][j-1] + qzh[i-1][j] - qzh[i-1][j-1];
}
}
while(q--){
int x1,y1,x2,y2; cin >> x1 >> y1 >> x2 >> y2;
int res = qzh[x2][y2] - qzh[x2][y1-1] - qzh[x1-1][y2] + qzh[x1-1][y1-1];
cout << res << endl;
}
return 0;
}
AcWing 797. 差分
#include <iostream>
using namespace std;
const int N = 1e5 + 11;
int a[N];
int b[N]; //差分数组
int main(){
int n,m; cin >> n >> m;
a[0] = 0;
b[0] = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
b[i] = a[i] - a[i-1];
}
while(m--){
int l,r,c; cin >> l >> r >> c;
b[l] += c;
b[r+1] -= c;
}
for(int i = 1; i <= n; i++){
b[i] += b[i-1];
cout << b[i] << " ";
}
return 0;
}
另外一种写法
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e5 + 11;
int a[N],b[N];
void insert(int l,int r,int c){
b[l] += c;
b[r+1] -= c;
}
int main(){
int n,m; cin >> n >> m;
memset(a,0,sizeof(a)); memset(b,0,sizeof(b));
for(int i = 1; i <= n; i++){
cin >> a[i];
insert(i,i,a[i]); //构造差分数组
}
while(m--){
int l,r,c; cin >> l >> r >> c;
insert(l,r,c);
}
for(int i = 2; i <= n; i++) b[i] += b[i-1]; //一系列操作之后的a数组
for(int i = 1; i <= n; i++) cout << b[i] << " ";
return 0;
}
AcWing 798. 差分矩阵
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1010;
int a[N][N],b[N][N];
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;
}
int main(){
//memset(a,0,sizeof(a)); memset(b,0,sizeof(b));
int m,n,q;
cin >> m >> n >> q;
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++) cin >> a[i][j];
//构造差分矩阵
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
insert(i,j,i,j,a[i][j]);
//操作
while(q--){
int x1,y1,x2,y2,c; cin >> x1 >> y1 >> x2 >> y2 >> c;
insert(x1,y1,x2,y2,c);
}
// b数组求前缀和就是 一系列操作之后的 a数组
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
b[i][j] = b[i][j] + b[i-1][j] + b[i][j-1] - b[i-1][j-1];
// 输出b数组
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++) cout << b[i][j] << " ";
cout << endl;
}
return 0;
}
6. 双指针算法
AcWing 799. 最长连续不重复子序列
#include <iostream>
#include <unordered_map>
using namespace std;
const int N = 1e5 + 11;
int a[N];
unordered_map<int,int> mp;
int main(){
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
int j = 1,res = 0;
for(int i = 1; i <= n; i++){
mp[a[i]]++;
if(mp[a[i]] > 1){
while(mp[a[j]] == 1){
mp[a[j]]--; //一定要清除j之前 map中的数据,后面的数据会累加
j++;
}
mp[a[j]]--;
j++;
}
res = max(res,i-j+1);
}
cout << res << endl;;
return 0;
}
#include <iostream>
using namespace std;
const int N = 1e5 + 11;
int a[N];
int main(){
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
int start = 1;
int ans = 0;
for(int i = 1; i <= n; i++){
for(int j = start; j < i; j++){
if(a[i] == a[j]){
ans = max(ans,i-j+1);
start = j+1;
break;
}
}
}
cout << ans << endl;
return 0;
}
AcWing 3624. 三值字符串
#include <string.h>
#include <stdio.h>
//#include <iostream>
using namespace std;
int min(int a,int b){
if(a >= b) return b;
return a;
}
const int N = 2e5 + 11;
char s[N];
int main(){
//std::ios::sync_with_stdio(false); cin.tie(0);
int T; scanf("%d",&T);
while(T--){
scanf("%s",s);
int n = strlen(s);
int cnt[4] = {0};
int j = 0, res = n+1;
for(int i = 0; i < n; i++){
cnt[s[i]-'0']++;
if(cnt[s[i]-'0'] > 1){
while(cnt[s[j]-'0'] > 1 && j < i) cnt[s[j++]-'0']--;
}
if(cnt[1] && cnt[2] && cnt[3]) res = min(res,i-j+1);
}
if(res == n+1) res = 0;
printf("%d\n",res);
}
return 0;
}
求子段和小于等于𝑠的最长子段
注意 LL sum
#include <iostream>
using namespace std;
const int N = 3e5 + 11;
int a[N];
typedef long long LL;
int main(){
int n; LL s;
bool has_ans = false;
scanf("%d%lld",&n,&s);
for(int i = 1; i <= n; i++){
scanf("%d",&a[i]);
if(s >= a[i]) has_ans = true;
}
if(has_ans == false){
printf("0\n");
return 0;
}
int l = 1, res = 0;
LL sum = 0;
for(int i = 1; i <= n; i++){
sum += a[i];
while(sum > s){
sum -= a[l];
l++;
}
res = max(res,i-l+1);
// cout << "i == " << i << " j == " << j << endl;
// cout << "w == " << w << endl;
// cout << "res == " << res << endl;
// cout << "-----" << endl;
}
printf("%d\n",res);
return 0;
}
求子段和大于等于𝑠的最短子段
#include <iostream>
using namespace std;
const int N = 3e5 + 11;
int a[N];
typedef long long LL;
int main(){
int n; LL s;
bool has_ans = false;
scanf("%d%lld",&n,&s);
LL t_sum = 0;
for(int i = 1; i <= n; i++){
scanf("%d",&a[i]);
t_sum += a[i];
}
if(t_sum < s){
printf("-1\n");
return 0;
}
int l = 1,res = n + 1;
LL sum = 0;
for(int i = 1; i <= n; i++){
sum += a[i];
while(sum - a[l] > s && l < i){
sum -= a[l];
l++;
}
if(sum >= s) res = min(res,i-l+1);
}
printf("%d\n",res);
return 0;
}
AcWing 800. 数组元素的目标和
使用unordered_map
#include <iostream>
#include <unordered_map>
using namespace std;
const int N = 100010;
int a[N];
unordered_map<int,int> mp;
int main(){
int n,m,x; scanf("%d%d%d",&n,&m,&x);
for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
for(int i = 1; i <= m; i++){
int t; scanf("%d",&t);
mp[t] = i;
}
for(int i = 1; i <= n; i++){
if(mp.count(x-a[i])){
printf("%d %d\n",i-1,mp[x-a[i]]-1);
break;
}
}
return 0;
}
双指针
#include <iostream>
#include <unordered_map>
using namespace std;
const int N = 100010;
int a[N],b[N];
int main(){
int n,m,x; scanf("%d%d%d",&n,&m,&x);
for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
for(int i = 1; i <= m; i++) scanf("%d",&b[i]);
int j = m;
for(int i = 1; i <= n; i++){
while(j > 0 && a[i] + b[j] > x) j--;
if(a[i] + b[j] == x){
printf("%d %d\n",i-1,j-1);
return 0;
}
}
return 0;
}
AcWing 2816. 判断子序列
1e5 为啥不行?
#include <iostream>
using namespace std;
const int N = 2e5;
int a[N],b[N];
int main(){
int n,m; scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
for(int i = 1; i <= m; i++) scanf("%d",&b[i]);
int j = 1;
bool isok = false;
for(int i = 1; i <= n; i++){
while(a[i] != b[j] && j < m) j++;
if(i == n && j <= m && a[i] == b[j]){
isok = true;
break;
}
j++;
}
if(!isok) puts("No");
else puts("Yes");
return 0;
}
7. 位运算
AcWing 801. 二进制中1的个数
#include <iostream>
using namespace std;
int lowbit(int a){
return a&-a;
}
int main(){
int n; cin >> n;
for(int i = 1; i <= n; i++){
int t; cin >> t;
int res = 0;
while(t > 0){
res++;
t -= lowbit(t);
}
cout << res << " ";
}
return 0;
}
这个样子会慢一点
#include <iostream>
using namespace std;
int main(){
int n; cin >> n;
for(int i = 1; i <= n; i++){
int t; cin >> t;
int res = 0;
for(int i = 0; i < 32; i++){
if((t >> i) & 1) res++;
}
cout << res << " ";
}
// int t = 0x7fffffff;
// cout << t << endl;
// for(int i = 0; i < 32; i++) cout << ((t >> i)&1);
return 0;
}
7. 离散化
AcWing 802. 区间和
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 3e5 + 11;
int a[N],s[N];
vector<int> all;
vector<pair<int,int> > add,query;
//find x in all's index
int find(int x){
int l = 0, r = all.size()-1;
while(l <= r){
int mid = l + (r-l)/2;
if(all[mid] == x) return mid;
if(all[mid] < x) l = mid + 1;
else r = mid - 1;
}
return l;
}
int main(){
int n,m; scanf("%d%d",&n,&m);
int a[N] = {0};
for(int i = 1; i <= n; i++){
int x,c; scanf("%d%d",&x,&c);
all.push_back(x);
add.push_back({x,c});
}
for(int i = 1; i <= m; i++){
int l,r; scanf("%d%d",&l,&r);
all.push_back(l);
all.push_back(r);
query.push_back({l,r});
}
sort(all.begin(),all.end());
all.erase(unique(all.begin(),all.end()),all.end());
//离散化 a[1开始]
for(int i = 0; i < add.size(); i++){
a[find(add[i].first)+1] += add[i].second; //add[0].first = 1, c = 3 --> a[0] += 3
//cout << "i == " << i << " " << a[i] << endl;
}
for(int i = 1; i <= all.size(); i++){
a[i] += a[i-1];
}
for(int i = 0; i < query.size(); i++){
int l = find(query[i].first)+1;
int r = find(query[i].second)+1;
cout << a[r] - a[l-1] << endl;
}
return 0;
}
8. 区间合并
AcWing 803. 区间合并
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<pair<int,int> > all;
int main(){
int n; cin >> n;
while(n--){
int l,r; cin >> l >> r;
all.push_back({l,r});
}
sort(all.begin(),all.end());
int ans = 1;
// for(int i = 1; i < all.size(); i++){
// cout << all[i].first << " " << all[i].second << endl;
// }
int t_max = all[0].second;
for(int i = 1; i < all.size(); i++){
if(all[i].first > t_max){
ans++;
}
t_max = max(t_max,all[i].second);
}
cout << ans << endl;
return 0;
}