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的更多相关文章

  1. 【USACO】Strolling Cows

    Strolling Cows 给定有 \(n\) 个点 \(n\) 条边的有向图,每个点的出度都为 \(1\),求图中的最大环. 显然入度为 \(0\) 的点不可能为最大环上的点,所以考虑删点. 然后 ...

  2. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  3. 【2186】Popular Cows(强连通分支及其缩点)

    id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS   Memory Limit: 65536 ...

  4. 1642: 【USACO】Payback(还债)

    1642: [USACO]Payback(还债) 时间限制: 1 Sec 内存限制: 64 MB 提交: 190 解决: 95 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 &quo ...

  5. 【POJ3621】Sightseeing Cows 分数规划

    [POJ3621]Sightseeing Cows 题意:在给定的一个图上寻找一个环路,使得总欢乐值(经过的点权值之和)/ 总时间(经过的边权值之和)最大. 题解:显然是分数规划,二分答案ans,将每 ...

  6. 【POJ2182】Lost Cows

    [POJ2182]Lost Cows 题面 vjudge 题解 从后往前做 每扫到一个点\(i\)以及比前面小的有\(a[i]\)个数 就是查询当前的第\(a[i]+1\)小 然后查询完将这个数删掉 ...

  7. 1519: 【USACO】超级书架

    1519: [USACO]超级书架 时间限制: 1 Sec 内存限制: 64 MB 提交: 1735 解决: 891 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 Farmer Jo ...

  8. Java实现【USACO】1.1.2 贪婪的礼物送礼者 Greedy Gift Givers

    [USACO]1.1.2 贪婪的礼物送礼者 Greedy Gift Givers 题目描述 对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少(and vice versa for th ...

  9. 【USACO】Optimal Milking

    题目链接 :        [POJ]点击打开链接        [caioj]点击打开链接 算法 : 1:跑一遍弗洛伊德,求出点与点之间的最短路径 2:二分答案,二分”最大值最小“ 3.1:建边,将 ...

随机推荐

  1. 查看python selenium的api

    打开命令行工具,doc中输入: python -m pydoc -p 然后在浏览器中访问http://localhost:4567/,此时应该可以看到python中所有的Modules 按ctrl+f ...

  2. cookie和session的代码实现

    cookie和session的代码实现 1.设置cookie 今天笔试题考的是cookie的设置,我竟然选了request也可以设置cookie,我的天呀. 我们来看如何在response设置吧 pu ...

  3. poj 3264 Balanced Lineup 区间极值RMQ

    题目链接:http://poj.org/problem?id=3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) alw ...

  4. 【POJ】【3680】Intervals

    网络流/费用流 引用下题解: lyd: 首先把区间端点离散化,设原来的数值i离散化后的标号是c[i].这样离散化之后,整个数轴被分成了一段段小区间. 1.建立S和T,从S到离散化后的第一个点连容量K, ...

  5. Dynamic Programming - Part2

    实现如下: public static void main(String[] args) { String squence1 = "ABCBDAB"; String squence ...

  6. vector内存分配

    vector,map 这些容器还是在堆上分配的内存,在析构时是释放空间 vector在提高性能可以先reserve在push_back() reserve:决定capacity,但没有真正的分配内存, ...

  7. TestDriven.Net

    转: http://www.cnblogs.com/AlexLiu/archive/2008/12/01/1345002.html

  8. Sqli-labs less 19

    Less-19 从源代码中我们可以看到我们获取到的是HTTP_REFERER 那和less18是基本一致的,我们从referer进行修改. 还是像less18一样,我们只给出一个示例 将referer ...

  9. 通过登入IP记录Linux所有用户登录所操作的日志

    通过登入IP记录Linux所有用户登录所操作的日志 对于Linux用户操作记录一般通过命令history来查看历史记录,但是如果在由于误操作而删除了重要的数据的情况下,history命令就不会有什么作 ...

  10. KMP模板,最小循环节

    (可以转载,但请注明出处!) 下面是有关学习KMP的参考网站 http://blog.csdn.net/yaochunnian/article/details/7059486 http://blog. ...