【USACO】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.
PROGRAM NAME: milk2
INPUT FORMAT
Line 1: | The single integer |
Lines 2..N+1: | Two non-negative integers less than 1000000, 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
题解:简单的区间合并。先把时间区间按照开始时间排序,然后开始合并区间。区间之间的情况有以下几种:
黑色表示早开始的区间,红色表示在它之后开始的区间。那么第1,3中区间都不用合并,第2中情况需要将区间合并成一个长区间。然后用一个vector存放合并后的区间,最后再遍历vector,找到长度最长的区间,即为最长的持续时间,而区间之间的最长时间即为最长闲置时间。代码如下:
/*ID:Moment1991
PROG:milk2
LANG:C++
Compiling...
Compile: OK Executing...
Test 1: TEST OK [0.003 secs, 3496 KB]
Test 2: TEST OK [0.000 secs, 3496 KB]
Test 3: TEST OK [0.000 secs, 3496 KB]
Test 4: TEST OK [0.008 secs, 3496 KB]
Test 5: TEST OK [0.003 secs, 3496 KB]
Test 6: TEST OK [0.005 secs, 3496 KB]
Test 7: TEST OK [0.008 secs, 3496 KB]
Test 8: TEST OK [0.019 secs, 3496 KB] All tests OK.
*/
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <fstream>
#include <vector>
using namespace std;
struct interval{
int start;
int end;
};
int cmp( const void *a ,const void *b)
{
return (*(interval *)a).start > (*(interval *)b).start ? : -;
}
int main(){
ifstream cin("milk2.in");
ofstream cout("milk2.out"); struct interval INTERVAL[];
int n; //读入输入
cin >> n;
for(int i = ;i < n;i ++){
cin >> INTERVAL[i].start>>INTERVAL[i].end;
}
//根据开始时间对区间排序
qsort(INTERVAL,n,sizeof(INTERVAL[]),cmp); int max_busy = ;
int max_free = ; struct interval temp; temp.start = INTERVAL[].start;
temp.end = INTERVAL[].end; //合并区间,将合并后的区间存放在vector AFTER_MERGE中
vector <interval> AFTER_MERGE;
for(int i = ;i < n;i++){
temp.start = INTERVAL[i].start;
temp.end = INTERVAL[i].end; while(i+<n && INTERVAL[i+].start <= temp.end){
if(INTERVAL[i+].end > temp.end)
temp.end = INTERVAL[i+].end;
i++;
}
AFTER_MERGE.push_back(temp);
} //遍历AFTER_MERGE,找到最长的区间以及区间之间最长的闲置时间
for(int i = ;i < AFTER_MERGE.size();i++)
{
if(AFTER_MERGE[i].end - AFTER_MERGE[i].start > max_busy)
max_busy= AFTER_MERGE[i].end - AFTER_MERGE[i].start; if(i+<AFTER_MERGE.size() && AFTER_MERGE[i+].start - AFTER_MERGE[i].end > max_free)
max_free = AFTER_MERGE[i+].start - AFTER_MERGE[i].end;
}
cout << max_busy<<" "<<max_free<<endl;
}
【USACO】Milking Cows的更多相关文章
- 【USACO】Strolling Cows
Strolling Cows 给定有 \(n\) 个点 \(n\) 条边的有向图,每个点的出度都为 \(1\),求图中的最大环. 显然入度为 \(0\) 的点不可能为最大环上的点,所以考虑删点. 然后 ...
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
- 【2186】Popular Cows(强连通分支及其缩点)
id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS Memory Limit: 65536 ...
- 1642: 【USACO】Payback(还债)
1642: [USACO]Payback(还债) 时间限制: 1 Sec 内存限制: 64 MB 提交: 190 解决: 95 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 &quo ...
- 【POJ3621】Sightseeing Cows 分数规划
[POJ3621]Sightseeing Cows 题意:在给定的一个图上寻找一个环路,使得总欢乐值(经过的点权值之和)/ 总时间(经过的边权值之和)最大. 题解:显然是分数规划,二分答案ans,将每 ...
- 【POJ2182】Lost Cows
[POJ2182]Lost Cows 题面 vjudge 题解 从后往前做 每扫到一个点\(i\)以及比前面小的有\(a[i]\)个数 就是查询当前的第\(a[i]+1\)小 然后查询完将这个数删掉 ...
- 1519: 【USACO】超级书架
1519: [USACO]超级书架 时间限制: 1 Sec 内存限制: 64 MB 提交: 1735 解决: 891 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 Farmer Jo ...
- Java实现【USACO】1.1.2 贪婪的礼物送礼者 Greedy Gift Givers
[USACO]1.1.2 贪婪的礼物送礼者 Greedy Gift Givers 题目描述 对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少(and vice versa for th ...
- 【USACO】Optimal Milking
题目链接 : [POJ]点击打开链接 [caioj]点击打开链接 算法 : 1:跑一遍弗洛伊德,求出点与点之间的最短路径 2:二分答案,二分”最大值最小“ 3.1:建边,将 ...
随机推荐
- MyEclipse构建WebService案例
Hi,大家好! 今天主要和大家分享,如何搭建一个Web服务,做Android开发,不可避免会涉及到客户端开发,我们怎么样来实现一个服务端,怎么样来实现一个客户端,并相互传递数据.就算调用别人的服务时, ...
- 【Merge K Sorted Lists】cpp
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- Angular 2 Quickstart
写一个Angular 2的应用最基本的步骤概括为三步:写root组件,启动它(Boostrap),写index.html. 一些关于命名空间的基本知识 把所有代码放入一个立即调用函数中,通过传入一个空 ...
- mac os 10.10上安装my eclipse显示virtual memory不足,解决方案
mac os 10.10上安装my eclipse显示virtual memory不足,安装失败. 自从把OS 升级到10.10 之后, 各种问题, 安装的时候向导提示提示我们说没有足够的虚拟内存, ...
- Codeforces Round #249 (Div. 2) D. Special Grid 枚举
题目链接: http://codeforces.com/contest/435/problem/D D. Special Grid time limit per test:4 secondsmemor ...
- 【转】Basic C# OOP Concept
This Article will explain a very simple way to understand the basic C# OOP Concept Download ShanuBas ...
- matrix_last_acm_1
password 123 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=96950#problem/A 题意:n个数初始ai,m次操作 ...
- CRC16校验
C++中的代码如下:传入字节(byte)数组引用和数组长度 unsigned short CTcpClient::Crc16(const char *pBuf, unsigned short nLen ...
- 报名|「OneAPM x DaoCloud」技术公开课:Docker性能监控!
如今,越来越多的公司开始 Docker 了,「三分之二的公司在尝试了 Docker 后最终使用了它」,也就是说 Docker 的转化率达到了 67%,同时转化时长也控制在 60 天内. 既然 Dock ...
- CSS 的overflow:hidden 属性详细解释
overflow:hidden这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出, 而对于清除浮动这个含义不是很了解.一提到清除浮动,我们就会想到另外一个CSS样式 ...