USACO milking cows

来源:互联网 发布:好看的网络剧霸道总裁 编辑:程序博客网 时间:2024/04/30 23:35

Description:

  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 1,000,000, respectively the starting and ending time in seconds after 0500

SAMPLE INPUT (file milk2.in) 

3300 1000700 12001500 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
solution:
    use a pair array to store all the segment,sort it by start time,and then we use an another pair array to decide how many segment exactly exist.First of all,let a[0] be valued by b[0],and we try another a[1],if a[1]'s start time is earlier than b[0]'s end time,then we update b[0] with a[1] by expand it's time range,else we should let a[1] be an new segment b[1],then do that until all the element in array a be visited,and then you should just traverse b array to find the answer.
code:
 #include <bits/stdc++.h> using namespace std; pair<int,int> a[10005],b[10005]; void debug(int cnt){      for (int i=0;i<cnt;i++){          cout<<i<<" "<<"Event ";          cout<<b[i].second<<" "<<b[i].first<<endl;      } } int main() {      freopen("milk2.in","r",stdin);      freopen("milk2.out","w",stdout);      int n;      while (scanf("%d",&n)==1) {          int cnt=0;          for (int i=0;i<n;i++){              scanf("%d%d",&a[i].first,&a[i].second);          }          sort(a,a+n);          b[cnt++]=a[0];          for (int i=1;i<n;i++){              int flag=0;              for (int ii=0;ii<cnt;ii++){                  if (a[i].first<=b[ii].second){                      b[ii].second=max(b[ii].second,a[i].second);                      b[ii].first=min(b[ii].first,a[i].first);                      flag=1;                      break;                  }              }              if (!flag) b[cnt++]=a[i];          }          //debug(cnt);          if (cnt==1) printf("%d %d\n",b[cnt-1].second-b[cnt-1].first,0);          else{              int mxt=0,mxl=0;              for (int i=0;i<cnt;i++){                 mxl=max(b[i+1].first-b[i].second,mxl);                 mxt=max(b[i].second-b[i].first,mxt);              }              printf("%d %d\n",mxt,mxl);          }      }      return 0; }                                


1 0
原创粉丝点击