首页 > 专题系列 > Java解POJ > POJ 2653 Pick-up sticks [解题报告] Java
2013
11-11

POJ 2653 Pick-up sticks [解题报告] Java

Pick-up sticks

问题描述 :

Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.

输入:

Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.

输出:

For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.

The picture to the right below illustrates the first case from input.

样例输入:

5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0

样例输出:

Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.

温馨提示:

Huge input,scanf is recommended.

解题代码:

//* @author:
import java.io.*;
import java.util.*;
public class Main
{
 public static void main(String args[]) throws Exception
 {
  Scanner cin=new Scanner(System.in);
  int N;
  double[][] point;
  int i,j;
  while(true)
  {
	N=cin.nextInt();
	if(N==0) break;
	point=new double[N][4];
	for(i=0;i< N;i++)
	{
         for(j=0;j< 4;j++)
	  {
	   point[i][j]=cin.nextDouble();
	   }
	 }
	 System.out.print("Top sticks:");
	  for(i=0;i< N-1;i++)
	  {
	   for(j=i+1;j< N;j++)
	   {
	    if(fun(point[i],point[j])) break;
	    }
	   if(j==N) System.out.print(" "+(i+1)+",");
	   }
	   System.out.println(" "+N+".");
   }
 }
	
 static boolean fun(double p[],double q[])
 {
  if(((q[1]-p[1])*(p[0]-p[2])+(q[0]-p[0])*(p[3]-p[1]))*((q[3]-p[1])*(p[0]-p[2])+(q[2]-p[0])*(p[3]-p[1]))< 0)
  if(((p[1]-q[1])*(q[0]-q[2])+(p[0]-q[0])*(q[3]-q[1]))*((p[3]-q[1])*(q[0]-q[2])+(p[2]-q[0])*(q[3]-q[1]))< 0)
	return true;
	return false;
  }
}

  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  2. 网站做得很好看,内容也多,全。前段时间在博客园里看到有人说:网页的好坏看字体。觉得微软雅黑的字体很好看,然后现在这个网站也用的这个字体!nice!

  3. Gucci New Fall Arrivals

    This is really nice to know. I hope it will be successful in the future. Good job on this and keep up the good work.