So this problem is about finding the closest pair of points on the plane,here's my code which gets TLE. I think my code should work in logarithmic time,but it doesn't seem so.So I kindly ask to provide any advices or improvements,for those people who will try to help me,for simplicity,comments are inserted in the code.
Thanks.
UPDATE: Got accepted thanks to Alex_2oo8's observations,no more help needed
Hi, the problem seems to be in the part under comment
//Choosing elements from Secondary[] array
. You iterate over all n points, so the complexity of every call toSolveClosest
is O(n) and in total you receive O(n2) complexity.The main idea is that
SolveClosest(l, r)
must work in poly(r - l) time (if we don't consider the recursive calls). The only problem is that we want to have a subarray Main[l..r] in sorted by y order. So here are two solutions:SolveClosest(l, r)
, where len = r - l + 1. And O(n log2 n) in total.SolveClosest
process. This results in O(len) complexity for call and O(n log n) in total.Thanks,it was really helpful,I'll try to implement those improvements asap
By the way, just found that your template contains rather bad defines:
For example, in your solution the next line, replacing SolveClosest with just f
converts to
and... performs 3 recursive calls! I don't thing you really want this to happen.
lol haven't mentioned it thanks :D
I got AC in this problem:
http://mirror.codeforces.com/problemset/problem/429/D
My submission is here:
http://mirror.codeforces.com/contest/429/submission/6603978
My algorithm is the same with Wikipedia's one.
http://en.wikipedia.org/wiki/Closest_pair_of_points_problem
Have tried checking other participants solutions,as well as read an article on Wiki before posting this but didn't find it helpful :/ Anyway thanks for your time Alex_2oo8's advises are enough till now I think :)