Luo Gu P1803 messy yyy / line coverage

Topic links: Luo Gu P1803 messy yyy / line coverage

Program description:

Greedy algorithm interval scheduling problem, in an alternative work, always select the end time of the earliest work. All work by the end of the time from small to large, if time does not overlap the selection.
Note that since the definition of the structure of the sorting operation.
This question pit! ! Structure name can not be used time. . For this reason it does not compile. . .

code show as below:

#include <iostream>
#include <algorithm>
#define MAX 1000000
using namespace std;
struct point {
	int x, y;
} p[MAX];
bool cmp(point t1, point t2) {
	return t1.y < t2.y;
}
int n, cnt = 1;
int main() {
	cin>>n;
	for(int i = 0; i < n; i++)
		cin>>p[i].x>>p[i].y;
	sort(p, p + n, cmp);
	point t = p[0];
	for(int i = 1; i < n; i++) {
		if(p[i].x >= t.y) {
			cnt++;
			t = p[i];
		}			
	}
	cout<<cnt;
	return 0;
}
Published 44 original articles · won praise 0 · Views 834

Guess you like

Origin blog.csdn.net/Komatsu_1137/article/details/104073496