【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:建边,将 ...
随机推荐
- 15、android 用toast实现简单的进度显示
if(mtoast!=null) { mtoast.setText(progress); } else { mtoast=Toast.makeText(getApplicationContext(), ...
- Window.document对象(1)
1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunme ...
- 剑指offer--3题
题目:输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值.要求时间复杂度为O(n). 例如输入的数组为1, -2, 3, ...
- [工作积累] error: bad class file magic (cafebabe) or version (0033.0000)
Update Android SDK build tool to latest can solve my problem.
- CSS Hack大全-可区分出IE6-IE10、FireFox、Chrome、Opera
今天把一些常用的CSS Hack整理了一下,包括常用的IE hack以及火狐.Chrome.Opera浏览器的Hack,并把这些CSS Hack综合的一起,写了一个小的浏览器测试器.如图所示: 下面就 ...
- C#创建UTF8无BOM文本文件
In order to omit the byte order mark (BOM), your stream must use a custom instance of UTF8Encoding i ...
- poj 2723
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7295 Accepted: 2778 Des ...
- 动态调用webservice 接口
1.url:http://localhost:8002/名称.asmx(asmx结尾) 2.需要引用的命名空间:System.Web.Services 3.调用代码: public class Dyn ...
- (1)在sina app engine 上建个人博客
为啥想起来搞这一块呢? 有个哥们在新浪云上做了一个博客,有个师兄也做了这东西,我看挺炫酷的,也想做一个出来,把我在吉他,技术,摇滚,骑行,摄影方面的东西放到上面,也算是个个人简历,给人看也好看,比微博 ...
- strrchr函数
C语言函数 函数简介 函数名称: strrchr 函数原型:char *strrchr(char *str, char c); 所属库: string.h 函数功能:查找一个字符c在另一个字符串str ...