star

搜索

RSS

RSS Link

计数器

133359
求一个数的二进制有多少个1?
求一个数是第几个排列数

求平面最近点对

Star posted @ 2010年8月04日 03:33 in C语言 with tags c 数学 分治 平面点集 , 2286 阅读

对于平面上N个点,求其中两个距离最近的点。输入格式:点的个数N,然后是N个坐标即可计算出。

#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <stdlib.h>

struct pos {double x, y;};
typedef struct pos Pos;
Pos p[100000];
int d[100000];
double eps = 0.00001;

int cmp_x(const void *p, const void *q)
{
	double tmp=((Pos*)p)->x-((Pos*)q)->x;
	if(tmp>0) return 1;
	else if(fabs(tmp)<eps) return 0;
	else return -1;
}

double Dest(Pos p2, Pos p1)
{
    return ((p2.y-p1.y)*(p2.y-p1.y)+(p2.x-p1.x)*(p2.x-p1.x));
}

double Min(double a, double b)
{
    return a < b ? a : b;
}

int cmp(const void *a, const void *b)
{
     return(*(int *)a-*(int *)b);
}

double NSP(int left, int right)
{
    double m1, m2, m3 , res;
    int i, j, ix, mid;
    if(right - left == 1)
        return Dest(p[right], p[left]);
    else if(right - left == 2)
    {
        m1 = Dest(p[left], p[left+1]);
        m2 = Dest(p[left], p[left+2]);
        m3 = Dest(p[left+1], p[left+2]);
        return Min(m1, Min(m2, m3));
    }
    mid = (left+right)/2;
    res = Min(NSP(left, mid), NSP(mid+1, right));
    
    ix = 0;
    for(i = mid; i>=left && p[mid].x-p[i].x < res; --i)
        d[ix++] = i;
    for(i = mid+1; i<=right && p[i].x-p[mid+1].x < res; ++i)
        d[ix++] = i;
    qsort(d, ix, sizeof(d[0]), cmp);
    for(i = 0; i < ix; ++i)
        for(j = i+1; j<ix && j<i+4 && p[d[j]].y-p[d[i]].y < res; ++j)
            res = Min(res, Dest(p[d[j]], p[d[i]])); 
    return res;
}

int main(void)
{
    int N, i;
    while(scanf("%d", &N), N)
    {
    	assert(N!=1 && N<100000);
        for(i = 0; i < N; ++i)
            scanf("%lf %lf", &p[i].x, &p[i].y);
            
        qsort(p, N, sizeof(p[0]), cmp_x);
        printf("%.2lf\n", sqrt(NSP(0, N-1)));
    }
    return 0;
}

用的是分治法。

Tyler Hamilton 说:
2020年4月22日 03:32

Usually while I surf the 'net I look for web-sites which provide reliable information and so I found this site. Pleased when I came across your web blog because I greatly liked this and that I look forward to your next post. An incredible web-site and i'll come back again for further useful content…  us phone number redirect

Tyler Hamilton 说:
2020年4月25日 21:41

For this web site, you will see our account, remember to go through this info. buy phenq

Tyler Hamilton 说:
2020年5月07日 17:09

There is so much in this article that I would never have thought of on my own. Your content gives readers things to think about in an interesting way.https://www.thelostwaysreview.eubookshop.com/the-lost-book-of-remedies-review/

Tyler Hamilton 说:
2020年5月07日 18:29

Amazing, this is great as you want to learn more, I invite to       This is my page. his secret obsession

Anonymous 说:
2020年5月13日 05:55

Acknowledges for penmanship such a worthy column, I stumbled beside your blog besides predict a handful advise. I want your tone of manuscript... Arya Toufanian

Anonymous 说:
2020年5月14日 23:13

Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing... Benefits of Rutabagas

Anonymous 说:
2020年5月15日 08:05

Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your rss feed to stay informed of any updates.Essential Oils for Wrinkles

Anonymous 说:
2020年5月15日 08:10

It is somewhat fantastic, and yet check out the advice at this treat. Foods That Helps Boost Your Mood

Anonymous 说:
2020年5月15日 08:14

During this website, you will see this shape, i highly recommend you learn this review. Benefits Of Pomegranates

Anonymous 说:
2020年5月15日 08:19

Cool you write, the information is very good and interesting, I'll give you a link to my site.  Benefits of Orach

Anonymous 说:
2020年5月15日 08:23

I invite you to the page where     see how much we have in common. Benefits of Lemongrass Essential Oil

Anonymous 说:
2020年5月15日 08:28

You possess lifted an essential offspring..Blesss for using..I would want to study better latest transactions from this blog..preserve posting.. Benefits of Garlic

Anonymous 说:
2020年5月22日 18:08

Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.  новини

Anonymous 说:
2020年5月22日 18:16

It has fully emerged to crown Singapore's southern shores and undoubtedly placed her on the global map of residential landmarks. I still scored the more points than I ever have in a season for GS. I think you would be hard pressed to find somebody with the same consistency I have had over the years so I am happy with that. хотели гърция

Anonymous 说:
2020年5月22日 18:22

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.работни обувки

Anonymous 说:
2020年5月22日 18:27

This post is very simple to read and appreciate without leaving any details out. Great work!  мебели варна

Anonymous 说:
2020年5月22日 18:36

I can give you the address       Here you will learn how to do it correctly. Read and write something good. принтери варна

Anonymous 说:
2020年5月22日 18:40

I should assert barely that its astounding! The blog is informational also always fabricate amazing entitys. тениски

Anonymous 说:
2020年5月27日 14:03

You bear through a awesome vacancy. I sanity definitely quarry it moreover personally suggest to my buddys. I am self-possessed they determination be benefited from this scene.Insta Followers Pro

Anonymous 说:
2020年6月01日 03:12

You should mainly superior together with well-performing material, which means that see it: a course in miracles unity online radio

Anonymous 说:
2020年6月01日 03:30

On my website you'll see similar texts, write what you think. acim david hoffmeister audio

Anonymous 说:
2020年6月01日 03:35

I read this article. I think You put a lot of effort to create this article. I appreciate your work. spotify david hoffmeister

Anonymous 说:
2020年6月04日 18:23

It's superior, however , check out material at the street address.   mondaymotivation

Anonymous 说:
2020年6月08日 22:03

This is very interesting, but it is necessary to click on this link: voyance retour amour

Anonymous 说:
2020年6月12日 20:37

In this case you will begin it is important, it again produces a web site a strong significant internet site: essential oils for back pain

Anonymous 说:
2020年6月18日 13:52

I can give you the address       Here you will learn how to do it correctly. Read and write something good.https://www.butywolka.eu/

Anonymous 说:
2020年6月20日 03:18

In this particular article, you will see a summary, satisfy browse this post. Marketing agency Sydney

Anonymous 说:
2020年6月20日 14:25

Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written. Coloradocreditrepair

Anonymous 说:
2020年6月26日 19:41

I like to recommend exclusively fine plus efficient information and facts, hence notice it: Agenzia SEO

Anonymous 说:
2020年6月28日 15:54

I invite you to the page where     see how much we have in common. A4 Display Stand

Anonymous 说:
2020年7月02日 18:59

I prefer merely excellent resources - you will see these people in: www.iphysio.sg

Anonymous 说:
2020年7月02日 19:12

During this website, you will see this shape, i highly recommend you learn this review. https://www.dynamicmarketing.sg

호빠, 호스트바, 호빠 구인, 선수하 说:
2020年7月04日 19:00

그리고 샴페인 콜,(손님이 특정 금액 이상의 샴페인을 시켰을 때 진행되며 대체로 6만엔에서 8만엔 정도일때 들어간다.) 혹은 술을 원샷 할 시에 사용 할 수 있는 방법이 있다. 보통의 호스트바에서는 술을 원샷 할 때 다른 호스트가 물수건을 입가에 받쳐준다. 이는 마시는 동안 술이 흘러 옷이라던가 가게가 더럽혀지지 않게 하기위한 행위인데, 바로 이 때 술을 전부 마시지않고 적당량 물수건에 흡수시켜 버리는 것 이다. 애초에 술의 양에 따라 모든 술을 물수건에 흡수시키기는 어렵기 때문에 주의와 컨트롤 능력이 필요하다. 호빠

호빠, 호스트바, 호빠 구인, 선수하 说:
2020年7月09日 14:02

해외사이트 해외토토 추천 토토사이트 안전놀이터 업계최고 퀸88벳 해외사이트

Anonymous 说:
2020年7月17日 12:36

On my website you'll see similar texts, write what you think. Military Miniatures

Anonymous 说:
2020年7月19日 19:45

Such sites are important because they provide a large dose of useful information ...  Buy Instagram Followers

Anonymous 说:
2020年7月24日 19:06

There is so much in this article that I would never have thought of on my own. Your content gives readers things to think about in an interesting way. ICON

Anonymous 说:
2020年7月27日 23:07

Gangaur Realtech is a professionally managed organisation specializing in real estate services where integrated services are provided by professionals to its clients seeking increased value by owning, occupying or investing in real estate.  copripiumino matrimoniale

Anonymous 说:
2020年7月28日 02:51

For true fans of this thread I will address      is a free online!Pamela Obame Watara

Anonymous 说:
2020年7月30日 20:02

Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing... www.matures-webcam.com

Anonymous 说:
2020年8月03日 01:21

So it is interesting and very good written and see what they think about other people.  bitcoin casinos

cub customer id chec 说:
2022年10月03日 13:44

You can use Bank Passbook, Account Statement, Visit the Home Branch and Call the Customer Care to Find Your City Union Bank Customer ID. The customers receive User ID and Password for net banking. The User ID or enquiry number Check City Union Bank Account Balance Through Missed Call. cub customer id check The customers receive User ID and Password for net banking. The User ID or enquiry number Check City Union Bank Account Balance Through Missed Call.The customers receive User ID and Password for net banking. The User ID or enquiry number Check City Union Bank Account Balance Through Missed Call.

+1 Model Paper 2024 说:
2023年2月15日 16:20

Class 11th Question Paper 2024 Every one of the students are right now sitting tight for the 11th test Model Paper to be Download, so need to reveal to you that as per the data got from the sources as of late, +1 Model Paper 2024 The board will deliver its 11th test Model Paper, realizing that it will be made accessible to you. So continue to check this post consistently.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter