【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:建边,将 ...
随机推荐
- 【转】oracle查询不到表的问题
ORACLE的问题解决:Ora-00942:表或视图不存在 分类: 数据库2006-07-05 00:15 10793人阅读 评论(4) 收藏 举报 oraclesqlmanager 由powerde ...
- python 数据结构-列表
列表常用方法汇总: #定义列表li li=[12.23,456,88,9] a=[1,2,3] #添加元素到列表结尾 li.append(360) #追加列表元素extend(L) li.extend ...
- zip&rar格式压缩包的解压缩
实际工作中例子: 首先需要引入两个jar包: 注意:支持压缩软件4.20及以上版本 (1)压缩包的解压: (2)压缩包的压缩打包 zip格式:
- Leetcode#147 Insertion Sort List
原题地址 心得:有关链表的题目,多用中间变量,代码写得清晰一点,适当注释 代码: ListNode *insertionSortList(ListNode *head) { if (!head) re ...
- HDOJ 1518 Square
Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- Unity3D研究院之LZMA压缩文件与解压文件
原地址:http://www.xuanyusong.com/archives/3095 前两天有朋友告诉我Unity的Assetbundle是LZMA压缩的,刚好今天有时间那么就研究研究LZMA.它是 ...
- HDU1724 Ellipse(数值积分)
补一下一些小盲区,譬如simpson这种数值积分的方法虽然一直知道,但是从未实现过,做一道例题存一个模板. #pragma warning(disable:4996) #include<iost ...
- [读]剑指offer
研二的开始找工作了,首先祝愿他们都能够找到自己满意的工作.看着他们的身影,自问明年自己这个时候是否可以从容面对呢?心虚不已,赶紧从老严那儿讨来一本<剑指offer>.在此顺便将自己做题所想 ...
- C#--判断当前是否是移动设备和设备的型号
--如果是移动设备是true var ismobile = System.Web.HttpContext.Current.Request.Browser.IsMobileDevice; --设备型号( ...
- ***iOS开发中@selector的理解与应用
@selector 是什么? 1一种类型 SEL2代表你要发送的消息(方法), 跟字符串有点像, 也可以互转.: NSSelectorFromString() / NSSelectorFromStri ...