You are on page 1of 3

Mooing Cows

The cows in Byterland are mooing loudly, annoying the residents very much. Mrs. Darcy of
the villa Pemberley is planning to resolve this problem by allowing only one cow to moo.
She will pick the cow whose mooing is the least offensive to all the other cows.

The farmland in Pemberley is divided into n*m squares of grassland. Each square is either
empty or occupied by a single cow. When a cow at (x,y) moos, the dissatisfaction of a cow
at (i,j) is equal to the square of the distance between them: ((x-i)2 + (y-j)2). The total
dissatisfaction is the sum of the dissatisfaction of all the cows.

Input

Read first two integer numbers n and m both between 1 and 50 inclusive. Next read n lines
each of m characters of only two possible characters: a character ‘C’ denoting a cow, an ‘,’
denoting an empty square. You can be certain that there will be at least a cow for each test
case.

Output

Print the minimal total dissatisfaction that can be achieved by allowing only a single cow to
moo.

Sample Input

3
3
C..
.C.
.C.

4
4
CCCC
CCCC
CCCC

1
1
C

5 7
CCC....
C......
....C.C
.C.CC..
C......

Sample Output

3
26
0
81
B. Stand In Line
Soldiers at the Byterland South Border are ordered to stand in line each morning. They
always choose their positions randomly to the displeasure of their general. One evening, the
soldiers learn that their strict general has secretly recorded their standing positions from
that morning, and that he will be checking their positions the next morning to make sure
they are exactly the same.

Each soldier only remembers one thing from that morning: the number of people to his left
that were taller than him. There are n soldiers, each with a different height between 1 and n,
inclusive. Using this information, you must reconstruct the lineup from that morning.

Input

For each test case read a number soldiers between 1 and 10. Next soldiers numbers follows,
each one between 0 and n-i-1 (where n is the number of elements in left, and i is a 0-based
index)

Output

Print the sequence of soldiers, represented by their heights from left to right in the lineup.
The input is guaranteed to produce a valid and unique output.

Sample Input

4 2 1 1 0
5 0 0 0 0 0
6 5 4 3 2 1 0
7 6 1 1 1 2 0 0

Sample Output

4 2 1 3
1 2 3 4 5
6 5 4 3 2 1
6 2 3 4 7 5 1

You might also like