10750 - Beautiful Points
Moderator: Board moderators
-
- Learning poster
- Posts: 63
- Joined: Mon Dec 22, 2003 5:05 am
- Location: Russia, Chelyabinsk
- Contact:
10750 - Beautiful Points
Hi !
Could you give me some tricks to find two points with shortest distance between them. O(N^2) algo is too slow for N=10000.
THanks in advance
Could you give me some tricks to find two points with shortest distance between them. O(N^2) algo is too slow for N=10000.
THanks in advance
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
I am at the top..
My algo is also O(nlogn) but I didn't use divide and conquer to solve this problem.
- I sort all the points with respect to the x-coordinate and then wrt y-coordinate incase of a tie.... and this takes O(nlogn) time.
- then I search for the closest pair points using approx. O(n).
My code gets AC in 0.166 seconds and it is ranked 1st ( as at today).
http://acm.uva.es/cgi-bin/OnlineJudge?ProblemStat:10750


- I sort all the points with respect to the x-coordinate and then wrt y-coordinate incase of a tie.... and this takes O(nlogn) time.
- then I search for the closest pair points using approx. O(n).
My code gets AC in 0.166 seconds and it is ranked 1st ( as at today).

http://acm.uva.es/cgi-bin/OnlineJudge?ProblemStat:10750
Isn't it the algo book by Coreman.rotoZOOM wrote: What does CRL means ?

-
- Learning poster
- Posts: 63
- Joined: Mon Dec 22, 2003 5:05 am
- Location: Russia, Chelyabinsk
- Contact:
Re: I am at the top..
Can you explain in more detail your second point.sohel wrote:My algo is also O(nlogn) but I didn't use divide and conquer to solve this problem.![]()
- I sort all the points with respect to the x-coordinate and then wrt y-coordinate incase of a tie.... and this takes O(nlogn) time.
- then I search for the closest pair points using approx. O(n).
My code gets AC in 0.166 seconds and it is ranked 1st ( as at today).![]()
http://acm.uva.es/cgi-bin/OnlineJudge?ProblemStat:10750
I thought about sorting, but I didn't understand what to do in further.

Here it goes:rotoZOOM wrote: Can you explain in more detail your second point.
I thought about sorting, but I didn't understand what to do in further. )
For each point P, check all the points from P[i-1] to its left until the distance of P->P[j] is more than dist of P->p[j+1].
Although it might look to be a n^2 algo but invariably the second loop terminates early.
-
- Learning poster
- Posts: 63
- Joined: Mon Dec 22, 2003 5:05 am
- Location: Russia, Chelyabinsk
- Contact:
sohel wrote:Here it goes:rotoZOOM wrote: Can you explain in more detail your second point.
I thought about sorting, but I didn't understand what to do in further. )
For each point P, check all the points from P[i-1] to its left until the distance of P->P[j] is more than dist of P->p[j+1].
Although it might look to be a n^2 algo but invariably the second loop terminates early.
Probably I don't understand exactly what you mean.
But for this example what you code output:
4 points (x,y):
A: (0,0)
B: (1,100)
C: (2,50)
D: (3,0)
As I understand sort isn't change any points place.
Well when we go from the last D point we check it with C point,
then we break as dist(D,B)>dist(D,C), and we never check (D,A), but
(D,A) is the shortest distance.
May be I didn't understand something ?
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
The book by Cormen, Rivest, Leiserson. A must-read.What does CRL means
For each point P, check all the points from P[i-1] to its left until the distance of P->P[j] is more than dist of P->p[j+1].
Although it might look to be a n^2 algo but invariably the second loop terminates early
Not only this is worst-case O(n^2) , I believe it is also incorrect. Either you haven't told us about some important trick that saves the day, or OJ test cases are hopeless.
BTW: guys, how did you make it to quote with "Krzysztof Duleba wrote:" instead of "Quote:"?
-
- Learning poster
- Posts: 63
- Joined: Mon Dec 22, 2003 5:05 am
- Location: Russia, Chelyabinsk
- Contact:
Krzysztof Duleba wrote:The book by Cormen, Rivest, Leiserson. A must-read.What does CRL means
For each point P, check all the points from P[i-1] to its left until the distance of P->P[j] is more than dist of P->p[j+1].
Although it might look to be a n^2 algo but invariably the second loop terminates early
Not only this is worst-case O(n^2) , I believe it is also incorrect. Either you haven't told us about some important trick that saves the day, or OJ test cases are hopeless.
BTW: guys, how did you make it to quote with "Krzysztof Duleba wrote:" instead of "Quote:"?
Just press "Reply with quote" button located in upper-right corner of message

I hope I can get this book as electronic book.
Believe it or not... the method I used got AC.
.. and my program was completly wrong.
My program produces the wrong output for the case rootZOOM mentioned.
Here goes the correct algo: ( hopefully )
sort all the points wrt x first then y.
then for each point P check all the points to its left until the difference between P.x and P[j].x is more then the current minimum value found.
where j = i-1, i-2, ..... , 0.
Warning : This case might not work for data to the limit.
.. and my program was completly wrong.
My program produces the wrong output for the case rootZOOM mentioned.
Here goes the correct algo: ( hopefully )
sort all the points wrt x first then y.
then for each point P check all the points to its left until the difference between P.x and P[j].x is more then the current minimum value found.
where j = i-1, i-2, ..... , 0.
Warning : This case might not work for data to the limit.
sohel wrote:Believe it or not... the method I used got AC.
.. and my program was completly wrong.
My program produces the wrong output for the case rootZOOM mentioned.
Here goes the correct algo: ( hopefully )
sort all the points wrt x first then y.
then for each point P check all the points to its left until the difference between P.x and P[j].x is more then the current minimum value found.
where j = i-1, i-2, ..... , 0.
Warning : This case might not work for data to the limit.
I use the same algo but get in result 2 second run time. How I can speed up it in ten times??
-
- Learning poster
- Posts: 63
- Joined: Mon Dec 22, 2003 5:05 am
- Location: Russia, Chelyabinsk
- Contact:
Arm.Turbo wrote:sohel wrote:Believe it or not... the method I used got AC.
.. and my program was completly wrong.
My program produces the wrong output for the case rootZOOM mentioned.
Here goes the correct algo: ( hopefully )
sort all the points wrt x first then y.
then for each point P check all the points to its left until the difference between P.x and P[j].x is more then the current minimum value found.
where j = i-1, i-2, ..... , 0.
Warning : This case might not work for data to the limit.
I use the same algo but get in result 2 second run time. How I can speed up it in ten times??
If you used algo explained by sohel last time then you can't speed up your
run time, cause your method have complexity O(N^2) see Per reply.
The reason behind my code being very fast is that I have used the wrong algorithm and that takes O(NlogN) time.
I mean I have used this one:
I have no idea how it worked and why the judge data is very shallow.
I mean I have used this one:
For each point P, check all the points from P[i-1] to its left until the distance of P->P[j] is more than dist of P->p[j+1].
I have no idea how it worked and why the judge data is very shallow.
-
- Learning poster
- Posts: 63
- Joined: Mon Dec 22, 2003 5:05 am
- Location: Russia, Chelyabinsk
- Contact:
sohel wrote:The reason behind my code being very fast is that I have used the wrong algorithm and that takes O(NlogN) time.
I mean I have used this one:
For each point P, check all the points from P[i-1] to its left until the distance of P->P[j] is more than dist of P->p[j+1].
I have no idea how it worked and why the judge data is very shallow.
If you use that kind of algo and you got AC, then judge data is incomplete.