usaco1.2.1 Milking Cows

来源:互联网 发布:舒乐安定 淘宝 编辑:程序博客网 时间:2024/06/08 19:40

一. 原题 

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, NLines 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)

3300 1000700 12001500 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

二. 分析

漂浮法。想象区间像若干易碎的木条,一开始都沉在水底,依次向上漂浮。上浮过程中若与之前的木条有重合之处,就只继续上浮没有重合的部分。最终得到若干互不重叠的区间。题目最终要求最长挤奶时间和最长休息时间,由于上述方法得到的区间互不重合,对区间按起点大小排序之后遍历一遍就行了。


三. 代码

USER: Qi Shen [maxkibb3]TASK: milk2LANG: C++Compiling...Compile: OKExecuting...   Test 1: TEST OK [0.000 secs, 4276 KB]   Test 2: TEST OK [0.000 secs, 4276 KB]   Test 3: TEST OK [0.000 secs, 4276 KB]   Test 4: TEST OK [0.000 secs, 4276 KB]   Test 5: TEST OK [0.000 secs, 4276 KB]   Test 6: TEST OK [0.000 secs, 4276 KB]   Test 7: TEST OK [0.000 secs, 4276 KB]   Test 8: TEST OK [0.054 secs, 4276 KB]All tests OK.

YOUR PROGRAM ('milk2') WORKED FIRST TIME! That's fantastic-- and a rare thing. Please accept these special automatedcongratulations.


AC代码:
/*ID:maxkibb3PROG:milk2LANG:C++*/#include<cstdio>#include<algorithm>using namespace std;struct seg {    int s, e;    bool operator < (const seg &obj) const {        return s < obj.s;    }};const int MAX = 5005;int n, cnt;long long ans1, ans2;seg a[MAX], ans[MAX];void float_up(int idx, int _s, int _e) {    if(idx == -1) {        seg new_ele;        new_ele.s = _s, new_ele.e = _e;        ans[cnt++] = new_ele;    }    else {        if(_s < a[idx].s) {            float_up(idx - 1, _s, min(_e, a[idx].s));        }        if(_e > a[idx].e) {            float_up(idx - 1, max(_s, a[idx].e), _e);        }    }}int main() {    freopen("milk2.in", "r", stdin);    freopen("milk2.out", "w", stdout);    scanf("%d", &n);    for(int i = 0; i < n; i++) {        scanf("%d%d", &a[i].s, &a[i].e);        float_up(i - 1, a[i].s, a[i].e);    }    sort(ans, ans + cnt);    long long pre_len = 0;    for(int i = 0; i < cnt; i++) {        if(i == 0) {            pre_len = ans[i].e - ans[i].s;            if(pre_len > ans1) {                ans1 = pre_len;            }        }        else {            if(ans[i].s == ans[i - 1].e) {                pre_len += ans[i].e - ans[i].s;                if(pre_len > ans1) {                    ans1 = pre_len;                }            }            else {                pre_len = ans[i].e - ans[i].s;                if(pre_len > ans1) {                    ans1 = pre_len;                }                if(ans[i].s - ans[i - 1].e > ans2) {                    ans2 = ans[i].s - ans[i - 1].e;                }            }        }    }    printf("%lld %lld\n", ans1, ans2);    return 0;}


0 0
原创粉丝点击