《3311: Milking Cows》

本来是一个水题,贪心排下序然后区间覆盖就行了。

但是这里有个坑:那就是时间要从开始挤牛奶才算。

也就是说

1

100 200

答案是100 0.

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 5e3+5;
const int M = 1e3+5;
const LL Mod = 1e9 + 7;
#define pi acos(-1)
#define INF 1e9
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}
        return x * f;
    }
}
using namespace FASTIO;

struct Node{int L,r;}p[N];
bool cmp(Node a,Node b)
{
    if(a.L == b.L) return a.r < b.r;
    else return a.L < b.L;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i = 1;i <= n;++i) p[i].L = read(),p[i].r = read();
        sort(p + 1,p + n + 1,cmp);
        int ans1 = p[1].r - p[1].L,ans2 = 0;
        int L = p[1].L,r = p[1].r;
        for(int i = 2;i <= n;++i)
        {
            if(p[i].L > r)
            {
                ans2 = max(ans2,p[i].L - r);
                L = p[i].L,r = p[i].r;
                ans1 = max(ans1,r - L);
            }
            else
            {
                r = max(r,p[i].r);
                ans1 = max(ans1,r - L);
            }
        }
        printf("%d %d
",ans1,ans2);
    }
    system("pause");
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zwjzwj/p/14082985.html