USACO Training 出锅
  • 板块学术版
  • 楼主刘嘉琦
  • 当前回复15
  • 已保存回复15
  • 发布时间2022/1/21 12:58
  • 上次更新2023/10/28 11:41:04
查看原帖
USACO Training 出锅
267481
刘嘉琦楼主2022/1/21 12:58

我在USACO Training上交这道题:

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.

NOTE:Milking from time 1 through 10, then from time 11 through 20 counts as two different time intervals.

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



评测机返回:

返回结果

提交代码:

#include <iostream>
#include <cstdio>
#include <algorithm> 
using namespace std;

/*
ID: 此处隐藏
PROG: milk2
LANG: C++                 
*/

int n, t[1000005] = {0}, d[1000005] = {0}, a, b;
int m[1000005] = {0}, k[1000005] = {0}, t1 = 0, t2 = 0;
int st = 1e9, ed = -1;

int main()
{
	freopen("milk2.in", "r", stdin);
	freopen("milk2.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a >> b;
        d[a + 1]++, d[b + 1]--;
        st = min(st, a + 1);
        ed = max(ed, b);
    }
    for (int i = st; i <= ed; i++) {
        t[i] = t[i - 1] + d[i];
        if (t[i] > 0)
            m[i] = m[i - 1] + 1;
        else
            k[i] = k[i - 1] + 1;
        t1 = max(t1, m[i]);
        t2 = max(t2, k[i]);
    }
    cout << t1 << ' ' << t2 << endl;
    return 0;
}

哪位大佬知道,我出了什么问题

2022/1/21 12:58
加载中...