双指针算法
运用某些性质,将O(n2)的算法优化到O(n).递归排序,不同序列的双指针算法,快速排序,同一个序列的双指针算法
//模板
for (int i = 0, j = 0; i < n; i ++ )
{
while (j < i && check(i, j)) j ++ ;
// 具体问题的逻辑
}
常见问题分类:
(1) 对于一个序列,用两个指针维护一段区间
(2) 对于两个序列,维护某种次序,比如归并排序中合并两个有序序列的操作
//避免暴力循环,减少时间复杂度,如果整数过大,哈希表会是不错的选择
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5+10;
int a[N],q[N];
int main(){
int num;
int temp = 0;
scanf("%d",&num);
for(int i = 0;i<num;i++) scanf("%d",&a[i]);
for(int i = 0,j= 0 ;i<num;i++){
q[a[i]]++;
while(q[a[i]]>1){
q[a[j]]--;
j++;
}
temp = max(temp,i-j+1);
}
printf("%d",temp);
return 0;
}
//建议先暴力循环,然后找关系在此基础上修改
#include<iostream>
using namespace std;
const int N = 1e5+10;
int a[N],b[N];
int main(){
int num1,num2,num3,n1,n2;
scanf("%d%d%d",&num1,&num2,&num3);
for(int i = 0;i<num1;i++) scanf("%d",&a[i]);
for(int i = 0;i<num2;i++) scanf("%d",&b[i]);
for(int i = 0,j = num2-1;i<num1;i++){
while(j>=0&&a[i]+b[j]>num3) j--;
if(a[i]+b[j]==num3){
printf("%d %d",i,j);
break;
}
}
return 0;
}
//双指针判断子序列
#include<iostream>
using namespace std;
const int N = 1e5+10;
int a[N],b[N];
int main(){
int num1,num2,;
scanf("%d%d",&num1,&num2);
for(int i = 0 ;i<num1;i++) scanf("%d",&a[i]);
for(int i = 0 ;i<num2;i++) scanf("%d",&b[i]);
b[num2] = -1e9-1;
int i=0,j=0;
while (i < num1 && j < num2)
{
while (j < num2 && a[i] != b[j]) j++;
if (a[i] == b[j])
{
i++; j++;
}
}
if (i == num1) printf("Yes");
else printf("No");
return 0;
}
位运算
求n的第k位数字: n >> k & 1
返回n的最后一位1:lowbit(n) = n & -n lowbit(n) = n & (~n - 1)
//返回二进制表示中一的个数
#include<iostream>
using namespace std;
int lowbit(int x){
return x&-x;
}
int main(){
int num;
scanf("%d",&num);
while(num--){
int x;
int res=0;
scanf("%d",&x);
while(x){
x-=lowbit(x);
res++;
}
printf("%d ",res);
}
return 0;
}
离散化
//模板
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; // 映射到1, 2, ...n
}
//离散化加前缀和,处理稀疏数据
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<int,int> p;
const int N = 3e5+10;
//离散数组和前缀和数组
int a[N],s[N];
//存放两队数据
vector<p> add,place;
//存放初始数据
vector<int> alls;
int find(int x){
int l = 0;
int 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;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i = 0;i<n;i++){
int x,y;
cin>>x>>y;
add.push_back({x,y});
alls.push_back(x);
}
for(int i = 0;i<m;i++){
int x,y;
cin>>x>>y;
place.push_back({x,y});
alls.push_back(x);
alls.push_back(y);
}
sort(alls.begin(), alls.end()); // 将所有值排序
alls.erase(unique(alls.begin(), alls.end()), alls.end()); // 去掉重复元素
for(int i = 0;i<(int)add.size();i++){
int l = find(add[i].first);
a[l]+=add[i].second;
}
for(int i = 1;i<=(int)alls.size();i++) s[i] = s[i-1] + a[i];
for(int i = 0;i<(int)place.size();i++){
int l = find(place[i].first);
int r = find(place[i].second);
printf("%d\n",s[r]-s[l-1]);
}
return 0;
}
区间合并
// 模板,将所有存在交集的区间合并
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;
}
//实例,合并区间
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<int,int> p;
vector<p> range;
void merge(vector<p> & seg){
vector<p> res;
int l = -2e9;
int r = -2e9;
sort(seg.begin(),seg.end());
for(auto n:seg){
if(r<n.first){
if(l!=-2e9) res.push_back({l,r});
l=n.first;
r=n.second;
}
else r = max(r,n.second);
}
if(l!=-2e9) res.push_back({l,r});
seg = res;
}
int main(){
int num;
scanf("%d",&num);
for(int i = 0;i<num;i++){
int x,y;
cin>>x>>y;
range.push_back({x,y});
}
merge(range);
cout<<range.size();
return 0;
}