Corral reservation

topic

topic

practice

We now consider a greedy method, we press ll for all cattlel Sort it again, and then traverse from small to large. If all my corrals are working now, then I will create a new corral, otherwise I just pick a corral (it’s not working anyway, it’s the same), it’s not difficult I think it's right, but is it rigorous to prove it? I'd better fry yxc and QMQ.


prove:

By contradiction, suppose there is a scheme that makes the number of stalls needed less, and the number of stalls needed is mmm

Considering the above approach, the first new m + 1 m+1m+At the moment of 1 corral, it may be assumed that the current processing is theiii head of cattle.

Since all the cows are sorted from small to large according to the start time, now mmThe start time of the last cow in m corrals must be less than or equal to theiithThe start time of i cows.

And the front mmThe smallest end time of m corrals is greater than or equal to theiithThe start time of the i cow, so the grazing interval of the last cow in the first mm corral must include theiithe start time of i cows, so we foundm + 1 m+1m+1 interval has an intersection, so at leastm + 1 m+1 is requiredm+1 corral, contradictory.

Therefore, the optimal solution can be obtained by the above method, and the proof is completed.


Of course, as for the processing number and to see if this corral is free, I use the small root pile for processing, and the small root pile is sorted according to the liberation time (that is, the end time of the cow + 1).

Here I tested it again, in fact, the maximum number of coverage can also find out how many corrals are needed, such as [1, 5] [1,5][1,5 ] and[2, 6] [2,6][2,6] [ 2 , 5 ] [2,5] [2,5 ] covers the number of2 22 , so the answer is2 22. In fact, it is not difficult to think that if two intervals have an intersection, it means that they cannot be in the same corral, so if at mostnnIf n cannot be established,nnn corrals. (I don't know how to prove it. If you know, you can reply to me. I will write it in the blog, although I personally think it is right.)

Personally, here is a false proof, for mmFor m stalls, we consider each cow in the second stall. If there is no conflict between the range of this cow and the first stall, move it to the first stall, so that the second stall The cows in the first column have conflicts in the first column, and in the third column, the first and second columns are at the same time (first move to the lower numbered column), so that the third column is a conflict in both the first and second columns (at the same time The same is true for two-to-one, and it hasn’t changed), after repeating this way, tonnColumn n becomes at least one cow so thatnnThere is a conflict between n columns. (If it does not exist, at least one column will be missing.)

Of course, this method does not require a number, so let's honestly use the previous method.

Time complexity: O (nlogn) O(nlogn)O ( n l o g n )

Code

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define  N  51000
#define  M  1100000 
using  namespace  std;
typedef  pair<int,int> PII;
struct  node
{
    
    
	int  l,r,id;
}a[N];int  n;
int  b[M],ans=0,an[N],fuck[N];
inline  bool  cmp(node  x,node  y){
    
    return  x.l<y.l;}
priority_queue<PII,vector<PII>,greater<PII> >c;//小根堆
int  list[N],top=0;
int  main()
{
    
    
	scanf("%d",&n);
	for(int  i=1;i<=n;i++){
    
    scanf("%d%d",&a[i].l,&a[i].r);a[i].id=i;}
	sort(a+1,a+n+1,cmp);
	for(int  i=1;i<=n;i++)
	{
    
    
		fuck[a[i].id]=i;
		while(!c.empty()  &&  c.top().first<=a[i].l)list[++top]=c.top().second,c.pop();
		if(top>0)an[i]=list[top--];
		else  an[i]=++ans;
		c.push(make_pair(a[i].r+1,an[i]));
	}
	printf("%d\n",ans);
	for(int  i=1;i<=n;i++)printf("%d\n",an[fuck[i]]);
	return  0;
}

Experimental code, which is to test whether the idea behind is correct (of course it is correct, otherwise it will not be written):

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define  N  51000
#define  M  1100000 
using  namespace  std;
typedef  pair<int,int> PII;
struct  node
{
    
    
	int  l,r,id;
}a[N];int  n;
int  b[M],ans=0,an[N],fuck[N];
inline  bool  cmp(node  x,node  y){
    
    return  x.l<y.l;}
priority_queue<PII,vector<PII>,greater<PII> >c;
int  list[N],top=0;

//试验区 
int  f[M];
inline  int  mymax(int  x,int  y){
    
    return  x>y?x:y;}
///
int  main()
{
    
    
	scanf("%d",&n);
	for(int  i=1;i<=n;i++){
    
    scanf("%d%d",&a[i].l,&a[i].r);a[i].id=i;f[a[i].l]++;f[a[i].r+1]--;}
	int  anss=0;
	for(int  i=1;i<=1000000;i++)anss=mymax(anss,f[i]+=f[i-1]);
	sort(a+1,a+n+1,cmp);
	for(int  i=1;i<=n;i++)
	{
    
    
		fuck[a[i].id]=i;
		while(!c.empty()  &&  c.top().first<=a[i].l)list[++top]=c.top().second,c.pop();
		if(top>0)an[i]=list[top--];
		else  an[i]=++ans;
		c.push(make_pair(a[i].r+1,an[i]));
	}
	printf("%d\n",anss);
	for(int  i=1;i<=n;i++)printf("%d\n",an[fuck[i]]);
	return  0;
}

summary

In fact, you will find that whether it is this problem or the last sunscreen, it is solved by sorting lll orrrr , and then process another one.

Guess you like

Origin blog.csdn.net/zhangjianjunab/article/details/107746943