双指针算法 位运算 区间合并

文章介绍了双指针算法在处理序列和合并有序序列中的应用,以及位运算在获取二进制位和统计二进制1个数中的作用。还详细阐述了如何使用贪心策略进行区间合并,通过排序和迭代找到无交集或有交集的区间,最终减少区间数量。
摘要由CSDN通过智能技术生成

双指针 位运算 离散化 区间合并


一 双指针算法

算法应用范围:

两个指针指向一个序列:用两个指针维持一段区间

两个指针指向两个序列:归并排序中合并两个有序序列的操作(两个指针指向一个序列)以及KMP算法中用两个指针分别指向两个序列。

代码模版:

for (int i = 0, j = 0; i < n; i++)
{
	while(j<i&&check(i,j)) j++:
	//具体问题的逻辑
}

代码实例:

#include<iostream>
#include<algorithm>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
#include<iomanip>
#include<fstream>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e3 + 10, null = 0x3f3f3f3f,M=2*N;
const double eps = 1e-6;
int a[N], b[N];//数组b记录每个数出现的次数
int n;
int main()
{
	ios;
	cin >> n;
	for (int i = 0; i < n; i++) cin >> a[i];
	int ans = 0;
	for (int i = 0, j = 0; i < n; i++)
	{
		b[a[i]]++;//每个数出现的次数加一
		while (b[a[i]] > 1)//只要新加入的数字重复,j右移,同时j指向的数出现的次数减一
		{
			b[a[j]]--;
			j++;
		}
		ans = max(ans, i - j + 1);//当区间内没有重复数字的时候,求区间长度的最大值
	}
	cout << ans << endl;
	return 0;
}

二 位运算

  1. n>>k&1表示n的二进制中逆序第k位数字是多少(下标从0开始)
#include<iostream>
#include<algorithm>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
#include<iomanip>
#include<fstream>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e3 + 10, null = 0x3f3f3f3f,M=2*N;
const double eps = 1e-6;
int a[N], b[N];//数组b记录每个数出现的次数
int n;
int main()
{
	ios;
	int n;
	cin >> n;
	int res = n >>2 & 1;//n的二进制中逆序第2个数字,下标从0开始
	cout << res << endl;
	return 0;
}

输入:5(二进制表示为101)
输出:1

  1. lowbit()函数,返回x的二进制中最后一位1

int lowbit(int x)//返回二进制中的最后一位1,可以用来统计二进制中1的个数,方法为
//用一个变量res表示x的二进制中1的个数,初始化为0,只要x不等于0,每次将这个数减去二进制中的最后一位。

代码实例:有n个数,统计每个数的二进制中一的个数

#include<iostream>
#include<algorithm>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
#include<iomanip>
#include<fstream>
#include<map>
#include<unordered_map>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e6 + 10, null = 0x3f3f3f3f;
const double eps=1e-6;
const int M=1e9+7;
int n;
int lowbit(int x)//返回二进制中的最后一位1,可以用来统计二进制中1的个数,方法为
//用一个变量res表示x的二进制中1的个数,初始化为0,只要x不等于0,每次将这个数减去二进制中的最后一位1,res++
{
	return x&-x;
}
int main()
{
	ios;
	cin>>n;
	int ans=0;
	while(n) 
	{
		n-=lowbit(n);
		ans++;
	}
	cout<<ans<<endl;
	return 0;
}

三 区间合并

模板:

vector<PII> merge(vector<PII> &a)
{
	vector<PII> res;//用来存储合并后的区间
	int st=-2e9,ed=-2e9;//表示当前要更新的区间,两个端点均初始化为-2e9
	sort(a.begin(),a.end());//首先将所有区间按照左端点从小到大进行排序
	for(vector<PII>::iterator i=a.begin();i!=a.end();i++)
	{
		if(ed<(*i).first)//如果要更新区间的右端点小于当前区间的左端点时,说明两个区间没有交集
		{
			if(st!=-2e9)//如果不是第一个区间的话,此处要加一个判断,因为前面初始化要更新区间的左右端点为-2e9
			{
				res.push_back({st,ed});//将该区间存入到res容器中
			}
			st=(*i).first;//更新要更新的区间为当前区间
			ed=(*i).second;
		}
		else  ed=max(ed,(*i).second);//要更新的区间与当前区间有交集,进行合并,更新一下右端点ed即可
	}
	if(st!=-2e9) res.push_back({st,ed});//为了防止只有一个区间,如果不是初始化的区间的话,则将其存入到容器res中
	return res;/
}

输入:
5

1 2

2 4

5 6

7 8

7 9

输出; 3

题目分析:

本题主要采取贪心算法,贪心策略在于我们要先对输入的区间按左端点从小到大进行排序,只考虑要更新区间与当前区间的相交情况。具体做法为,首先定义一个数据类型为pair的数组a用来成对储存相应的区间,通过一个merge函数来归并有交集的区间,最后通过输出合并后的数组大小,即为合并完成后区间个数。在merge函数中我们来具体实现贪心算法:首先将采用sort函数将所有区间按照左端点从小到大进行排序,用st和ed来记录要更新区间的左右端点,赋初值为-2e9表示均为区间(题目规定的数据范围所能达成的最大区间)最左端的位置,然后用for的范围遍历,依次处理每一个区间。根据贪心思想,对于每一个区间,我们只用考虑要更新区间和当前区间即可,而他们的相交情况一共有三种:1 . 要更新区间和当前区间没有交集 2.要更新区间与当前区间有部分交集 3. 要更新区间是当前区间的子集。其中第1种情况可以刻画成,要更新区间的右断点小于当前区间的左断点,这样便没有交集,而第2种和第3种情况属于有交集的情况,可以发生区间合并,合并最后的区间右端点即为两个区间中最大右端点,故可以用一条语句进行处理,代码为ed= max(ed, a.second)。进行完合并后的区间可以他们储存到res辅助数组当中,在for循环完毕后,最后一个区间还没放入数组中,但已经处理完了,此时为了防止数组中只含有一个区间的情况,加一个判断,如果st != -2e9(即不是第一个区间),则把其直接储存到数组中。最后返回res即为返回合并后的区间数组。详见如下代码。

#include<iostream>
#include<algorithm>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
#include<iomanip>
#include<fstream>
#include<map>
#include<unordered_map>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e6 + 10, null = 0x3f3f3f3f;
const double eps=1e-6;
const int mod=1e9+7;
int n;
vector<PII> a;
vector<PII> merge(vector<PII> &a)
{
	vector<PII> res;//用来存储合并后的区间
	int st=-2e9,ed=-2e9;//表示当前要更新的区间,两个端点均初始化为-2e9
	sort(a.begin(),a.end());//首先将所有区间按照左端点从小到大进行排序
	for(vector<PII>::iterator i=a.begin();i!=a.end();i++)
	{
		if(ed<(*i).first)//如果要更新区间的右端点小于当前区间的左端点时,说明两个区间没有交集
		{
			if(st!=-2e9)//如果不是第一个区间的话,此处要加一个判断,因为前面初始化要更新区间的左右端点为-2e9
			{
				res.push_back({st,ed});//将该区间存入到res容器中
			}
			st=(*i).first;//更新要更新的区间为当前区间
			ed=(*i).second;
		}
		else  ed=max(ed,(*i).second);//要更新的区间与当前区间有交集,进行合并,更新一下右端点ed即可
	}
	if(st!=-2e9) res.push_back({st,ed});//为了防止只有一个区间,如果不是初始化的区间的话,则将其存入到容器res中
	return res;/
}
int main()
{
	ios;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		a.push_back({x,y});
	}
	vector<PII> ans=merge(a);
	cout<<ans.size()<<endl;//输出合并后的区间个数
	return 0;
}

也可以使用map进行存储,因为map内部用红黑树实现,具有自动排序(按键值从小到大)的功能。
代码实例:

#include<iostream>
#include<algorithm>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
#include<iomanip>
#include<fstream>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e3 + 10, null = 0x3f3f3f3f,M=2*N;
const double eps = 1e-6;
map<int,int> a;
int n;
map<int,int> merge(map<int,int> &a)
{
	map<int,int> ans;
	int st=-2e9,ed=-2e9;
	for(map<int,int>::iterator it=a.begin();it!=a.end();it++)
	{
		if(ed<(*it).first) 
		{
			if(st!=-2e9) ans.insert({st,ed});
					st=(*it).first;
					ed=(*it).second;
		}
		else ed=max(ed,(*it).second);
	}
	if(st!=-2e9) ans.insert({st,ed});
	return ans;
}
int main()
{
	ios;
	cin>>n;
	while(n--)
	{
		int x,y;
		cin>>x>>y;
		a.insert({x,y});
	}
	map<int,int> ans=merge(a);
	for(map<int,int>::iterator it=ans.begin();it!=ans.end();it++) cout<<(*it).first<<' '<<(*it).second<<endl;
	return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值