首页 > 专题系列 > Java解POJ > POJ 3067 Japan [解题报告] Java
2013
11-12

POJ 3067 Japan [解题报告] Java

Japan

问题描述 :

Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, ... from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.

输入:

The input file starts with T – the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one is the number of the city of the West coast.

输出:

For each test case write one line on the standard output:

Test case (case number): (number of crossings)

样例输入:

1
3 4 4
1 4
2 3
3 2
3 1

样例输出:

Test case 1: 5

解题代码:

//* @author: 82638882@163.com
import java.io.*;
import java.util.Arrays;
public class Main
{
 static long ans=0;
 public static void main(String[] args) throws IOException
 {
  InputStreamReader is=new InputStreamReader(System.in);
  BufferedReader in=new BufferedReader(is);
  int a=Integer.parseInt(in.readLine());
  int cnt=0;
  while((a--)!=0)
  {
   ans=0;
   cnt++;
   String[] ss=in.readLine().split(" ");
   int n=Integer.parseInt(ss[2]);
   my[] p=new my[n];
   for(int i=0;i< n;i++)
   {
	ss=in.readLine().split(" ");
	int x1=Integer.parseInt(ss[0]);
	int y1=Integer.parseInt(ss[1]);
	p[i]=new my(x1,y1);
    }
    Arrays.sort(p);
    mergesort(p,0,n);
    System.out.println("Test case "+cnt+": "+ans);
   }
 }

 static void mergesort(my[] arr,int f,int n)
 {
   int n1,n2;
   if(n>1)
   {
	n1=n/2;
	n2=n-n1;
	mergesort(arr,f,n1);
	mergesort(arr,f+n1,n2);
	merge(arr,f,n1,n2);
   }
 }


 static void merge(my[] arr,int f,int n1,int n2)
 {
  int[] temp=new int[n1+n2];
  int cp=0,cp1=0,cp2=0,i=0;
  while((cp1< n1)&&(cp2< n2))
  {
	if(arr[f+cp1].y< arr[f+n1+cp2].y)
		temp[cp++]=arr[f+(cp1++)].y;
	else if (arr[f+cp1].y==arr[f+n1+cp2].y)
	{
		temp[cp++]=arr[f+(cp1++)].y;
	}
	else {
		temp[cp++]=arr[f+n1+(cp2++)].y;
		ans+=n1-cp1;
	}
   }
  while(cp1< n1)
	temp[cp++]=arr[f+(cp1++)].y;
  while(cp2< n2)
	temp[cp++]=arr[f+n1+(cp2++)].y;
  for(i=0;i< cp;i++)
	arr[f+i].y=temp[i];
 }
}

class my implements Comparable< my>
{
	int x,y;
	public my(int a,int b)
	{
         x=a;
	  y=b;
	}
	public int compareTo(my arg0) {
	  if(x==arg0.x)
		 return y-arg0.y;
	  return x-arg0.x;
	}
}

  1. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
    O(n) = O(n-1) + O(n-2) + ….
    O(n-1) = O(n-2) + O(n-3)+ …
    O(n) – O(n-1) = O(n-1)
    O(n) = 2O(n-1)