This is a question from some programming contest (I don’t know exactly what programming it refers to, I saw it a year ago). The question is this:
There are N buildings, each of which has its own height (not necessarily unique)
{h1,h2,h3,...,hn}
We must make all buildings of the same height say h.
Allowed operations:
- We can add some height to the building.
- We can remove some height from the building.
For each building, there is a cost associated with removing / adding unit height. Let's say c (i) will cost to remove / add unit height to the building. Relevant costs are as follows:
{c1,c2,c3,...,cn}
, (), , .
Input:
N . (1 <= N < 100000).
.
{h1,h2,h3,...,hn}
{c1,c2,c3.....,cn}
.
:
3
2 1 3
10 1000 1
12
: 1,
10 * (2-1) + 1000 * (1-1) + 1 * (3-1) i.e 12.
, O (n ^ 2).
, , :
h,
{h1,h2,h3,....,hn}
. h h (i).
, h (i), O (n ^ 2).
?