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\n", maxwk, maxfe);
    }
    return 0;
}
时间: 2024-08-17 03:31:52

USACO milking cows的相关文章

USACO Milking Cows(模拟)

题目请点我 题意: 题意很简单,最开始的时候想要用优先队列存储时间,用map存储对应时间起点与终点.按时间轴顺序排列的思路是没错的,但是忽略了很重要的一点,一个时间起点可能会有多个对应的时间终点.改用结构体存储,定义cmp,得到时间轴.有两个变量表示总的时间起点和终点,注意起点与终点变换的条件,不断向后遍历就可以了. 代码实现: /* ID: eashion LANG: C++ TASK: milk2 */ #include <iostream> #include <cstdio>

USACO 1.1 Milking Cows

Milking Cows Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends a

Milking Cows 挤牛奶 USACO 排序 模拟

1005: 1.2.1 Milking Cows 挤牛奶 时间限制: 1 Sec  内存限制: 128 MB提交: 15  解决: 9[提交] [状态] [讨论版] [命题人:外部导入] 题目描述 1.2.1 Milking Cows 挤牛奶 (milk2.pas/c/cpp) 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒.第二个农民在700秒开始,在 1200秒结束.第三个农民在1500秒开始2100秒结束.期间最长的至

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

P1204 [USACO1.2]挤牛奶Milking Cows 474通过 1.4K提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 请各位帮忙看下程序 错误 谢… 求大神扫一眼,模拟算法 求帮看看哪儿错 帮忙看看为什么不过 帮我看看哪里错了,挤牛奶,… 帮我看看哪里错了 题目描述 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒.第二个农民在700秒开始,在 1200秒结束.第三个农民

【USACO1.2.1】Milking Cows

Milking Cows Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends a

USACO SECTION1 2.1 Milking Cows 简单贪心

题目链接: http://train.usaco.org/usacoprob2?a=p8taXWtZBpU&S=milk2 题目描述: 给出n组数, 求出最长连续有数区间与最长连续无数区间 解题思路: 简单贪心, 先排序, 然后如果有交集判断右端点,如果右端点大则延伸, 否则continue, 如果无交集更新两个ans. 代码: http://train.usaco.org/usacoprob2?a=p8taXWtZBpU&S=milk2 思考: 简单的贪心我的代码却WA了, 记住一切从简

USACO Section1.2 Milking Cows 解题报告

milk2解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] N个农民,每个农民从L[i]到R[i]时间给奶牛挤奶.问最长的一直有人挤奶的时间,和最长的没有人挤奶的时间.[数据范围] 1<=N

USACO 1.2 Milking Cows (枚举)

标记数组(哈希) 1e6的范围,开一个char数组全然能够,有人为1,无人为0,注意边界就可以.最后线性扫描就可以. 时间复杂度,应该是O(n),n为最后结束的时间. 缺点就是--比較慢 /* ID:twd30651 PROG:milk2 LANG:C++ */ #include<iostream> #include<fstream> #include<string.h> #include<stdlib.h> char a[1000001]; using n

【USACO】milking cows

{ ID: anniel11 PROG: milk2 LANG: PASCAL } Program milk2; Var ans1,ans2,i,j,n,sum,head,tail:longint; a:array[1..5000,1..2] of longint; map:array[1..100000] of integer; f:array[0..100000,1..2] of integer; Begin fillchar(map,sizeof(map),0); ans1:=0; ans