0
点赞
收藏
分享

微信扫一扫

【acwing】905. 区间选点*(贪心)

J简文 2022-04-13 阅读 275
c++贪心

穿越隧道
在这里插入图片描述

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int,int> pii;
const int N = 1e5 + 10;
pii a[N];
bool st[N];
int n;
bool cmp(pii a, pii b){
	return a.y <= b.y;
}
int main(){
	scanf("%d",&n);
	for(int i = 0; i < n; i++){
		cin >> a[i].x >> a[i].y;
	}
	sort(a,a + n,cmp);
// 	for(int i = 0; i < n; i++){
// 		cout << a[i].x <<" " << a[i].y << endl;
// 	}
	int ans = n;
	st[0] = true;
	for(int i = 0, j = 0; i < n - 1; i++){
		j = i;
		while(j < n - 1 && a[i].y >= a[j + 1].x && !st[j + 1]){
//			cout <<"i = " << i <<"j = " << j << endl;
			ans--;
			st[j + 1] = true;
			j++;
		}
		i = j;//关键一步,若无,则只能过一个点
	}
	cout << ans << endl;
	return 0;
}
举报

相关推荐

0 条评论