首页 > 专题系列 > Java解POJ > POJ 1089 Intervals [解题报告] Java
2013
11-09

POJ 1089 Intervals [解题报告] Java

Intervals

问题描述 :

There is given the series of n closed intervals [ai; bi], where i=1,2,…,n. The sum of those intervals may be represented as a sum of closed pairwise non−intersecting intervals. The task is to find such representation with the minimal number of intervals. The intervals of this representation should be written in the output file in acceding order. We say that the intervals [a; b] and [c; d] are in ascending order if, and only if a <= b < c <= d.

Task

Write a program which:

reads from the std input the description of the series of intervals,

computes pairwise non−intersecting intervals satisfying the conditions given above,

writes the computed intervals in ascending order into std output

输入:

In the first line of input there is one integer n, 3 <= n <= 50000. This is the number of intervals. In the (i+1)−st line, 1 <= i <= n, there is a description of the interval [ai; bi] in the form of two integers ai and bi separated by a single space, which are respectively the beginning and the end of the interval,1 <= ai <= bi <= 1000000.

输出:

The output should contain descriptions of all computed pairwise non−intersecting intervals. In each line should be written a description of one interval. It should be composed of two integers, separated by a single space, the beginning and the end of the interval respectively. The intervals should be written into the output in ascending order.

样例输入:

5
5 6
1 4
10 10
6 9
8 10

样例输出:

1 4
5 10

解题代码:

//* @author: SmilingWang
import java.util.*;

public class Main{
  public static void main(String[] args){
   Scanner in = new Scanner(System.in);
   int n;
   n = in.nextInt();
  Interval intervals[] = new Interval[n];
  for(int i = 0; i < n; i++){
    int x = in.nextInt();
    int y = in.nextInt();
    intervals[i] = new Interval(x, y);
   }
   qsort(intervals, 0, n-1);
   //Arrays.sort(intervals);
   //System.out.println(Arrays.toString(intervals));
   int st = intervals[0].s;
   int ed = intervals[0].e;
  for(int i = 0; i < n; i++){
	if(st >= intervals[i].s && st <= intervals[i].e){
         st = intervals[i].s;
	}
	if(ed >= intervals[i].s && ed <= intervals[i].e){
	  ed = intervals[i].e;
	}
	else if(ed < intervals[i].s){
	  System.out.println(st + " " + ed);
	  st = intervals[i].s;
	  ed = intervals[i].e;
	}
   }
   System.out.println(st + " " + ed);
 }

 public static void qsort(Interval arr[], int l0, int h0){
   int l = l0;
   int h = h0;
  Interval mid = arr[(l+h)/2];
		
  while(l<=h){
    while(l< h0 && arr[l].compareTo(mid) < 0)
	l++;
    while(h>l0 && arr[h].compareTo(mid) > 0)
	--h;
    if(l <= h){
	Interval temp = new Interval(arr[l].s, arr[l].e);
	arr[l].s = arr[h].s;
	arr[l].e = arr[h].e;
	arr[h].s = temp.s;
	arr[h].e = temp.e;
	l++;
	h--;
     }
    }
   if(l < h0)
	qsort(arr, l, h0);
		
   if(h > l0)
	qsort(arr, l0, h);
  }
}

class Interval implements Comparable{
  int s;
  int e;
  public Interval(int s, int e){
	this.s = s;
	this.e = e;
  }

  public int compareTo(Interval rhs){
    if(s == rhs.s){
	return e - rhs.e;
    }
    else{
	return s - rhs.s;
     }
   }

   public String toString(){
	return s + " " + e;
  }
}

  1. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。