poj3190

题目链接:http://poj.org/problem?id=3190

题意:一批奶牛要挤奶,挤奶的时候要呆在一个棚子里,并且一个棚子只能给一个奶牛。

解题思路:这道题目我一开始就想错了,导致了我WA了好多次,后来参照了http://blog.csdn.net/z309241990/article/details/19566625#comments这篇博客才写了出来

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=60000;
int n,use[maxn];
struct Node
{
    int l;
    int r;
    int pos;
    bool operator <(const Node &a)const
    {
	if(r==a.r)
	    return l>a.l;
	return r>a.r;
    }
}a[maxn];
priority_queue<Node> q;
bool cmp(Node a,Node b)
{
    if(a.l==b.l)
	return a.r<b.r;
    return a.l<b.l;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
	for(int i=0;i<n;i++)
	{
	    scanf("%d%d",&a[i].l,&a[i].r);
	    a[i].pos=i;
	}
	sort(a,a+n,cmp);
	q.push(a[0]);
	int now=0,ans=1;
	use[a[0].pos]=1;
	for(int i=1;i<n;i++)
	{
	    if(!q.empty()&&q.top().r<a[i].l)
	    {
		use[a[i].pos]=use[q.top().pos];
		q.pop();
	    }
	    else
	    {
		ans++;
		use[a[i].pos]=ans;
	    }
	    q.push(a[i]);
	}
	printf("%d\n",ans);
	for(int i=0;i<n;i++)
	    printf("%d\n",use[i]);
	while(!q.empty())
	    q.pop();
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值