链接:http://acm.hdu.edu.cn/showproblem.php?pid=1051
思路:先将其按照长度升序排列,若长度相同,就按照宽度排序。然后从头开始取,找到符合条件的的元素,就将其标记表示其用过,再找到第一个没有用过的,在从这个地方开始遍历,符合条件的再标记,直到每个元素都用过。。
教训:一开始用while 写的 好乱啊啊。。。后来改成用for写,,还是for比较清楚一点,特别对于起点和终点比较明确的时候。。思维还是需要多锻炼。。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct state
{
int x,y;
}a[5001];
int mark[5001];
int cmp(state p,state q)
{
if(p.x != q.x)
return p.x < q.x;
else
return p.y < q.y;
}
int main ()
{
int t;
cin >> t;
while(t--)
{
memset(mark,0,sizeof(mark));
int n;
cin >> n;
for(int i = 0;i < n;i++)
cin >> a[i].x >> a[i].y;
sort(a,a+n,cmp);
int count = 0;
for(int i = 0;i < n;i++)
{
if(mark[i]==0) //没有被用过才使用
{
count++;
mark[i] = 1;
int cury = a[i].y;
for(int j = i+1;j < n;j++) //将当前的这个没有使用的,当做起点
{
if(mark[j]==0)
{
if(cury <= a[j].y)
{
cury = a[j].y; //更新前一个 y 。不用管 x 因为已经按 x排序了。
mark[j] = 1;
}
}
}
}
}
cout << count << endl;
}
return 0;
}