您的位置:首页 > 其它

hrbust/哈理工oj 1877 区间【水题】

2016-04-30 15:04 465 查看
区间
Time Limit: 1000 MS

Memory Limit: 32768 K

 

Total Submit: 148(56 users)

Total Accepted: 56(48 users)

Rating: 

Special Judge: No

 
Description

给你一些左闭右开的区间,有交集的区间可划分到一个集合。现在将所有区间划分为尽量少的集合。求最少的集合数。

Input

第一行是一个整数T代表测试数据组数

对于每组测试数据第一行是一个整数N(1<=N<=100000)代表区间个数。

接下来N行每行输入两个整数s,t代表[s,t)(0<=s<t<=2^31-1)。

Output

对于每组测试数据,输出最少的集合数。

Sample Input

3

4

0 1

1 2

2 3

3 4

4

0 2

1 3

2 4

3 5

4

0 4

1 5

2 4

3 6

Sample Output



2

1

Author

陈禹

思路:水题,对y排序然后维护当前集合最右端即可,如果有x大于了当前集合最右端,那么就更新。

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct zuobiao
{
int x,y;
}a[100002];
int cmp(zuobiao a,zuobiao b)
{
if(a.y!=b.y)return a.y<b.y;
else return a.x<b.x;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a,a+n,cmp);
int yy;
int output=1;
for(int i=0;i<n;i++)
{
//printf("%d %d\n",a[i].x,a[i].y);
if(i==0)
{
yy=a[i].y;
}
else
{
if(a[i].x>=yy)
{
yy=a[i].y;
output++;
}
}
}
printf("%d\n",output);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: