[Ybt] [Basic Calculation Greedy Lesson 3] Corral reservation

Corral reservation

Topic link: Corral reservation


Title description

Insert picture description here
Insert picture description here

Problem solving ideas

Obviously, this is greedy.
Sort them first, and then add them one by one.
If the stalls have been used up before, just add them directly.
If all the stalls have not been used up, just open a new stall.

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;
}

Guess you like

Origin blog.csdn.net/SSL_guyixin/article/details/111714813