CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » March 2010 Contest » The Best Tower

The Best Tower

Problem code: NX

  • All Submissions

All submissions for this problem are available.

ByteTel, the largest mobile operator in Byteland, would like to find a place to construct a cellphone tower. There are N important places in Byteland that need to be considered.

These N buildings can be represented by N points in a 2D plane. The cellphone tower can be represented by a circle, and in order to construct the tower, ByteTel needs to specify its center and radius.

Moreover, the center of the tower cannot be outside the secure area, which is also a circle.

ByteTel wants to find a center and radius for the tower such that the total squared distance between each of the building and the tower is as small as possible.

The distance between a point and a circle is defined to be the difference between the distance from the circle's center to the point and the circle's radius.

Your task is to help ByteTel find a center and radius for their cellphone tower.

Input

The first line contains a number t (about 10), which is the number of test case. Each test case has the following form.

The first line contains three numbers (Xs, Ys) and Rs, specifying the secure circle.

The second line contains N, the number of buildings (1 N 100).

Each line in the next N lines contain two numbers which are the coordinates of a building.

All coordinates are integer and are between 0 and 10000 (inclusive).

Output

For each test case, output 3 numbers (X,Y) and R, rounded to 3 decimal digits, which are the center and radius of the cellphone tower.

Scoring

For each test case, your score is d/D in which:

d is the total square distance between each of the building and the tower your program has found

D is the total square distance between each of the building and the secure circle's center.

Your program's score is the sum of each test case's score, the lower the better.

Example

Input:
1

1 1 4
5
3 -2
2 1
5 3
4 3
4 -1

Output:
4.000 1.000 2.000

The sample output's score is 0.023444. The secure circle is red and the tower is yellow.


Date: 2010-02-05
Time limit: 6

s
Source limit: 50000
Languages: C C99 strict C++ 4.0.0-8 C++ 4.3.2 PAS gpc PAS fpc JAVA NICE JAR C# NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp SCALA HASK ERL CAML CLPS PRLG WSPC BF ICK TEXT JS


  • Submit

Comments

  • Login or Register to post a comment.

Whats the constraint on

manu.skc @ 1 Mar 2010 03:45 PM

Whats the constraint on radius of tower?

I think the real question is,

Brian Drake @ 1 Mar 2010 05:50 PM

I think the real question is, “What are the constraints on the coordinates?” My understanding is that -2 and -1 are not “between 0 and 10000 (inclusive)”.

@Manu It looks like there is no constraint, except that the tower cannot overlap any of the given buildings. This should be clarified though.

I believe that you need to

johnpaulrobinson @ 1 Mar 2010 11:07 PM

I believe that you need to parse

"The distance between a point and a circle is defined to be the difference between the distance from the circle's center to the point and the circle's radius."

as follows;

The distance ... is defined to be

[the difference between

{the distance from the circle's center to the point}

and {the circle's radius.}]

This seems to work for the example and places a constraint on the tower's radius.

I would like to see a

triplem @ 2 Mar 2010 11:18 AM

I would like to see a constraint on R as well. (Plus fixing the test case or constraints on input points.)

Actually, the question itself doesn't specify whether the tower can contain buildings. Which isn't necessarily a silly question, since a tower could easily be built above buildings. So I would like that clarified too.

Scoring seems to be broken

triplem @ 2 Mar 2010 11:34 AM

Scoring seems to be broken too. Everyone is getting 0 points.

do we have to print a new

shrikant169 @ 2 Mar 2010 01:23 PM

do we have to print a new line after inputting number of test cases??

it doesnt say so in the problem statement but appears to be like that in the given example.

Please clarify. Thanks.

sorry, i'd like to reframe my

shrikant169 @ 2 Mar 2010 01:51 PM

sorry, i'd like to reframe my previous query.

is there a new line to be read after we read the number of test cases T ??

it doesnt say so in the problem statement but appears to be like that in the given example.

Please clarify. Thanks.

Checking the issue related to

i0exception @ 2 Mar 2010 02:50 PM

Checking the issue related to scoring. Internally it's working, it's being displayed as 0. So you may continue making submissions.

Checking the problem statement and constraints as well.

As a general point, the

triplem @ 2 Mar 2010 03:11 PM

As a general point, the submission system itself seems to be acting up - I seem to get internal error the first submission to a problem; every submission after that, I'm seeing the result of the *previous* submission. Had me very confused for a while..

The scoring issue is fixed.

i0exception @ 2 Mar 2010 04:38 PM

The scoring issue is fixed. Please let me know if you are facing problems related to it.

@Stephen Will check this. The 'My Submissions' page still displays the correct results. You can check status on that page till this is resolved.

what is squared distance. is

Naman Joshi @ 2 Mar 2010 05:38 PM

what is squared distance. is it the square of distance?

IMPORTANT: @All R is between

admin @ 2 Mar 2010 06:02 PM

IMPORTANT:

@All

R is between 0 and 10000 (inclusive) too

Co-ordinates in the test data are nonnegative.
The tower can contain buildings

Now the 'my submissions'

triplem @ 3 Mar 2010 02:03 AM

Now the 'my submissions' scores are right but the scoreboard has everyone on 0 for this problem ;)

@Stephen. There is some issue

admin @ 3 Mar 2010 03:31 PM

@Stephen. There is some issue with the DB which should be resolved soon.

still the score to this

udayrocks2k8 @ 3 Mar 2010 06:01 PM

still the score to this problem is showing zero

Sample output is not the best

dzhulgakov @ 3 Mar 2010 07:27 PM

Sample output is not the best tower that could be built, is it? Are you sure that there can be buildings inside tower?

Nobody said the sample output

triplem @ 4 Mar 2010 02:52 AM

Nobody said the sample output was optimal. This is a challenge problem; being optimal is not the point.

Slight wording

tomek @ 4 Mar 2010 03:44 AM

Slight wording problem:

"output 3 numbers (X,Y) and R, rounded to 3 decimal digits"

should be something like:

"output 3 numbers (X,Y) and R that have up to 3 decimal digits after the decimal point"

The first formulation says that the numbers I put in the output are not necessarily my actual answer, but an approximation of the answer. This means that the judge has no way of checking correctness, since it does not know the actual exact answer that might be correct while the rounded approximation isn't.

By the way - since we have no

tomek @ 4 Mar 2010 03:50 AM

By the way - since we have no test cases to verify solutions after the contest - I want to make sure that the checker actually verifies correctness exactly. That is, points on the secure circle are allowed, points even slightly outside the secure circle are rejected (using exact arithmetic), coordinates with more than 3 digits after decimal point are rejected.

@Tomek The numbers you put in

Brian Drake @ 4 Mar 2010 05:42 PM

@Tomek The numbers you put in your output are your answer, by definition. I don't understand your comment.

What is total square

thocevar @ 4 Mar 2010 08:53 PM

What is total square distance? Sum of squared distances or is it squared sum of distances?

@Brian I guess I wasn't

tomek @ 5 Mar 2010 12:54 AM

@Brian

I guess I wasn't clear. The problem is that the problem doesn't ask you to print X, Y, R. It asks you to print r(X), r(Y), r(R), where r(a) = a rounded to 3 decimal digits. But the constraints are on X,Y,R and not on r(X), r(Y), r(R). Therefore the judge has no way of verifying constraints or scoring, because it doesn't know what your X,Y,R are, since these are not in the output.

Example: suppose (Xs,Ys)=0, Rs=1. You find a valid tower (X,Y,R) = (0.9999, 0.001, 1). The output contains r(X), r(Y), r(R) = 1, 0.001, 1. The judge has no way of verifying whether X,Y lies inside the secure circle, because it only knows r(X), r(Y).

I don't think in this

dileep98490 @ 7 Mar 2010 02:04 PM

I don't think in this problem, my output needs to be matched with expected output exactly.Then what is the meaning of getting "wrong answer" after submitting the solution?

Wrong answer means your

triplem @ 7 Mar 2010 02:24 PM

Wrong answer means your answer is not valid; which (unless you have done something like output a negative radius, or used the wrong input format) probably means your center is not within the secure circle.

my radius is positive ,input

dileep98490 @ 7 Mar 2010 02:35 PM

my radius is positive ,input format is correct,center is with in the secure circle.What else the problems could be?

That's about it, other than

triplem @ 7 Mar 2010 02:44 PM

That's about it, other than perhaps not rounding answers to 3dp. Don't forget, as Tomek mentioned above, perhaps you think your point is in the secure circle, but after rounding to 3dp it isn't. Happened to me at one point.

@ dileep radius of tower

mcsharma1990 @ 7 Mar 2010 03:00 PM

@ dileep radius of tower circle should also be less than 1000 as menioned by annirudh..

Can anybody explain me how

jitendra_theta @ 7 Mar 2010 03:53 PM

Can anybody explain me how the sample output's score 0.023444 has been calculated???

Thankx for ur suggestions

dileep98490 @ 7 Mar 2010 06:35 PM

Thankx for ur suggestions ,and last problem ,is there any specific format for output,like now I am giving it with tab spaces (x{r} tab y{r} tab r} ,is it right?

@dileepkumar You simply have

jitendra_theta @ 7 Mar 2010 06:51 PM

@dileepkumar

You simply have to give one space between each x(r) ,y(r) and  r

sample output

1.234 3.456 4.567

Thanx all for ur

dileep98490 @ 7 Mar 2010 08:01 PM

Thanx all for ur suggesions,and my main problem is

In Dev c++ ,cout.precison(4),is giving 3 digits after decimal place,Got it corrected

 

The ranking is faulty. Those

akshay_utkarsh @ 8 Mar 2010 10:15 PM

The ranking is faulty.

Those who submitted 3 problems have been assigned the score of 4; seems Strange

How exactly the distance is

gautamverma @ 10 Mar 2010 06:17 PM

How exactly the distance is calculated between a point & a circle?

I don't understand what ot do with radius?

The distance between a point

jdmetz @ 11 Mar 2010 06:53 AM

The distance between a point and a circle in this problem is defined to be the absolute value of the difference between the radius of the circle and the distance from the point to the center of the circle.  In other words, let D be the distance from the point to the center of the circle and R be the circle's radius.  The distance from the point to the circle is now abs(D-R).

Did anybody verify that it's

MichaelD @ 13 Mar 2010 09:26 PM

Did anybody verify that it's impossible to do better than 1.563631? I used gradient descent (kinda), starting at a few different points, but there's no guarantee that my algorithm would always find the best possible solution.

My brute forcer gave the

mcsharma1990 @ 14 Mar 2010 12:23 AM

My brute forcer gave the solution 1.563817 (very close to the best possible) :)

I think the test data for

abhijith @ 14 Mar 2010 12:33 AM

I think the test data for this problem should be given out

hello, what is the optimal

akantev @ 16 Mar 2010 04:00 AM

hello,

what is the optimal solution for this problem? the simple n straightforward brute force comes close , though isnt the best 1.....

also, given a totally different input set, would all optimal solutions ( i.e. those that fetched 1.000000 pts) still give the same answer?

Given that you know the

triplem @ 16 Mar 2010 06:08 AM

Given that you know the center of the tower, you can easily prove that the optimal radius is the average distance from the center to each point. After that, any sort of hillclimbing algorithm will lead to the best center.

CodeChef submission 210202

murad007 @ 19 Mar 2010 06:43 PM
CodeChef submission 210202 (C++ 4.3.2) plaintext list. Status: AC, problem NX, contest MARCH10. By yayamao (yayamao), 2010-03-13 14:59:45.
  1. #include<cmath>
  2. #include<cstdio>
  3.  
  4. #define MAX_POINT_NUMBER 100
  5.  
  6. #define EPS 1E-9
  7. #define INF 1E99
  8.  
  9. struct Point
  10. {
  11. double x,y;
  12.  
  13. inline Point(){}
  14. inline Point(double x,double y):x(x),y(y){}
  15. inline ~Point(){}
  16. };
  17.  
  18. struct Circle
  19. {
  20. Point o;
  21. double r;
  22.  
  23. inline Circle(){}
  24. inline Circle(Point o,double r):o(o),r(r){}
  25. inline ~Circle(){}
  26. };
  27.  
  28. inline double squaredDistance(const Point &a,const Point &b)
  29. {
  30. return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
  31. }
  32.  
  33. inline bool contain(const Circle &c,const Point &p)
  34. {
  35. return squaredDistance(c.o,p)<=c.r*c.r;
  36. }
  37.  
  38. Circle secureCircle;
  39.  
  40. int n;
  41. Point p[MAX_POINT_NUMBER];
  42.  
  43. Circle tower;
  44. double minTotalDistance;
  45.  
  46. void readData()
  47. {
  48. scanf("%lf %lf %lf",&secureCircle.o.x,&secureCircle.o.y,&secureCircle.r);
  49. scanf("%d",&n);
  50. for(int i=0;i<n;i++) scanf("%lf %lf",&p[i].x,&p[i].y);
  51. }
  52.  
  53. double search(const Point &o,double &totalDist)
  54. {
  55. double D=0,D2=0;
  56. for(int i=0;i<n;i++)
  57. {
  58. double distPower=squaredDistance(o,p[i]);
  59. D+=sqrt(distPower);
  60. D2+=distPower;
  61. }
  62. totalDist=(n*D2-D*D)/n;
  63. return D/n;
  64. }
  65.  
  66. void solve()
  67. {
  68. tower.o=secureCircle.o;
  69. tower.r=search(tower.o,minTotalDistance);
  70.  
  71. double delta=secureCircle.r;
  72.  
  73. while(delta>0.0005)
  74. {
  75. while(true)
  76. {
  77. bool loop=true;
  78. for(int dx=-8;dx<=8;dx++) for(int dy=-8;dy<=8;dy++)
  79. {
  80. Point newO=Point(secureCircle.o.x+dx*delta-EPS,secureCircle.o.y+dy*delta-EPS);
  81. if(!contain(secureCircle,newO)) continue;
  82. double dist;
  83. double r=search(newO,dist);
  84. if(dist<minTotalDistance)
  85. {
  86. minTotalDistance=dist;
  87. tower=Circle(newO,r);
  88. //loop=false;
  89. }
  90. }
  91. if(loop) break;
  92. }
  93. delta*=0.9;
  94. }
  95. printf("%.3lf %.3lf %.3lfn",tower.o.x,tower.o.y,tower.r);
  96. }
  97.  
  98. int main()
  99. {
  100. //freopen("in","r",stdin);
  101.  
  102. int T;
  103. scanf("%d",&T);
  104. while(T--)
  105. {
  106. readData();
  107. solve();
  108. }
  109.  
  110. return 0;
  111. }

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com