1.2.1---Milking Cows

来源:互联网 发布:培训机构网站asp源码 编辑:程序博客网 时间:2024/06/06 12:51

Milking Cows

Three farmers riseat 5 am each morning and head for the barn to milk three cows. The first farmerbegins milking his cow at time 300 (measured in seconds after 5 am) and ends attime 1000. The second farmer begins at time 700 and ends at time 1200. Thethird farmer begins at time 1500 and ends at time 2100. The longest continuoustime during which at least one farmer was milking a cow was 900 seconds (from300 to 1200). The longest time no milking was done, between the beginning andthe ending of all milking, was 300 seconds (1500 minus 1200).

Your job is towrite 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 (filemilk2.in)

3

300 1000

700 1200

1500 2100

 

OUTPUT FORMAT

A single line withtwo integers that represent the longest continuous time of milking and thelongest idle time.

SAMPLE OUTPUT(file milk2.out)

900 300

本身题目并不难,但是由于采用map形式存储数据所以要注意起始工作时间相同的情况(哥哥脑残一开始忘了)。

 

/*

ID: ******

PROG: milk2

LANG: C++

*/

#include <iostream>

#include <fstream>

#include <queue>

#include <map>

using namespace std;

int main()

{

         map<int,int>map1;

         priority_queue<int,vector<int>,greater<int> >q;//小顶堆

         ofstreamfout ("milk2.out");

         ifstreamfin ("milk2.in");

         intn,i,temp2,start,end;

         intmax1,max2;

         start=end=0;

         fin>>n;

         for(i=0;i<n;i++)//数据输入

         {

                   fin>>start>>temp2;

                   if(temp2>map1[start])

                   map1[start]=temp2;

                   q.push(start);

         }

         start=q.top();//赋初值

         q.pop();

         max1=map1[start]-start;

         max2=0;

         end=map1[start];

         while(q.size())

         {

                   temp2=q.top();

                   q.pop();

                   if(temp2<=end&&map1[temp2]>end)//无间隔时直接累加

                   {

                           

                            end=map1[temp2];

                            if((end-start)>max1)

                            max1=(end-start);

                   }

                   else

                   {

                            if(temp2>end)//存在工作间隔的情况

                            {

                                    

                                     if((temp2-end)>max2)

                                                        max2=(temp2-end);

                                     start=temp2;

                                     end=map1[temp2];

                                    

                                     if((end-start)>max1)

                                               max1=(end-start);

                                    

                            }                

                   }

                                    

         }

         fout<<max1<<""<<max2<<endl;

         return0;

}

原创粉丝点击