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

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...