[AcWing算法基础课] 一.基础算法
Algorithms + Data Structures = Programs. ——Niklaus Wirth
本章包括排序、二分、高精度、前缀和与差分、双指针算法、位运算、离散化、区间合并等内容
目录
一.排序
-
快速排序
#include<iostream>
using namespace std;
const int N = 1e6 + 10;
int a[N];
void QuickSort(int a[],int l,int r)
{
if(l>=r) return;
int x=a[(l+r)>>1];
int i=l-1,j=r+1;// 因为下面的是最开始就将i,j移动一位,再判断,所以要在边界的两端前一位开始
while(i < j)
{
do i++; while(a[i] < x);
do j--; while(a[j] > x);
if(i < j)
swap(a[i],a[j]);
}
QuickSort(a,l,j);
QuickSort(a,j+1,r);
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i ++ )
scanf("%d", &a[i]);
QuickSort(a,0,n-1);
for (int i = 0; i < n; i ++ )
printf("%d ", a[i]);
return 0;
}
-
归并排序
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+10;
int a[N],tmp[N];
void Merge_sort(int a[],int l,int r)
{
if(l >= r) return ;
int mid = (l + r) >> 1;
//先分后合
Merge_sort(a, l, mid);
Merge_sort(a, mid+1, r);
int i = l,j = mid +1,k = 0;
//归并
while(i<=mid && j<=r)
{
if(a[i]<=a[j]) tmp[k++] = a[i++];
else tmp[k++] = a[j++];
}
//续尾
while(i <= mid) tmp[k++] = a[i++];
while(j <= r) tmp[k++] = a[j++];
for (i = l,j = 0; i <= r;)
a[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;
}
-
模板
作者:yxc
链接:https://www.acwing.com/blog/content/277/
来源:AcWing
快速排序算法模板
void quick_sort(int q[], int l, int r)
{
if (l >= r) return;
int i = l - 1, j = r + 1, x = q[l + 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, 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];
}
二.二分
了解更多:
输入样例:
6 3
1 2 2 3 3 4
3
4
5
输出样例:
3 4
5 5
-1 -1
#include <iostream>
using namespace std;
const int N = 1e5+10;
int n,m;
int a[N];
int main()
{
cin>>n>>m;
for(int i=0; i<n; i++) scanf("%d", &a[i]);
int x;
while (m -- )
{
cin >> x;
int l = 0, r = n-1;
while(l<r)
{
int mid = (l + r) >> 1;
if(a[mid]>=x) r = mid;
else l = mid + 1;
}
if (a[l] != x) cout << "-1 -1" << endl;
else{
cout<<l<<' ';
r = n-1;
while(l<r)
{
int mid = (l + r + 1) >> 1;
if(a[mid]>x) r = mid - 1;
else l = mid;
}
cout<<r<<endl;
}
}
return 0;
}
三.高精度
[练习] 高精度加减乘除_☆迷茫狗子的秘密基地☆-CSDN博客
四.前缀和与差分
[练习](一二维 )前缀和 与 差分_☆迷茫狗子的秘密基地☆-CSDN博客
五.双指针算法
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+10;
int a[N],cnt[N];
int main()
{
int n;
int res=0;
cin >> n;
for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
for (int i = 0,j = 0; i < n; i ++ ){
cnt[a[i]]++;
while(cnt[a[i]]>1){
cnt[a[j]]--;
j ++;
}
res = max(res, i+1-j);
}
cout << res;
return 0;
}
六.离散化
七.区间合并
输入样例:
3
1 2 3 2
2 5 2 3
1 4 3 4
输出样例:
8
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair<int, int> PII;
vector<PII> area;
int main()
{
int n;
int l,r;
cin >> n;
for (int i = 0; i < n; i ++ )
{
cin >> l >> r;
area.push_back({l,r});
}
sort(area.begin(), area.end());
int cnt=0;
int final=-2e9;
for(auto t: area)
{
if(t.first>final){
cnt++;
}
final=max(final,t.second);
}
cout << cnt;
return 0;
}
本文来自博客园,作者:泥烟,CSDN同名, 转载请注明原文链接:https://www.cnblogs.com/Knight02/p/15799106.html