【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 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

懒得翻译直接搬救兵 http://www.nocow.cn/index.php/Translate:USACO/milk2

这道题才是错的最多的 因为各种各样的考虑不周到错了很多次 时间比较久了我记不清WA的地方了。。。直接上代码吧反正就是模拟

/*
ID: Alpha Augur
PROG: milk2
LANG: C++
*/
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;

const int maxn = 5010;
struct Ti{
    int st, en;
}ti[maxn];

bool cmp(Ti a, Ti b);

int main(){
    freopen("milk2.in", "r", stdin);
    freopen("milk2.out", "w", stdout);
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i){
        scanf("%d%d", &ti[i].st, &ti[i].en);
    }
    sort(ti, ti+n, cmp);

    int cnt = ti[0].en - ti[0].st;
    int ma = cnt;
    int mi = 0;
    for(int i = 1; i < n; ++i){
        if(ti[i].st <= ti[i-1].en){
            if(ti[i].en > ti[i-1].en){
                cnt += ti[i].en - ti[i-1].en;
                ma = max(ma, cnt);
            }
            ti[i].en = max(ti[i].en, ti[i-1].en);
        }
        else{
            cnt = ti[i].en - ti[i].st;
            ti[i].en = max(ti[i].en, ti[i-1].en);
            mi = max(ti[i].st - ti[i-1].en, mi);
        }
    }

    printf("%d %d\n", ma, mi);
    return 0;
}

bool cmp(Ti a, Ti b){
    if(a.st == b.st){
        return a.en < b.en;
    }
    return a.st < b.st;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-05 06:05:27

【USACO1.2.1】Milking Cows的相关文章

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

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

【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

【USACO1.2.3】Name That Number(字符串处理)

Name That Number Among the large Wisconsin cattle ranchers, it is customary to brand cows with serial numbers to please the Accounting Department. The cow hands don't appreciate the advantage of this filing system, though, and wish to call the member

[USACO1.2]挤牛奶Milking Cows

题目描述 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒.第二个农民在700秒开始,在 1200秒结束.第三个农民在1500秒开始2100秒结束.期间最长的至少有一个农民在挤奶的连续时间为900秒(从300秒到1200秒),而最长的无人挤奶的连续时间(从挤奶开始一直到挤奶结束)为300秒(从1200秒到1500秒). 你的任务是编一个程序,读入一个有N个农民(1 <= N <= 5000)挤N头牛的工作时间列表,计算以下两

【洛谷P2889】Milking Time

很容易想到以结束时间加上R从小到大排序 之后怎样呢? 我们按层考虑,f[i]表示前i个时间段嫩得到的最大价值 每次枚举其之前的状态,如果其ed<当前i的st,那么取max即可 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N=1010; 6 int n,m,r,f[N]; 7 struct fx{ 8 int st,ed

【USACO1.1.2】Greedy Gift Givers(map)

Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to any or all of the other friends. Likewise, each friend might or might not receiv

【USACO1.1.1】Your Ride Is Here

Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect loyal supporters from here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do, however

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

https://www.luogu.org/problem/show?pid=1204 题目描述 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒.第二个农民在700秒开始,在 1200秒结束.第三个农民在1500秒开始2100秒结束.期间最长的至少有一个农民在挤奶的连续时间为900秒(从300秒到1200秒),而最长的无人挤奶的连续时间(从挤奶开始一直到挤奶结束)为300秒(从1200秒到1500秒). 你的任务是编一个程序

【USACO1.1.3】Friday the Thirteenth

Friday the Thirteenth Is Friday the 13th really an unusual event? That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13t