hdoj 1007 java MLE通不过

参考网上的一个算法,但是一直都是MLE,实在是想不出来什么办法了。

参考博客:编程之美寻找最近点对

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static int N=100000;
    public static int[] list=new int[N];
    public static Point[] ptns=new Point[N];
    public static float MAX=Float.MAX_VALUE;
    public static float dis(int i,int j)
    {
        return (ptns[i].x-ptns[j].x)*(ptns[i].x-ptns[j].x)+(ptns[i].y-ptns[j].y)*(ptns[i].y-ptns[j].y);
    }
    public static float closestPair(int left,int right)
    {
        float d=MAX;
        if(left==right)
            return d;
        else if(left+1==right)
            return dis(left, right);
        int mid=(left+right)>>1;
        float d1=closestPair(left, mid);
        float d2=closestPair(mid+1, right);
        d=Math.min(d1, d2);
        int i=0,j=0,k=0;
        for(i=left;i<=right;i++)
        {
            if(Math.abs(ptns[mid].y-ptns[i].y)<d && i!=mid)
                list[k++]=i;
        }
        //对d距离内的点求最近点对
        for(i=0;i<k;i++)
        {
            for(j=i+1;j<k && Math.abs(ptns[list[j]].x-ptns[list[i]].x)<d;j++)
            {
                float dx=dis(list[i], list[j]);
                if(d>dx)
                {
                    d=dx;
                }
            }
        }
        return d;
    }
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n=Integer.MAX_VALUE;
        n=scanner.nextInt();
        while(n!=0)
        {
            for(int i=0;i<n;i++)
            {
                Point point=new Point();
                point.x=scanner.nextFloat();
                point.y=scanner.nextFloat();
                ptns[i]=point;
            }
            Arrays.sort(ptns,0,n-1);
            System.out.println(String.format("%.2f",Math.sqrt(closestPair(0, n-1))/2));
            n=scanner.nextInt();
        }
        scanner.close();
    }
    static class Point implements Comparable<Point>
    {
        public float x;
        public float y;
        @Override
        public int compareTo(Point o) {
            // TODO Auto-generated method stub
            if(this.y>o.y)
                return 1;
            else if(this.y<o.y)
                return -1;
            else
            {
                if(this.x>o.x)
                    return 1;
                else if(this.x<o.x)
                    return -1;
                return 0;
            }
        }
    }
}
时间: 2024-10-12 12:09:19

hdoj 1007 java MLE通不过的相关文章

HDOJ 1000 java

第一次做hdoj 试了好几次才通过 发现主类的名字必须是Main import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); while(scanner.hasNext()) { int A=scanner.nextInt(); int B=scanner.nextInt(); System.out.p

ACM--大数阶乘--HDOJ 1042--N!--Java

HDOJ题目地址:传送门 N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 74952    Accepted Submission(s): 21832 Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Inp

HDOJ 1001 java

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); int sum,n; while(scanner.hasNext()) { n=scanner.nextInt(); sum=0; for(int i=1;i<=n;i++) { sum+=i; } System.out.println(sum

HDOJ 1002 java

import java.math.BigInteger; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); int n=scanner.nextInt(); for(int i=1;i<=n;i++) { BigInteger a=sca

基础贪心题 HDOJ 1007

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 80535    Accepted Submission(s): 27842 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gu

HDOJ 1007 Quoit Design 最近点对

3年前刚接触ACM的时候做的这一题,交了几十次怎么写都过不了......... 今天无意中又看到了这一题,随手一写终于过了..... Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 33151    Accepted Submission(s): 8709 Problem Description Have y

【hdoj 1007】最近点对

题目:传送门 解答:直接暴力求解肯定会超时.此题核心就是求出来最近的一对点之间的距离,即最近点对算法. 简要来说就是利用分治的思想,左半边的最小距离,与右半边的最小距离比较,得到一个mindist.再遍历分界线左右 mindist 范围内点的间距,取最小值. 这样,需要暴力的只有分界线周围的点.但是我第一次提交版本还是超时.询问之后是因为优化不够,写在trick中. 这里一些trick: 分界线:不一定是距离上的等分,根据 x 轴位置排序后进行数量上的等分(取最中间的点)左右更好: 取中点暴力时

Hdoj 1007 Quoit Design 题解

Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded. In the field of Cyberground, the position of each toy is fixed, and the ring is careful

【HDOJ 1002】A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 238517    Accepted Submission(s): 45969 Problem Description I have a very simple problem for you. Given two integers A and B, you