usaco 1.1 Miking Cows

来源:互联网 发布:csmhuan实时数据 编辑:程序博客网 时间:2024/06/05 05:43
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 integerLines 2..N+1:Two non-negative integers less than 1000000, 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



是一个很典型的分区间的题目,给出n个区间,求最大的连续区间以及最大的区间间的间隔。

先对每个区间[a,b]的a值进行排序,再将每一个区间的a值与当前连续区间的b值比较,如果小于,则将该区间合并进当前的连续区间;如果大于,则将该区间另起,作为一个新的当前区间。

而原始的连续区间则与最大的连续期间比较,更新最大值。

然后就是注意细节的处理,没有什么要特别处理的。。。


代码:

View Code
 1 /* 2 ID: jings_h1 3 PROG: milk2 4 LANG: C++ 5 */ 6  7  8 #include<iostream> 9 #include<algorithm>10 #include<fstream>11 using namespace std;12 struct node{13        int start;14        int end;15 };16 node p[5005];17 bool cmp(const node &a,const node &b){18      return a.start<b.start;19 }20 int main(){21     ofstream fout ("milk2.out");22     ifstream fin ("milk2.in");23     int n;24     while(fin>>n){25         for(int i=0;i<n;i++){26                 fin>>p[i].start>>p[i].end;27                 }28         sort(p,p+n,cmp);29         int maxmilktime=(p[0].end-p[0].start),maxrestime=0;30         int nowend=p[0].end,nowstart=p[0].start;31         for(int j=1;j<n;j++){32                 if(p[j].start<=nowend){33                         if(p[j].end>nowend){34                                 nowend=p[j].end;35                                 }36                                 }37                 else{   38                         if(maxmilktime<(nowend-nowstart)){39                                 maxmilktime=(nowend-nowstart);40                                 }41                         if(maxrestime<(p[j].start-nowend)){42                                 maxrestime=p[j].start-nowend;43                                 }44                         nowstart=p[j].start;45                         nowend=p[j].end;46                         }47                // cout<<nowstart<<" "<<nowend<<endl;48                         }49         if((nowend-nowstart)>maxmilktime){50                  maxmilktime=nowend-nowstart;51                  }52         fout<<maxmilktime<<" "<<maxrestime<<endl;53         }54     return 0;55 }56                         57