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

题目描述

三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒。第二个农民在700秒开始,在 1200秒结束。第三个农民在1500秒开始2100秒结束。期间最长的至少有一个农民在挤奶的连续时间为900秒(从300秒到1200秒),而最长的无人挤奶的连续时间(从挤奶开始一直到挤奶结束)为300秒(从1200秒到1500秒)。

你的任务是编一个程序,读入一个有N个农民(1 <= N <= 5000)挤N头牛的工作时间列表,计算以下两点(均以秒为单位):

最长至少有一人在挤奶的时间段。

最长的无人挤奶的时间段。(从有人挤奶开始算起)

输入输出格式

输入格式:

 

Line 1:

一个整数N。

Lines 2..N+1:

每行两个小于1000000的非负整数,表示一个农民的开始时刻与结束时刻。

 

输出格式:

 

一行,两个整数,即题目所要求的两个答案。

 

输入输出样例

输入样例#1:
3
300 1000
700 1200
1500 2100
输出样例#1:
900 300

说明

题目翻译来自NOCOW。

USACO Training Section 1.2

 

水题 模拟 

屠龙宝刀点击就送

#include <algorithm>
#include <ctype.h>
#include <cstdio>
#define N 5005
using namespace std;
inline void read(int &x)
{
    register char ch=getchar();
    for(x=0;!isdigit(ch);ch=getchar());
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
}
struct node
{
    int l,r;
    bool operator<(node a)const
    {
        return l<a.l;
    }
}t[N];
int n,ans1,ans2;
int main()
{
    read(n);
    for(int i=1;i<=n;i++) read(t[i].l),read(t[i].r);
    sort(t+1,t+1+n);
    int nowl=t[1].l,nowr=t[1].r,ans1=t[1].r-t[1].l;
    for(int i=2;i<=n;i++)
    {
        if(t[i].l>=nowl&&t[i].l<=nowr) nowr=max(nowr,t[i].r);
        else 
        {
            ans1=max(ans1,nowr-nowl);
            ans2=max(ans2,t[i].l-nowr); 
            nowl=t[i].l;
            nowr=t[i].r;
        }
    }
    printf("%d %d",ans1,ans2);
    return 0;
}

 

posted @ 2017-08-17 11:40  杀猪状元  阅读(289)  评论(0编辑  收藏  举报