## 11460 - Balance

All about problems in Volume 114. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

kalyon
New poster
Posts: 5
Joined: Wed Jun 25, 2008 10:49 am

### 11460 - Balance

do i have to implement centroid of a polygon algorithm from formula like in http://en.wikipedia.org/wiki/Centroid
or is there a simple way that i couldnt figure it out yet ...any idea?

kalyon
New poster
Posts: 5
Joined: Wed Jun 25, 2008 10:49 am

### Re: 11460 - Balance

i figured out as:
first make two polygon set as above and below the x-axis.
second add the points to these polygons at which points original big polygon crosses the x-axis.
third find the centroids of these polygons.
finally find the diff. between x coordinates of two centroids.

the important step is second step. when you adding this points to the polygons you have to be careful about the order of the points.

all i need now is to express this as a code.

Chirag Chheda
Learning poster
Posts: 74
Joined: Sat Jun 21, 2008 12:24 pm
Location: India

### Re: 11460 - Balance

Well in test case given in the problem
first polygon->(0 1) (2 3) (4 1) ..Its centroid(2,5/3)
second polygon->(4 -3) (2 -3) (2 -1) (0 -1)..Its centroid (2,-2)

Hence the difference between the X co-ordinate is 0.
then how come the answer is 0.5??
Can u plz xplain

kalyon
New poster
Posts: 5
Joined: Wed Jun 25, 2008 10:49 am

### Re: 11460 - Balance

as i wrote before since there are two polygon sets we have to add extra points (on wich points the x-axis crossed) to these two polygons.

so the polygons must be:
CE: (0, 0), (0, 1), (2, 3), (4, 1), (4, 0) x-coord of centroid is 2
CLR: (4, 0), (4, -3), (2, -3), (2, -1), (0, -1), (0, 0) x-coord of centroid is 2.5

so the difference is 0.5

Chirag Chheda
Learning poster
Posts: 74
Joined: Sat Jun 21, 2008 12:24 pm
Location: India

### Re: 11460 - Balance

the x co-ordinate of CLR shud be 2 I think.
How come its 2.5

kalyon
New poster
Posts: 5
Joined: Wed Jun 25, 2008 10:49 am

### Re: 11460 - Balance

http://local.wasp.uwa.edu.au/~pbourke/g ... /polyarea/
you can find some sample codes.

according to my calculations test case is correct.

Chirag Chheda
Learning poster
Posts: 74
Joined: Sat Jun 21, 2008 12:24 pm
Location: India

### Re: 11460 - Balance

Thnx got it.Now i will try 2 solve it

Keep posting..

hasib_bd
New poster
Posts: 14
Joined: Wed Apr 30, 2008 12:39 pm

### 11460 - Balance

I'm getting wa.
For the below Inputs my code generates following outputs.

Input:

Code: Select all

``````15
5
0 3
4 1
3 -1
-3 -1
-4 1
5
-3 -1
3 -1
4 1
0 3
-4 1
8
0 0
0 1
2 3
4 1
4 -3
2 -3
2 -1
0 -1
7
0 1
0 -1
2 -1
2 -3
4 -3
4 1
2 3
7
0 1
2 3
4 1
4 -3
2 -3
2 -1
0 -1
7
0 -1
2 -1
2 -3
4 -3
4 1
2 3
0 1
7
4 -3
4 1
2 3
0 1
0 -1
2 -1
2 -3
7
-4 -3
-4 1
-2 3
0 1
0 -1
-2 -1
-2 -3
7
0 1
0 -1
-2 -1
-2 -3
-4 -3
-4 1
-2 3
9
-3 0
-3 3
0 3
0 0
1 0
2 0
2 -3
-1 -3
-1 0
9
-3 0
-1 0
-1 -3
2 -3
2 0
1 0
0 0
0 3
-3 3
10
0 3
-300 3
-3 0
-1 0
-1 -3
2 -3
2 0
4 0
301 3
0 3
3
-1 2
-4 -1
2 -1
3
-1 2
-4 -1
2 1
3
-1 2
-4 1
2 -1``````
Output:

Code: Select all

``````Balanced.
Balanced.
CE is aft of CLR by 0.50 units.
CE is aft of CLR by 0.50 units.
CE is aft of CLR by 0.50 units.
CE is aft of CLR by 0.50 units.
CE is aft of CLR by 0.50 units.
CE is forward of CLR by 0.50 units.
CE is forward of CLR by 0.50 units.
CE is aft of CLR by 2.00 units.
CE is aft of CLR by 2.00 units.
Balanced.
Balanced.
CE is forward of CLR by 2.00 units.
CE is aft of CLR by 2.00 units.
``````
Can anyone who solved this problem can provide more input/output? I'm not getting any reason for WA.

hasib_bd
New poster
Posts: 14
Joined: Wed Apr 30, 2008 12:39 pm

### Re: 11460 - Balance

Can there be any inputs like below?
The problem description doesn't clarify this...
Input:

Code: Select all

``````2
5
-3 2
0 -1
3 2
3 -2
-3 -2
6
-3 2
-1 0
1 0
3 2
3 -2
-3 -2``````

kalyon
New poster
Posts: 5
Joined: Wed Jun 25, 2008 10:49 am

### Re: 11460 - Balance

Problem definition says:
"The first line of input will contain a single integer n specifying the number of points along the outline of the side view of the boat. The following n lines will each contain two integers: the x and y coordinate of a point along the outline. The points will be given in order along the outline. The x axis (i.e. the line y = 0) represents the waterline. Assume that the boat faces in the direction of increasing x coordinates."

from this i understand:
1. points are in clockwise order or counter-clockwise order along the coordinate system. (but when you are solving it must be clockwise order. i dont know why i just saw this in some document about polygon centroids. )
2. x coordinates can be negative. (i dont see any statement about it in the problem.)

according to these your inputs seems correct to me.

but i really wonder the judge's test case because i get WA too