[Ybtoj high-efficiency advanced 1.2] C. Corral reservation [greedy]

Insert picture description here
Original title link

analysis

Greedy strategy:
Sort the bullpen by the left end point.

If the right end of the bullpen does not have a bullpen, add one,
then find the first one on the left end of the back that is larger than the right end and place the same bullpen as the right end cow, and
update ans by the way.

I haven't had a sample but 100pts is outrageous

Upload code

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

int n,ans[500001],num;

struct node
{
    
    
	int s,p;
}a[100001],b[100001];

int cmp(node x,node y)
{
    
    
	return x.s<y.s;
}

int main()
{
    
    
	cin>>n;
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>a[i].s>>b[i].s;
		a[i].p=b[i].p=i;
	}
	sort(a+1,a+n+1,cmp);
	sort(b+1,b+n+1,cmp);
	int j=1;
	for(int i=1;i<=n;i++)
	{
    
    
		if(j<=n) j++;
		if(!ans[b[i].p]) ans[b[i].p]=++num;
		while(a[j].s<=b[i].s&&j<=n) j++;
		ans[a[j].p]=ans[b[i].p];
	}
	cout<<num<<endl;
	for(int i=1;i<=n;i++)
	{
    
    
		cout<<ans[i]<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/dglyr/article/details/112132199