题意:有n条长度为1的线段,现给出它们的区间,找出它们的位置,使得它们间的空隙数目最小
分析:贪心,按r,l进行排序,从左向右找,每次都取最右端,若r大于当前最后一个就可以放在r+1,r等于当前,那么当前的可以放在r-1的位置,r就放在r的位置,l大于当前最后一个,那么表示不相交,肯定有一个空隙
#include<iostream>
#include<string.h>
#include<sstream>
#include<set>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<math.h>
using namespace std;
struct node {
int l, r;
bool operator<(const node &u) {
return r == u.r ? l < u.l : r < u.r;
}
}p[100000+10];
int main() {
int kase,n;
cin >> kase;
while (kase--) {
cin >> n;
for (int i = 0; i < n; i++)cin >> p[i].l >> p[i].r;
sort(p, p + n);
int ant = p[0].r;//当前取长度1的线段的最后一个结点的r
int ans = 0;
for (int i = 0; i < n; i++) {
if (p[i].r == ant)continue;
if (p[i].r > ant&&p[i].l <= ant) {
ant++;
}
else if (p[i].l > ant) {
ans++;
ant = p[i].r;
}
}
cout << ans << endl;
}
return 0;
}