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. android webview开启html5支持

    最近做的一个小项目需要用到webview.虽然只是一个简单的网页,但是由于以前用的都只是显示本地文件,没有显示网页文件.现在需要显示网页文件,发现许多网站的webapp做的挺不错的,无论是显示还是用户 ...

  2. Python 读写excel数据

    读取excel 文件的数据 import csv with open('D:/mystuff/11.csv','r') as f: reader = csv.reader(f) for row in ...

  3. chm文件右边部分查看不了

    右键属性-> 下面好像有句话是说该文件来自网络为保护您的计算机什么什么的,, 然后你解除锁定就能用了 版权声明:本文为博主原创文章,未经博主允许不得转载.

  4. EF 更新条目时出错。有关详细信息,请参见内部异常。

    现象:使用EF新增记录时,一直报上述异常,网上说是值为空.主键外键未设等原因导致,但是改正这些情况下问题依然 解决过程:异常中有一句(请参见内部异常),一直都没有当回事,后来实在没办法就静下心来看了看 ...

  5. 第k短路

    poj 2449 模板题  A*+spfa #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  6. 【bzoj1013】[JSOI2008]球形空间产生器sphere

    1013: [JSOI2008]球形空间产生器sphere Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4530  Solved: 2364[Subm ...

  7. python抓取汇率

    # -*- coding: utf-8 -*- """ 获取实时汇率 Created on Fri Oct 18 13:11:40 2013 @author: alala ...

  8. Isomorphic JavaScript: The Future of Web Apps

    Isomorphic JavaScript: The Future of Web Apps At Airbnb, we’ve learned a lot over the past few years ...

  9. redis cluster安装部署(测试环境)

    redis 应用于web前端,做缓存和数据存取的速度是挺可观的,最近看了一些资料,手痒了,就弄了一个测试环境,两台方案,试用一下. ##Redis 集群部署## 一,方案调研: 参考博客: http: ...

  10. Asp.Net中用JS中操作cookie的方法

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="cookies.aspx.cs& ...