usaco1.2.1 Milking Cows

本文介绍了一个算法问题,涉及计算多个农民挤奶时间段内的最长连续挤奶时间和最长空闲时间。通过将挤奶时间段视为浮动的非重叠区间,并对其进行排序和遍历,有效地解决了这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 原题 

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

二. 分析

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


三. 代码

USER: Qi Shen [maxkibb3]
TASK: milk2
LANG: C++

Compiling...
Compile: OK

Executing...
   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 automated congratulations.


AC代码:
/*
ID:maxkibb3
PROG:milk2
LANG: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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值