STL

题意:有N条线段,求最少要将这些线段分成几组,使得每组中的线段都没有重叠

按左端点排序,然后每次挑之前分的组中右端点最左的那组,如果有重叠,就新开一组。

可用优先级队列进行维护(右端点最左的)

poj 3190 Stall Reservations

 

http://hi.baidu.com/zyz913614263/blog/item/cd3c5c2ee8c173138644f948.html

 

#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;

struct node{
	int l,r;
	int id;
	int room;
	bool operator < ( const node &t ) const{
		return r>t.r;//排序是从大到小  用在优先队列是小的在队首
	}
}a[60000];

int cmp(node a,node b)
{
	return a.l<b.l;	
}

int ans[60000];

int main()
{
	int n;
	int i,j;
	int tot=0;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&a[i].l,&a[i].r);
			a[i].id=i;
		}
		tot=1;
		priority_queue<node>q;
		sort(a,a+n,cmp);
		ans[a[0].id]=1;
		a[0].room=1;
		q.push(a[0]);
		for(i=1;i<n;i++)
		{
			node tmp=q.top();
			if(tmp.r<a[i].l)
			{
				q.pop();
				ans[a[i].id]=tmp.room;
				tmp.r=a[i].r;
				q.push(tmp);
			}
			else
			{
				ans[a[i].id]=++tot;
				a[i].room=tot;
				q.push(a[i]);
			}
		}
		printf("%d\n",tot);
		for(i=0;i<n;i++)
		{
			printf("%d\n",ans[i]);
		}
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值