USACO 1.2 Milking Cows

原创 2016年08月29日 07:35:43

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, N
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

比较简单的一道数据结构题,但对我来说还是有点难度的。。

关键在于区间标记。我一开始的做法是对于给出的区间【a,b】,标记a和b+1.理想很美好,现实很残酷,这种标记是错误的。为啥大家可以自己想一下。

(100 200

    201 300

那就只能a,b标记,这样又有一个问题,就是连续不放羊的天数会有错误。经过多次数据测试,我们知道了:如果从头到尾都是放羊,那么结果要-1.

/*
ID: myk5021
PROG: milk2
LANG: C++11
*/
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<climits>
using namespace std;
const int maxn=1000000+10;
int n,a[maxn],sum_a[maxn];
int main(void)
{
    freopen("milk2.in","r",stdin);
    freopen("milk2.out","w",stdout);
    int ans1=0,ans2=0,left,right,continue_0,continue_1,lmin=maxn,rmax=0;
    memset(a,0,sizeof(a));
    memset(sum_a,0,sizeof(sum_a));
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>left>>right;
        a[left]+=1;
        a[right]-=1;
        rmax=max(rmax,right);
        lmin=min(lmin,left);
    }
    sum_a[0]=a[0];
    for(int i=1;i<=rmax;i++)
        sum_a[i]=sum_a[i-1]+a[i];
    continue_1=continue_0=0;
    for(int i=lmin;i<=rmax;i++)
    {
        if(sum_a[i]==0)
        {
            continue_0++;
            continue_1=0;
        }
        else
        {
            continue_1++;
            continue_0=0;
        }
        ans1=max(ans1,continue_1);
        ans2=max(ans2,continue_0);
    }
    if(rmax-lmin==ans1)
    {
        ans2--;
    }
    printf("%d %d\n",ans1,ans2);
    return 0;
}


版权声明:转载请注明原博客链接

【USACO】Milking Cows

ProblemThree farmers rise at 5 am each morning and head for the barn to milk three cows. The first f...
  • LeongHouHeng
  • LeongHouHeng
  • 2016年08月12日 20:19
  • 314

USACO Milking Cows(模拟)

题目请点我 题意: 题意很简单,最开始的时候想要用优先队列存储时间,用map存储对应时间起点与终点。按时间轴顺序排列的思路是没错的,但是忽略了很重要的一点,一个时间起点可能会有多个对应的时间终点。...
  • EaShion1994
  • EaShion1994
  • 2015年07月14日 19:43
  • 358

usaco 1.2:Milking Cows

 描述三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300时刻(从5点开始计时,秒为单位)给他的牛挤奶,一直到1000时刻。第二个农民在700时刻开始,在 1200时刻结束。第三个农民...
  • gy_kw
  • gy_kw
  • 2010年08月26日 21:56
  • 200

usaco 1.2 Milking Cows

/* ID:Ting Yu PROG:milk2 LANG:C++ */ #include #include #include using namespace std; int N,T1,T2; in...
  • qq_36213111
  • qq_36213111
  • 2017年10月14日 19:07
  • 89

USACO 1.2 Milking Cows

Milking Cows  方法1: 动态规划? y
  • baidu_23081367
  • baidu_23081367
  • 2014年11月12日 12:20
  • 293

Milking Cows 挤牛奶

描述三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒。第二个农民在700秒开始,在 1200秒结束。第三个农民在1500秒开始21...
  • hyqsblog
  • hyqsblog
  • 2015年04月25日 22:03
  • 558

USACO 1.2 Milking Cows (枚举)

USCAO milk2 题解
  • wdkirchhoff
  • wdkirchhoff
  • 2014年11月19日 16:49
  • 952

(模拟)usaco 1.2 Milking Cows

其实我还是觉得是贪心。 给出N段挤牛奶的时间,球出最长至少一人挤奶时间和最长无人挤奶时间。 之前判断哪里出了点问题一直卡在test 7 ,看了下nocow,..>_ 哎,感觉还没回来,还要恢复啊...
  • cqlf__
  • cqlf__
  • 2012年02月23日 00:21
  • 798

USACO 1.2 Milking Cows(暴力枚举)

Three farmers rise at 5 am each morning and head for the barnto milk three cows. The first farmer be...
  • u013534690
  • u013534690
  • 2014年10月21日 22:46
  • 398

USACO Section 1.2 Milking Cows

/* ID: lucien23 PROG: milk2 LANG: C++ */ #include #include #include using namespace std; typedef s...
  • LucienDuan
  • LucienDuan
  • 2013年11月23日 15:30
  • 702
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:USACO 1.2 Milking Cows
举报原因:
原因补充:

(最多只允许输入30个字)