###### 【USACO-Chapter1-1.2】【模拟】Milking Cows

【题目描述】

• 最长至少有一人在挤奶的时间段。
• 最长的无人挤奶的时间段。（从有人挤奶开始算起）

【输入格式】(milk2.in)

【输出格式】(milk2.out)

【输入样例】

3
300 1000
700 1200
1500 2100

【输出样例】

900 300

#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn = 5000+10;
struct time
{
int left;
int right;
}times[maxn];
int n;
void init()
{
freopen("milk2.in","r",stdin);
freopen("milk2.out","w",stdout);
}

{
scanf("%d",&n);
for(int i = 1;i <= n;i++)
{
scanf("%d%d",×[i].left,×[i].right);
}
}

int cmp(const void *a,const void *b)
{
return (*(time *)a).left - (*(time *)b).left;
}

int max(int a,int b)
{
return a > b ? a : b;
}

void solve()
{
qsort(&time[1],n,sizeof(times[0]),cmp);
int ans1 = 0,ans2 = 0;
int leftt = times[1].left,rightt = times[1].right;
for(int i = 2;i <= n;i++)
{
ans1 = max(ans1,rightt - leftt);
if(times[i].left <= rightt && times[i].right > rightt)
{
rightt = times[i].right;
}
else if(times[i].left > rightt)
{
ans2 = max(ans2,times[i].left - rightt);
leftt = times[i].left;
rightt = times[i].right;
}
}
if(ans1 == 0)ans1 = rightt - leftt;
printf("%d %d\n",ans1,ans2);
}

int main()
{
init();
solve();
return 0;
}

#### Milking Cows 挤牛奶

2015-04-25 22:03:35

#### Codeforces 383A Milking cows(贪心）

2014-02-03 14:37:41

#### USACO 1.2 Milking Cows (枚举)

2014-11-19 16:49:15

#### USACAO Milking Cows 线段树

2014-12-18 22:46:00

#### 【USACO】Milking Cows

2016-08-12 20:19:27

#### 【USACO1.2_1】★Milking Cows 挤牛奶

2014-04-07 16:05:34

#### USACO----Milking Cows

2018-03-19 16:41:38

#### cf#225-C - Milking cows-贪心

2016-03-23 09:24:56

#### USACO题目Milking Cows及代码解析

2012年10月23日 16KB 下载

#### 洛谷 P1204 [USACO1.2] 挤牛奶Milking Cows

2016-11-15 09:03:24

## 不良信息举报

【USACO-Chapter1-1.2】【模拟】Milking Cows