[comprehensive question] farmers' milking problem

[problem description]
Three farmers get up at 5 a.m. every morning and then go to the cowshed to milk three cows. The first farmer milked his cow in 300 seconds (counting from 5 o'clock) until 1000 seconds. The second farmer starts in 700 seconds and ends in 1200 seconds. The third farmer starts in 1500 seconds and ends in 2100 seconds. The longest continuous time of at least one farmer milking was 900 seconds (from 300 seconds to 1200 seconds), while the longest continuous time of no milking (from the beginning of milking to the end of milking) was 300 seconds (from 1200 seconds to 1500 seconds).
Your task is to compile a program, read in a list of working hours when N farmers (1 < = N < = 5000) squeeze N cows, and calculate the following two points (both in seconds):

At least one person is milking for the longest period of time.
The longest period of no milking. (since someone milked)

Tip: if the number of farmers milking at each time point is directly judged one by one, it requires a large space cost and a slow speed. You can sort the time in ascending order first, and then scan one by one from left to right to maintain a current interval [tmp_begin, tmp_end], if the begin of the next group of data is greater than TMP_ If end is small (or equal to), the time period is continuous. Check the en of this group of data and go to max{end, tmp_end} as the value of tmp_end. Otherwise, the interval is disconnected, record the interval length and continue the operation to obtain the maximum interval length. There are many solutions to this problem. Students are welcome to use their own methods. If it is very clever, you can consider adding points.

[input form]
The first line is integer N, and then N lines, each line has two non negative integers less than 1000000, representing the start time and end time of each farmer

[output form]
The longest milking time with at least one person and the longest non milking time (within 1s)

[sample input]

3

300 1000

700 1200

1500 2100

[sample output]

900 300

[example description]

[scoring criteria]
Please write the necessary notes in the program. If the program does not have the necessary notes, points will be deducted as appropriate. Due to the large data scale of this question, if you use simple data structures and algorithms, you may not be able to meet the requirements of time efficiency, so please try to use more advanced data structures and solutions. If you fail to meet the time requirements, we will give points according to the time efficiency. The higher the efficiency, the higher the score.

#include<stdio.h>
#include<stdlib.h>
typedef struct
{
	long long begin;
	long long end;
	
}Node,*PNode;//Start and end of interval
int cmp(const void *a,const void *b)
{
	Node c=*(Node *)a;
	Node d=*(Node *)b;
	return c.begin-d.begin;
}
long long max(long long *a,int n)
{
	long long max=a[0];
	int i;
	for(i=0;i<n;i++)
		if(a[i]>max)
			max=a[i];
	return max;
}

int main()
{
	long int n;
	int i,j;
	scanf("%ld",&n);
	PNode p;
	p=(Node *)malloc(n*sizeof(Node));
	for(i=0;i<n;i++)
		scanf("%lld%lld",&p[i].begin,&p[i].end);
	qsort(p,n,sizeof(p[0]),cmp);
	// for(i=0;i<n;i++)
		// printf("%ld %ld  ",p[i].begin,p[i].end);
	int a[n];//It is used to record whether it is continuous with the previous time period. Continuous is 1 and discontinuous is 0
	a[0]=1;
	for(i=1;i<n;i++)
	{
		if(p[i].begin<=p[i-1].end)
			a[i]=1;
		else a[i]=0;
	}
	//for(i=0;i<n;i++)
		// printf("%d ",a[i]);
	//printf("\n");
	long long  b[1000]={0};//Record the period of milking
	long long c[1000]={0};//Record the time period of no milking
	int k1=0,k2=0,m=0;//k1 is the array length of c and k2 is the array length of b
	for(i=0;i<n;i++)
	{	if(a[i]==0)
		{	
			long long max=p[m].end;
			for(j=m;j<i;j++)
				if(p[j].end>max)
					max=p[j].end;
			if(max<p[i].begin)
			{
				c[k1++]=p[i].begin-max;
				m=i;
			}
			else 	
				c[k1++]=0;
			//printf("%lld ",c[k1-1]);
			
		}
		if(a[i]==1)
		{	
			b[k2++]=p[i].end-p[m].begin;
			//printf("%ld %ld\n",p[m].begin,b[k2-1]);
		}

	}
	printf("%lld %lld",max(b,k2),max(c,k1));
	free(p);
	return 0;
}


Added by jkrettek on Wed, 22 Dec 2021 22:55:04 +0200