/*
ID: sdjllyh1
PROG: milk2
LANG: JAVA
complete date: 2008/9/11
efficiency: O(n * logN)
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjl

程序解析:先把时间段按照起始时间进行排序,然后把相交的时间段合并,最后简单找出两个最大值
*/


import java.io.*;
import java.util.*;

class MilkTime implements Comparable {    
    
public    int startTime = 0;
    
public    int endTime = 0;
    
    
public MilkTime(int startTime, int endTime)    {
            
this.startTime = startTime;
            
this.endTime = endTime;
    }

    
public int compareTo(Object arg0) {
        MilkTime m1 
= (MilkTime) arg0;
        
if(this.startTime < m1.startTime)
            
return -1;
        
else if(this.startTime == m1.startTime)
            
return 0;
        
else
            
return 1;        
    }
    
    
    
public boolean isOverlapping(MilkTime value){
        
int left = Math.max(this.startTime, value.startTime);
        
int right = Math.min(this.endTime, value.endTime);
        
if(left <= right)
            
return true;
        
else
            
return false;
    }

    
    
public MilkTime unite(MilkTime value){
        
int left = Math.min(this.startTime, value.startTime);
        
int right = Math.max(this.endTime, value.endTime);
        
return new MilkTime(left, right);
    }

}


public class milk2 {

    
static private List<MilkTime> milkTime =new ArrayList<MilkTime>();
    
static private int longestWorking = 0;
    
static private int longestNoWorking = 0;
    
        
    
private static void init()  throws IOException{
        BufferedReader f 
= new BufferedReader(new FileReader("milk2.in"));   
        StringTokenizer st 
= new StringTokenizer(f.readLine());

        
int n = Integer.parseInt(st.nextToken());   
        
for(int i=0; i<n; i++)
        
{
            st 
= new StringTokenizer(f.readLine());
            
int start = Integer.parseInt(st.nextToken());
            
int end = Integer.parseInt(st.nextToken());
            milkTime.add(
new MilkTime(start, end));            
        }

    }

    
    
private static void show() throws IOException{
        PrintWriter out 
= new PrintWriter(new BufferedWriter(new FileWriter("milk2.out")));    
            
        out.print(longestWorking);
        out.print(
' ');
        out.print(longestNoWorking);
        out.println();
        out.close(); 
    }

    
    
static private void combination(){
        List
<MilkTime> tmpMilkTime =new ArrayList<MilkTime>();
        
        Iterator
<MilkTime> i = milkTime.iterator();
        MilkTime first 
= i.next();
        MilkTime second;
        
if(i.hasNext())
        
{
            
while(i.hasNext())
            
{
                second 
= i.next();
                
if(first.isOverlapping(second))
                
{                
                    first 
= first.unite(second);                                    
                }

                
else
                
{
                      //first.unite(first)返回一个与first拥有同样数据的对象,但是在内存中使用了新空间

                    tmpMilkTime.add(first.unite(first));

                      first = second;
                }

            }

            tmpMilkTime.add(first.unite(first));
            milkTime 
= tmpMilkTime;
        }

        
    }

    
    
static private void findLongest(){
        Iterator
<MilkTime> i = milkTime.iterator();
        MilkTime first 
= i.next();
        longestWorking 
= Math.max(longestWorking, first.endTime - first.startTime);
        MilkTime second 
= null
        
while(i.hasNext())
        
{
            second 
= i.next();
            longestWorking 
= Math.max(longestWorking, second.endTime - second.startTime);
            longestNoWorking 
= Math.max(longestNoWorking, second.startTime - first.endTime);
            first 
= second;
        }

    }

    
    @SuppressWarnings(
"unchecked" })
    
private static void run(){
        
        Collections.sort(milkTime);        
         
        combination();                
        
        findLongest();
    }

    
    
public static void main(String[] args) throws IOException {
        init();
        run();
        show();
        System.exit(
0);
    }


}

posted on 2008-09-11 22:51  刘永辉  阅读(350)  评论(2编辑  收藏  举报