1.2.1 Milking Cows 挤牛奶

Description

三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300时刻(从5点开始计时,秒为单位)给他的牛挤奶,一直到1000时刻。第二个农民在700时刻开始,在 1200时刻结束。第三个农民在1500时刻开始2100时刻结束。期间最长的至少有一个农民在挤奶的连续时间为900秒(从300时刻到1200时刻),而最长的无人挤奶的连续时间(从挤奶开始一直到挤奶结束)为300时刻(从1200时刻到1500时刻)。 你的任务是编一个程序,读入一个有N个农民(1 <= N <= 5000)挤N头牛的工作时间列表,计算以下两点(均以秒为单位): 最长至少有一人在挤奶的时间段。 最长的无人挤奶的时间段。(从有人挤奶开始算起)

Input

Line 1: 一个整数N。 Lines 2..N+1: 每行两个小于1000000的非负整数,表示一个农民的开始时刻与结束时刻。

Output

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

Sample Input

3
300 1000
700 1200
1500 2100

Sample Output

900 300

 思路:如果第i行的 end[i] 数据大于i+1行的 start[i+1] 数据,则把第i行的 start[i] 数据赋值给 start[i] ,然后依次用max进行取值,如果发现begin[i+1]>end[i],则进行相减的出结果。

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>

typedef long long LL;
const long long INF = 0x3f3f3f3f;
const long long mod = 1e9+7;
const double PI = acos(-1.0);
const int dir4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir8[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
const int maxx = 100010;
using namespace std;
int cmp(int a,int b)
{
    return a<b;
}
int main()
{
    int n;
    int max1=0,max2=0,begin1[5010],end1[5010];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>begin1[i]>>end1[i];
    }
    sort(begin1,begin1+n,cmp);
    sort(end1,end1+n,cmp);
    max1=end1[0]-begin1[0];
    for(int i=0;i<n-1;i++)
    {
        if(end1[i]>=begin1[i+1])
        {
            begin1[i+1]=begin1[i];
        }
        max1=max(max1,end1[i+1]-begin1[i+1]);
        if(begin1[i+1]>end1[i])
        {
            max2=max(max2,begin1[i+1]-end1[i]);
        }
    }
    cout<<max1<<" "<<max2<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38984851/article/details/82502084
今日推荐