I was trying to solve the KOPC12A problem in SPOJ.
Link to problem: http://www.spoj.com/problems/KOPC12A/
Problem in brief:
Given n buildings, each of different height(number of bricks), with each building having a cost for adding or removing a brick, find the minimum cost to make all the buildings have the same height.
After trying to solve this problem, though in vain, I came across a solution that used ternary search, after sorting the input based on their heights.
I was not able to understand how the cost for equalizing the heights of the buildings becomes unimodal(because ternary search can only be applied on unimodal functions)
This stumped me and I was not able to proceed much further.
Any insights on this is much appreciated.
Thanks-
To expand on sasha's comment, we can define the (strong) unimodality of a function
fas the conditionand the (strong) convexity of a function
fas the conditionLet the heights of the buildings be
h1, ..., hnand the unit alteration costs bec1, ..., cn. The costf(h')to make all buildings heighth'isNow here is a sequence of propositions, each with a fairly simple proof, leading via induction to the conclusion that
fis unimodal.gwhereg(x) = |x|is convex.h, for all convex functionsg1, the functiong2whereg2(x) = g1(x - h)is convex.c > 0, for all convex functionsg1, the functiong2whereg2(x) = c g1(x)is convex.g1andg2, the functiong3whereg3(x) = g1(x) + g2(x)is convex.