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 at time 1200. The third farmer begins at time 1500 and ends at time 2100. The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).

Your job is to write a program that will examine a list of beginning and ending times for N (1 <= N <= 5000) farmers milking N cows and compute (in seconds):

  • The longest time interval at least one cow was milked.
  • The longest time interval (after milking starts) during which no cows were being milked.

PROGRAM NAME: milk2

INPUT FORMAT

Line 1: The single integer
Lines 2..N+1: Two non-negative integers less than 1,000,000, respectively the starting and ending time in seconds after 0500

SAMPLE INPUT (file milk2.in)

3
300 1000
700 1200
1500 2100

OUTPUT FORMAT

A single line with two integers that represent the longest continuous time of milking and the longest idle time.

SAMPLE OUTPUT (file milk2.out)

900 300

题解:应该算是道超级简单的题 晚上失眠到五点半 索性不睡了。迷迷糊糊码下这道题 竟然一遍就过了
/*
ID: cxq_xia1
PROG: milk2
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn=5005;
int N;
struct node
{
    int begins,ends;
    friend bool operator < (node a,node b)
    {
        return a.begins > b.begins;
    }

};
node farmers[maxn];
int main()
{
    int ansMilk,ansNoMilk;
    node tmp1,MilkTime,NoMilkTime;
    freopen("milk2.in","r",stdin);
    freopen("milk2.out","w",stdout);
    while(cin >> N)
    {
        priority_queue<node> q;
        for(int i=0;i<N;i++)
        {
            cin >> farmers[i].begins >> farmers[i].ends;
            q.push(farmers[i]);
        }
        tmp1=q.top();
        q.pop();
        MilkTime=tmp1;
        ansMilk=MilkTime.ends-MilkTime.begins;
        ansNoMilk=0;
        while(!q.empty())
        {
            tmp1=q.top();
            q.pop();
            if(tmp1.begins>MilkTime.ends)
            {
                NoMilkTime.begins=MilkTime.ends;
                NoMilkTime.ends=tmp1.begins;
                if(ansNoMilk<(tmp1.begins-MilkTime.ends))
                    ansNoMilk=NoMilkTime.ends-NoMilkTime.begins;

                MilkTime=tmp1;
                if(ansMilk<MilkTime.ends-MilkTime.begins)
                        ansMilk=MilkTime.ends-MilkTime.begins;
            }
            else
            {
                if(tmp1.ends>MilkTime.ends)
                {
                    MilkTime.ends=tmp1.ends;
                    if(ansMilk<MilkTime.ends-MilkTime.begins)
                        ansMilk=MilkTime.ends-MilkTime.begins;
                }
            }
        }
        cout <<ansMilk<<" "<<ansNoMilk<<endl;
    }
    return 0;
}

  

时间: 2024-08-06 11:52:45

USACO 1.1 Milking Cows的相关文章

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 s1.2.Milking Cows(求最长连续时间和最长间断时间)

题意:输入多个时间段,表示喂牛的时间,问喂牛的最长的持续时间和不喂牛的最长的时间. key:注意输入的时间没有先后顺序.有两种方法.一是对时间段进行排序,比较每段时间的的末尾就行了,记得求得最大连续时间的时候要更新最后的时间,更新最大间断时间的时候要更新最前面的时间.方法二,用数组标记喂牛时间,,for一遍大概就可以找到这两个时间了.下面的是方法一. /* TASK:milk2 LANG:C++ ID:huibaochen */ #include <iostream> #include <

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 Milking Cows(模拟)

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

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;