zoukankan      html  css  js  c++  java
  • Milking Cows

    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
    

     思路 :大意是要找最大的有奶牛挤奶的区间和没有奶牛要挤奶的时间区间;可以把所有区间按照起始时间排序,然后遍历,不断判断区间的包含关系,同时记录下最大的挤奶区间best_milked和不挤奶的区间大小best_n0_milked。区间之间的关系有以下三种情况:

    |___________________|  (start,end)

            |____________|     (start1,end1)

            |__________________|   (start2,end2)

                                                     |________________|   (start3,end3)

    第一种 :考察是否更新更大的best_milked = end -start ; start 、end 不用更新,继续向后考察下一个区间;

    第二种: (start2<=end && end2>=end) 考察是否更新更大的best_milked = end2 - start; start 不用更新,end 更新为end2,继续向下考察下一个区间;

    第三种: (start3>end)  更新best_milked为end-start和end3-start3和原来的值的最大值,同时可以更新best_no_milked为原来的值和start3-end之间的最大值,start更新为start3,end更新为end3,继续考察下一个区间;

    源代码如下:

    #include <stdio.h>
    #include <stdlib.h>
    struct node
    {
        int start;
        int end;
    };
    int cmp(const void *a ,const  void *b)//注意返回值当作是bool值,必须保证交换a,b后真假发生变化
    {
        if(((struct node *)a)->start -  ((struct node *)b)->start <0 ) return -1;
        else if(((struct node *)a)->start -  ((struct node *)b)->start > 0 ) return 1;
        else return 0;
    }
    int main () 
    {
        FILE *fin  = fopen ("milk2.in", "r");
        FILE *fout = fopen ("milk2.out", "w");
        int n;
        fscanf (fin, "%d", &n);    
        struct node *info;
        info=new struct node[n];
        int i=0;
        for(i=0;i<n;i++)
        {
            fscanf(fin,"%d%d",&info[i].start,&info[i].end);
        }
    
        qsort(info,n,sizeof(struct node),cmp);
        int start,end;
        start=info[0].start;
        end=info[0].end;
        int best_milked=end-start;
        int best_no_milked=0;
        int temp;
        for(i=1;i<n;i++)
        {
            if(info[i].end <=end)
            {
                temp= end - start;
                if(temp>best_milked) best_milked=temp;
            }
            else if(info[i].start<=end && info[i].end >=end)
            {
                temp= info[i].end - start;
                if(temp>best_milked) best_milked=temp;
                end = info[i].end;
            }
            else if(info[i].start>end)
            {
                if((info[i].end -info[i].start) > (end -start) )temp =info[i].end -info[i].start;
                else temp = end -start;
                if(temp>best_milked) best_milked=temp;            
                temp = info[i].start -end;
                if(temp>best_no_milked) best_no_milked =temp;
                start=info[i].start;
                end = info[i].end;
            }
        }
        fprintf (fout, "%d %d
    ",best_milked, best_no_milked);
        return 0;
    }
  • 相关阅读:
    (转载)C++ string中find() ,rfind() 等函数 用法总结及示例
    UVA 230 Borrowers (STL 行读入的处理 重载小于号)
    UVA 12100 打印队列(STL deque)
    uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)
    uva 1592 Database (STL)
    HDU 1087 Super Jumping! Jumping! Jumping!
    hdu 1176 免费馅饼
    HDU 1003 Max Sum
    转战HDU
    hust 1227 Join Together
  • 原文地址:https://www.cnblogs.com/easyFancy/p/3330266.html
Copyright © 2011-2022 走看看