Usaco Training刷怪旅 第二层第一题:Milking Cows
创始人
2024-04-27 20:59:52
0

usaco training

关注我持续更新usaco training内容

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

 

这题连大模拟都算不上,就是一个小小的模拟而已

 

翻译一下:给出n个奶牛挤奶开始时间以及结束时间,并求出最长连续挤奶时间和最长不挤奶时间

最长连续挤奶时间有两种情况:

(1)就是一个奶牛的挤奶时间

(2)第二头奶牛挤奶开始时间比第一头奶牛挤奶结束时间还要早,也就是说两头奶牛的挤奶时间相交,所以就把两段时间相加

我们把一头奶牛挤奶时间抽象成一个长方形

第一种:

很明显就能看出最长连续挤奶时间是第二头,闲置时间也就是空白那里

 那么这种情况程序怎么写呢

我们用两个变量代表要求的两个时间:max_和min_

还有两个变量表示max_的时间的起始和结束时间:s和e

初始化:首先先将s定义为第一头奶牛的开始时间,将e定义为第一头奶牛的结束时间

然后对后面的奶牛依次考虑,如果这头奶牛的挤奶时间比现在的e和s的间隔长,就把e和s更新

然后求max_

因为共有两种情况,这里就先不给代码了

第二种:

这样的图形很明显可以看到最长连续挤奶时间是两个时间加在一起,间隔时间为0

两个方块相交,其实就可以抽象成这样:

 

新长方形的开始为原第一个的开始,新长方形的结束为第二个的结束 

把这两个思想一结合就成为了ac代码

/*
ID:
TASK:milk2
LANG:C++
*/
# include 
# include 
# include 
using namespace std;
# define int long long
# define N 5005
int n,v[N],maxn=0;
struct node{int st,en;
}cow[N];
bool cmp(node a,node b){if (a.st==b.st){return a.en

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...