P1803 凌乱的yyy / 线段覆盖
重点是排序的方法 定义一个结构体 分别存x y值 排序只需要排Y的大小关系即可(这样排列会在坐标面上 按照顺序排列) 再查找没有相交的线段个数
#include<iostream>
#include<algorithm>
using namespace std;
struct node {
int x;
int y;
} a[100006];
bool cmp(node c,node v) {
return c.y<v.y;
}
int main() {
int n;
cin>>n;
for(int i=0; i<n; i++) {
cin>>a[i].x>>a[i].y;
}
sort(a,a+n,cmp);
int L=0;
int ans=0;
int right=a[0].x;
while(L<n) {
if(a[L].x>=right) {
ans++;
right=a[L].y;
}
L++;
}
cout<<ans;
return 0;
}