算法笔记区间贪心

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
struct zuobiao {
	int x;
	int y;
}zuo[100];
bool cmp(zuobiao x, zuobiao y) {
	if (x.x != y.x) {
		return x.x > y.x;
	}
	else {
		return x.y < y.y;
	}
}
int main() {
	int n;
	int last;
	int ans;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d%d", &zuo[i].x, &zuo[i].y);
	}
	sort(zuo, zuo + n, cmp);
	last = zuo[0].x;
	ans = 1;
	for (int j = 1; j < n; j++) {
		if (zuo[j].y < last || zuo[j].y == last) {
			ans++;
			last = zuo[j].x;
		}
	}
	printf("%d", ans);
}

你可能感兴趣的:(算法,贪心算法)