# 1.2.1 Milking Cows 挤牛奶

1.2.1 Milking Cows 挤牛奶

Line 1:

Lines 2..N+1:

``````3
300 1000
700 1200
1500 2100
``````

``````900 300
``````

``````#include<stdio.h>
#include<stdlib.h>
struct TMP
{
int a,b;
}sum[5010];
long max1,max2;

int cmp(const void *c,const void *d)
{
return (*(struct TMP *)c).a-(*(struct TMP *)d).a;
}
int main()
{
int n,i;
int pos1,pos2;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d %d",&sum[i].a,&sum[i].b);
qsort(sum,n,sizeof(struct TMP),cmp);
pos1=sum[0].a;
pos2=sum[0].b;
if(n==1)
printf("%d 0\n",pos2-pos1);
else
{
for(i=1;i<n;i++)
{
if(sum[i].a<=pos2&&sum[i].b>pos2)
pos2=sum[i].b;
else if(sum[i].a>pos2)
{
if(pos2-pos1>max1)
max1=pos2-pos1;
if(sum[i].a-pos2>max2)
max2=sum[i].a-pos2;
pos1=sum[i].a;
pos2=sum[i].b;
}
}
if(pos2-pos1>max1)
max1=pos2-pos1;
if(sum[i].a-pos2>max2)
max2=sum[i].a-pos2;
printf("%d %d\n",max1,max2);

}
return 0;
}
``````

``````#include <stdio.h>
#include<stdlib.h>
typedef struct node
{
int x,y;
} node;
int n,myspace=0,result=0;
node f[10005];
int cmp(const void *a,const void *b)
{
return((*(node*)a).x-(*(node*)b).x);
}
void jisuan()
{
int i,head,maxx=f[0].y;
head=f[0].x;
for(i=1; i<n; i++)
{
if(f[i].x<=maxx&&f[i].y>maxx)maxx=f[i].y;
if(f[i].x>maxx)
{
if((maxx-head)>result)result=maxx-head;
if((f[i].x-maxx)>myspace)myspace=f[i].x-maxx;
head=f[i].x;
maxx=f[i].y;
}
}
if((maxx-head)>result)result=maxx-head;
printf("%d %d",result,myspace);
}
int main()
{
int i;
scanf("%d",&n);
for(i=0;i<n;i++)scanf("%d%d",&f[i].x,&f[i].y);
qsort(f,n,sizeof(f[0]),cmp);
jisuan();
return 0;
}
``````