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-Chapter1-1.2】【模拟】Milking Cows

【题目描述】 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒。第二个农民在700秒开始,在 1200秒结束。第三个农民在1...

USACO 1.2 Milking Cows (归并排序+离散化)

#include #define DEBUG 0 #define TESTCASES 9 #define MAX 5000 int numOfFarmers; int time[MAX][2]; ...

USACO 1.2 Milking Cows

Milking Cows题意概述: 第一行输入一个整数N,表示有N个工作区间,接下依次输入每个区间的开始和结束时间,求从这里面最早的 一个 开始时间到最晚的结束时间这个时间区间内的最长...

USACO-Section1.2 Milking Cows [线段树]

2017-5-23 题目大意: 读入N个农民(1 <= N <= 5000)挤N头牛的工作时间列表, 求最长至少有一人在挤奶的时间段。 最长的无人挤奶的时间段。(从有人挤奶开始算起)...

USACO-Section 1.2 Milking Cows【贪心算法】

中文描述:三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒。第二个农民在700秒开始,在 1200秒结束。第三个农民在1500秒开...

USACO 1.2 Milking Cows

Milking Cows  方法1: 动态规划? y

USACO-Section 1.2 Milking Cows[数组模拟]

这道题的基本想法就是,利用数组模拟。开一个布尔型的标记数组,若标记值为true,表明这个时间是至少有一头牛是出于被喂奶的状态的。在输入每个农名的时间段的时候,可以用memset方法直接填充那个时间段。...

【模拟】洛谷 P1204 [USACO1.2]挤牛奶Milking Cows

题目描述三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒。第二个农民在700秒开始,在 1200秒结束。第三个农民在1500秒开始...

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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