hdoj 1007 java MLE通不過

來源:互聯網
上載者:User

標籤:

參考網上的一個演算法,但是一直都是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;            }        }        }}

 

hdoj 1007 java MLE通不過

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.