当前位置: 首页 > news >正文

USACO Training 1.3 Milking Cows

题干

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

思路

我们读取时间列表,按开始时间排序,然后遍历列表一次,合并重叠时间。然后我们遍历列表,观察长时间的挤奶期和长时间的非挤奶期。

另一种方法是只保留一个大小为一百万的数组并标记时间。在一个不错的快速处理器上,这可能已经足够快了,但是我们上面的算法即使在慢速处理器上也能工作,而且编写起来并不难。

解题

/*
ID: choiyin1
LANG: C++
PROG: milk2
*/

#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;
} 

相关文章:

  • < Linux > 进度条小程序 + git三板斧
  • 惊险的十天
  • 【数据结构初阶】堆堆的实现堆排序TOP-K
  • 自动驾驶技术综述1:自动驾驶算法软件架构介绍
  • PySpark数据分析基础:pyspark.mllib.regression机器学习回归核心类详解(一)+代码详解
  • 线程池-手写线程池
  • TCP延申
  • C进阶——指针详解
  • Serverless 架构下的 AI 应用开发:入门、实战与性能优化
  • 网络整体框架介绍
  • 利用MCU实现制作一台蓝牙控制小车方法
  • pythonGUI(二)基本元素之一
  • 猿创征文|【云原生】学习云原生经验分享
  • ESP8266-Arduino编程实例-HP303B数字气压传感器驱动
  • 【目标检测】雷达目标CFAR检测算法
  • 【comparator, comparable】小总结
  • 5、React组件事件详解
  • conda常用的命令
  • docker-consul
  • ES6 ...操作符
  • export和import的用法总结
  • IP路由与转发
  • Java IO学习笔记一
  • Java 内存分配及垃圾回收机制初探
  • Mithril.js 入门介绍
  • node入门
  • ReactNative开发常用的三方模块
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • Spring Boot MyBatis配置多种数据库
  • TypeScript迭代器
  • 编写高质量JavaScript代码之并发
  • 不用申请服务号就可以开发微信支付/支付宝/QQ钱包支付!附:直接可用的代码+demo...
  • 一天一个设计模式之JS实现——适配器模式
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (ZT)一个美国文科博士的YardLife
  • (一)认识微服务
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (转)关于如何学好游戏3D引擎编程的一些经验
  • .net framework profiles /.net framework 配置
  • .net redis定时_一场由fork引发的超时,让我们重新探讨了Redis的抖动问题
  • .net 发送邮件
  • .NET/C# 避免调试器不小心提前计算本应延迟计算的值
  • .net分布式压力测试工具(Beetle.DT)
  • .Net中ListT 泛型转成DataTable、DataSet
  • @GetMapping和@RequestMapping的区别
  • @Transactional类内部访问失效原因详解
  • [04] Android逐帧动画(一)
  • [BPU部署教程] 教你搞定YOLOV5部署 (版本: 6.2)
  • [codevs 1288] 埃及分数 [IDdfs 迭代加深搜索 ]
  • [CSS]CSS 字体属性
  • [datastore@cyberfear.com].Elbie、[thekeyishere@cock.li].Elbie勒索病毒数据怎么处理|数据解密恢复