zoukankan      html  css  js  c++  java
  • 【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 ? 1 : -1;
    }
    int main(){
        ifstream cin("milk2.in");
        ofstream cout("milk2.out");
    
        struct interval INTERVAL[5050];
        int n;
    
        //读入输入
        cin >> n;
        for(int i = 0;i < n;i ++){
            cin >> INTERVAL[i].start>>INTERVAL[i].end;
        }
        //根据开始时间对区间排序
        qsort(INTERVAL,n,sizeof(INTERVAL[0]),cmp);
        
        int max_busy = 0;
        int max_free = 0;
    
        struct interval temp;    temp.start = INTERVAL[0].start;
        temp.end = INTERVAL[0].end;
    
        //合并区间,将合并后的区间存放在vector AFTER_MERGE中
        vector <interval> AFTER_MERGE;
        for(int i = 0;i < n;i++){
            temp.start = INTERVAL[i].start;
            temp.end = INTERVAL[i].end;
    
            while(i+1<n && INTERVAL[i+1].start <= temp.end){
                if(INTERVAL[i+1].end > temp.end)
                    temp.end =  INTERVAL[i+1].end;
                i++;
            }
            AFTER_MERGE.push_back(temp);
        }
    
        //遍历AFTER_MERGE,找到最长的区间以及区间之间最长的闲置时间
        for(int i = 0;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+1<AFTER_MERGE.size() && AFTER_MERGE[i+1].start - AFTER_MERGE[i].end > max_free)
                max_free = AFTER_MERGE[i+1].start - AFTER_MERGE[i].end;
        }
        cout << max_busy<<" "<<max_free<<endl;
    }
  • 相关阅读:
    让自己的网站或博客被百度收录的小技巧
    Linux cp一个文件夹时提示cp: omitting directory `test/'
    svn checkout 提示“由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。”解决方法
    Linux iptables开放特定端口
    CentOS7使用iptables防火墙开放端口
    Linux平台的SVN服务器的配置及搭建
    官网svn提交到代码库,但是不能同步到web目录
    【LINUX】SVN 代码提交之后。同步到web目录下
    React Native For Android 架构初探
    腾讯QQ会员技术团队:以手机QQ会员H5加速为例,为你揭开sonic技术内幕
  • 原文地址:https://www.cnblogs.com/sunshineatnoon/p/3727444.html
Copyright © 2011-2022 走看看