USACO milking cows

  题意是给你一个工人挤奶的时间, 然后让你求出最长连续工作时间和最长连续不工作时间。。离散化后直接扫一遍即可:

/*
    ID: m1500293
    LANG: C++
    PROG: milk2
*/

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int n;
int x[2*5000 + 100];
int num;

struct Person
{
    int x, y;
    bool operator<(const Person&r) const
    {
        return x < r.x;
    }
};
Person per[5000+10];

bool judge(int x1, int x2)
{
    for(int i=0; i<n; i++)
    {
        if(per[i].x<=x1 && per[i].y>=x2)
            return true;
    }
    return false;
}

int main()
{
    freopen("milk2.in", "r", stdin);
    freopen("milk2.out", "w", stdout);
    while(scanf("%d", &n) == 1)
    {
        num = 0;
        for(int i=0; i<n; i++)
        {
            int x1, x2;
            scanf("%d%d", &x1, &x2);
            per[i].x=x1; per[i].y=x2;
            x[num++] = x1;
            x[num++] = x2;
        }
        sort(x, x+num);
        num = unique(x, x+num)-x;
        sort(per, per+n);
        int maxwk=0, maxfe=0;
        int tpwk=0, tpfe=0;
        for(int i=0; i<num-1; i++)
        {
            if(judge(x[i], x[i+1]))   //属于工作时间
            {
                tpwk += x[i+1] - x[i];
                maxfe = max(maxfe, tpfe);
                tpfe = 0;
            }
            else 
            {
                tpfe += x[i+1] - x[i];
                maxwk = max(maxwk, tpwk);
                tpwk = 0;
            }
        }
        maxfe = max(maxfe, tpfe);
        maxwk = max(maxwk, tpwk);
        printf("%d %d
", maxwk, maxfe);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xingxing1024/p/5055441.html