代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
struct T
{
int head; //区间首部
int tail; //区间尾部
}a[40010];
bool cmp(struct T a, struct T b) {//贪心策略:尾部按照从小到大排列,并且第一个区间一定要选
return a.tail < b.tail;
}
int main()
{
int m;
while(~scanf("%d", &m)) {//多组数据输入
for(int i = 0; i < m; i++) {
scanf("%d %d", &a[i].head, &a[i].tail);
int t;
if(a[i].head > a[i].tail) //交换首尾部,使首部小于尾部
{
t = a[i].head;
a[i].head = a[i].tail;
a[i].tail = t;
}
}
sort(a, a+m, cmp); //排序
int n = a[0].tail;
int sum = 0;
for(int i = 1; i < m; i++) {
if(a[i].head <= n)
sum++;
else n = a[i].tail;
}
printf("%d\n", sum);
}
return 0;
}