# USACO 1.2 Milking Cows

Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends at time 1200. The third farmer begins at time 1500 and ends at time 2100. The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).

Your job is to write a program that will examine a list of beginning and ending times for N (1 <= N <= 5000) farmers milking N cows and compute (in seconds):

• The longest time interval at least one cow was milked.
• The longest time interval (after milking starts) during which no cows were being milked.

### INPUT FORMAT

 Line 1: The single integer, N Lines 2..N+1: Two non-negative integers less than 1,000,000, respectively the starting and ending time in seconds after 0500

### SAMPLE INPUT (file milk2.in)

3
300 1000
700 1200
1500 2100



### OUTPUT FORMAT

A single line with two integers that represent the longest continuous time of milking and the longest idle time.

### SAMPLE OUTPUT (file milk2.out)

900 300

（100 200

201 300

/*
ID: myk5021
PROG: milk2
LANG: C++11
*/
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<climits>
using namespace std;
const int maxn=1000000+10;
int n,a[maxn],sum_a[maxn];
int main(void)
{
freopen("milk2.in","r",stdin);
freopen("milk2.out","w",stdout);
int ans1=0,ans2=0,left,right,continue_0,continue_1,lmin=maxn,rmax=0;
memset(a,0,sizeof(a));
memset(sum_a,0,sizeof(sum_a));
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>left>>right;
a[left]+=1;
a[right]-=1;
rmax=max(rmax,right);
lmin=min(lmin,left);
}
sum_a[0]=a[0];
for(int i=1;i<=rmax;i++)
sum_a[i]=sum_a[i-1]+a[i];
continue_1=continue_0=0;
for(int i=lmin;i<=rmax;i++)
{
if(sum_a[i]==0)
{
continue_0++;
continue_1=0;
}
else
{
continue_1++;
continue_0=0;
}
ans1=max(ans1,continue_1);
ans2=max(ans2,continue_0);
}
if(rmax-lmin==ans1)
{
ans2--;
}
printf("%d %d\n",ans1,ans2);
return 0;
}



• 本文已收录于以下专栏：

## 【USACO】Milking Cows

ProblemThree farmers rise at 5 am each morning and head for the barn to milk three cows. The first f...
• LeongHouHeng
• 2016年08月12日 20:19
• 314

## USACO Milking Cows(模拟)

• EaShion1994
• 2015年07月14日 19:43
• 358

## usaco 1.2:Milking Cows

描述三个农民每天清晨5点起床，然后去牛棚给3头牛挤奶。第一个农民在300时刻(从5点开始计时，秒为单位)给他的牛挤奶，一直到1000时刻。第二个农民在700时刻开始，在 1200时刻结束。第三个农民...
• gy_kw
• 2010年08月26日 21:56
• 200

## usaco 1.2 Milking Cows

/* ID:Ting Yu PROG:milk2 LANG:C++ */ #include #include #include using namespace std; int N,T1,T2; in...
• qq_36213111
• 2017年10月14日 19:07
• 89

## USACO 1.2 Milking Cows

Milking Cows  方法1： 动态规划？ y
• baidu_23081367
• 2014年11月12日 12:20
• 293

## Milking Cows 挤牛奶

• hyqsblog
• 2015年04月25日 22:03
• 558

## USACO 1.2 Milking Cows (枚举)

USCAO milk2 题解
• wdkirchhoff
• 2014年11月19日 16:49
• 952

## (模拟)usaco 1.2 Milking Cows

• cqlf__
• 2012年02月23日 00:21
• 798

## USACO 1.2 Milking Cows（暴力枚举）

Three farmers rise at 5 am each morning and head for the barnto milk three cows. The first farmer be...
• u013534690
• 2014年10月21日 22:46
• 398

## USACO Section 1.2 Milking Cows

/* ID: lucien23 PROG: milk2 LANG: C++ */ #include #include #include using namespace std; typedef s...
• LucienDuan
• 2013年11月23日 15:30
• 702

举报原因： 您举报文章：USACO 1.2 Milking Cows 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)