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

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
AWSECS:哪种网络模式具有... 使用AWS ECS中的awsvpc网络模式来获得最佳性能。awsvpc网络模式允许ECS任务直接在V...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...