贪心

区间选点

905. 区间选点 - AcWing

        N个区间,选尽量少的点,使得每个区间内至少包含一个选出的点

        按左端点对边排序,记录当前区间的右端点ed,如果当前边的左端点>ed,则开一个新的区间,更新ed为当前边的右端点;否则更新ed为ed和当前边的右端点两者的最小值

#include<bits/stdc++.h>
using namespace std;
int n;
typedef pair<int,int> PII;
vector<PII> vc;
int res;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		int a,b;cin>>a>>b;
		vc.push_back({a,b});
	}
	sort(vc.begin(),vc.end());
	int ed=-0x3f3f3f3f;
	for(int i=0;i<n;i++){
		int a=vc[i].first,b=vc[i].second;
		if(a>ed){
			res++;
			ed=b;
		}else{
			ed=min(ed,b);
		}
	}
	cout<<res;
} 

最大不相交区间数量

908. 最大不相交区间数量 - AcWing

        N个区间,选尽可能多的区间,并且选择的区间互不相交

        按右端点对边排序,记录当前区间的右端点ed,如果当前边的左端点>ed,则开一个新的区间,更新ed为当前边的右端点;否则跳过该边

#include<bits/stdc++.h>
using namespace std;
int n;
int res;
const int N=1e5+5;
struct node{
	int l,r;
	//按右端点排序
	bool operator<(const node& nd){
		return r<nd.r;
	}
};
vector<node>vc;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		int a,b;cin>>a>>b;
		vc.push_back({a,b});
	}
	sort(vc.begin(),vc.end());
	int ed=-0x3f3f3f3f;
	for(auto t:vc){
		int a=t.l,b=t.r;
		if(a>ed){
			res++;
			ed=b;
		}
	}
	cout<<res;
} 

区间分组

906. 区间分组 - AcWing

        N个区间分成若干组,每组内的区间两两之间(包括端点)没有交集,组数尽可能小

        按左端点对边排序,用小根堆维护各组的右端点,记小根堆的堆顶元素为ed,如果当前边的左端点>ed,则弹出堆顶,将当前边的右端点放入;否则将当前边的右端点直接放入。最后小根堆中右端点的数量即为组数

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef pair<int,int> PII;
vector<PII> vc;
int n;
int cnt;
priority_queue<int,vector<int>,greater<int> > heap;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
    	int a,b;cin>>a>>b;
		vc.push_back({a,b});
	}
	sort(vc.begin(),vc.end());
	for(int i=0;i<n;i++){
		int a=vc[i].first,b=vc[i].second;
		if(!heap.size())
			heap.push(b);
		else if(a<=heap.top())
			heap.push(b);
		else{
			heap.pop();
			heap.push(b);
		}
	}
	cout<<heap.size();
} 

区间覆盖

907. 区间覆盖 - AcWing

        N个区间中选出尽可能少的区间,覆盖一条线段

        按左端点对边排序,每次选择左端点满足条件的所有边中,右端点最远的边

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int st,ed;
int n;
vector<pair<int,int> >vc;
int res;
int main(){
	cin>>st>>ed>>n;
	for(int i=1;i<=n;i++){
		int a,b;cin>>a>>b;
		vc.push_back({a,b});	
	}
	sort(vc.begin(),vc.end());
	bool f=false;
	for(int i=0;i<n;){
		int j=i,r=-2e9;
		//在满足左边的所有里,找右端点最远的 
		while(j<n&&vc[j].first<=st){
			r=max(r,vc[j].second);
			j++;
		}
		if(r<st){
			res=-1;
			break;
		}
		res++;
		if(r>=ed){
			f=true;
			break;
		}
		st=r;
		i=j;
	}
	if(f) cout<<res;
	else cout<<-1;
} 

排队打水

913. 排队打水 - AcWing

        n个人排队打水,第i个人打水的时间为t[i],如何安排顺序使所有人等待时间之和最小

        按打水时间排序,让t[i]最小的人先打水


货仓选址

104. 货仓选址 - AcWing题库

        数轴上选择一点,使该点到n个点的距离之和最小

        当选择的点和n个点中处在最中间的点重合时,距离之和最小


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vic.GoodLuck

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值