【ybt】【基算 贪心 课过 例3】畜栏预定

畜栏预定

题目链接:畜栏预定


题目描述

在这里插入图片描述
在这里插入图片描述

解题思路

很明显,这是一个贪心。
先进行排序,然后逐个加入。
如果之前已经有畜栏结束使用了,直接加进去就可以了。
如果所有的畜栏都还没有结束使用,那么就新开一个畜栏即可。

code

#include<iostream>
#include<cstdio>
#include<algorithm> 
using namespace std;

int n,tot,ans;
int h[50010];
int s[50010];

struct abc{
    
    
	int x,y;
}a[50010];

bool cmp(abc a,abc b)
{
    
    
	if(a.x!=b.x)
		return a.x<b.x;
	return a.y<b.y;
}

int main()
{
    
    
	cin>>n;
	for(int i=1;i<=n;i++)
		scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;i++)
	{
    
    
		while(h[tot]<a[i].x&&tot>0)
			tot--;
		for(int j=1;j<=tot+1;j++)
			if(h[j]<a[i].x)
			{
    
    
				h[j]=a[i].y;
				s[i]=j;
				if(j==tot+1)
					tot++;
				ans=max(ans,tot);
				break;
			}
	}
	cout<<ans<<endl;
	for(int i=1;i<=n;i++)
		cout<<s[i]<<endl;
}

猜你喜欢

转载自blog.csdn.net/SSL_guyixin/article/details/111714813