USACO milking cows 挤牛奶

Question

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

题目描述

三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒。第二个农民在700秒开始,在 1200秒结束。第三个农民在1500秒开始2100秒结束。期间最长的至少有一个农民在挤奶的连续时间为900秒(从300秒到1200秒),而最长的无人挤奶的连续时间(从挤奶开始一直到挤奶结束)为300秒(从1200秒到1500秒)。

你的任务是编一个程序,读入一个有N个农民(1 <= N <= 5000)挤N头牛的工作时间列表,计算以下两点(均以秒为单位):

最长至少有一人在挤奶的时间段。

最长的无人挤奶的时间段。(从有人挤奶开始算起)

输入格式:

Line 1:一个整数N。
Lines 2~Line N+1:每行两个小于1000000的非负整数,表示一个农民的开始时刻与结束时刻。

输出格式:

一行,两个整数,即题目所要求的两个答案。

输入样例1:

3
300 1000
700 1200
1500 2100

输出样例1:

900 300

解题思路:

我拿输入样例1举例子:在这里插入图片描述
先要排序,begin和end分别排,并且保留end[0]和begin[0]分别等于最小的end和begin以免发生错误。
然后如果begin[i]比end[i-1]小,代表从begin[i-1]到end[i]的范围内都是有人的。
如图即为:
300到1000有人
700到1200有人
因为1000>700
所以300到1200都有人。

之后所做的操作就是数数了
先把有人的时间段都数出来,挑最大值,也就是
end[i]-begin[i]
也就是1000-300=700,1200-300=900,2100-1500=600

然后再数没有人的时间段
也就是后一个开始到前一个结束的时间差。
300-300=0
300-1000=-700
1500-1200=300
如果最大的时间差也比0小,即所有时间有人,且有时有两个人时,没有人的最长时间即为0

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int cmp(int a,int b){
	return a<=b;
}
int main(){
//	freopen("milk2.in","r",stdin);
//	freopen("milk2.out","w",stdout);
	int n;
	cin>>n;
	int begin[6000];
	int end[6000];
	begin[0]=0;
	end[0]=0;
	for(int i=1;i<=n;i++){
		scanf("%d %d",&begin[i],&end[i]);
	}
	sort(begin,begin+n+1,cmp);
	sort(end,end+n+1,cmp);
	begin[0]=begin[1];
	end[0]=end[1];
	sort(begin,begin+n+1,cmp);
	sort(end,end+n+1,cmp);
	int have[6000];
	int havent[6000];
	for(int i=1;i<=n;i++){
		have[i]=0;
	}
	for(int i=1;i<=n;i++){
		havent[i]=0;
	}
	for(int i=1;i<=n;i++){
		if(end[i-1]>=begin[i]){
			begin[i]=begin[i-1];
		}
		have[i]=end[i]-begin[i];
	} 
	for(int i=1;i<=n;i++){
		havent[i]=begin[i]-end[i-1];
	}
	sort(have,have+n+1,cmp);
	sort(havent,havent+n+1,cmp);
	if(havent[n]<0){
		havent[n]=0;
	}
	cout<<have[n]<<" "<<havent[n]<<endl;
	return 0;
} 

原创
写得好累啊


附:
usaco地址:usaco.org
做题地址:train.usaco.org
注册很烦哦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值