畜栏预定(贪心)

畜栏预定在这里插入图片描述

解题思路

排序
贪心
如果当前有空的栏
就将这个空的栏变成不空的栏
否则就新开一个栏

AC代码

#include<cstdio>
#include<algorithm>
using namespace std;
int n,o,mmax,q[1000005],num[1000005];
struct node
{
    
    
	int l,r;
}a[1000005];
bool cmp(node x,node y)//结构体排序
{
    
    
	if(x.l==y.l)return x.r<y.r;
	return x.l<y.l;
}
int main()
{
    
    
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	 scanf("%d%d",&a[i].l,&a[i].r);
	sort(a+1,a+n+1,cmp);//排序
	for(int i=1;i<=n;i++)//贪心
	{
    
    
		while(a[i].l>q[o]&&o!=0)o--;//找栏
		for(int j=1;j<=o+1;j++)
		 if(a[i].l>q[j])
		 {
    
    
		 	q[j]=a[i].r;
		 	num[i]=j;
		 	if(j==o+1)o++;
		 	mmax=max(o,mmax);
		 	break;
		 }
	}
	printf("%d\n",mmax);//输出
	for(int i=1;i<=n;i++)
	 printf("%d\n",num[i]); 
	return 0;
}

谢谢

猜你喜欢

转载自blog.csdn.net/weixin_45524309/article/details/112124601