Reputation: 3089
This is a question from some programming contest( i don't know exactly which programming contest it belongs to , i saw it a year ago ). The question is as follows:
There are N buildings, each having its own height ( not necessarily unique )
{h1,h2,h3,...,hn}
We have to make all the buildings of same height say h.
The operations allowed are:
There is a cost associated with each building to remove/add a unit height. Say c(i) be the cost to remove/add a unit height to a building. The respective cost are as follows:
{c1,c2,c3,...,cn}
Assuming that we have enough height(unit) available, we have to find the min cost which is required to make all buildings of the same height.
Input: First Line will specify N the number of buildings. ( 1<=N<=100000). Second Line of Input will be for the height of buildings.
{h1,h2,h3,...,hn}
Third line of input will give the cost array
{c1,c2,c3.....,cn}
Output
The output will be simply the min cost required.
Sample Input:
3
2 1 3
10 1000 1
Sample Output
12
Explanation: Make all the buildings of height 1, so the cost will be 10*(2-1) + 1000*(1-1) + 1*(3-1) i.e 12.
I know the brute force force method which is O(n^2).
The brute force method i thought is as follows:
Whatever the common height h be, it must be from the
{h1,h2,h3,....,hn}
i.e. h must be equal to any of h(i) . Now checking for each h(i) I can compute the answer in O(n^2).
is it possible to do it faster?
Upvotes: 5
Views: 1985
Reputation: 890
O(n log(n)) Solution:
Let h(i) denote height of the ith building and let c(i) denote the cost of the ith building.
Now use this recursion:
Cost_of_Increase(i) = Cost_of_Increase(i-1) + ( h(i)-h(i-1) )*( Sum of costs of P(i-1) th building to P(n) th builing )
Note that in the above recursion the order of i and i-1 is according to the order of P, i.e. the sorted order.
Now let Cost_of_decrease(i) denote the cost if we make all the buildings that have heights GREATER than the ith building equal to hte height of the ith building in the SORTED ARRAY P, i.e. Cost_of_decrease(i) if we make the buildings P(1), P(2), ... P(i-2), P(i-1) equal to the height of the P(i) th building.
For this use this recursion:
Cost_of_decrease(i) = Cost_of_decrease(i+1) + ( h(i+1)-h(i) )*( Sum of cost of P(1) th building to P(i-1) th building )
So total cost for making the height of all the buildings equal to P(i) th building is:
Cost_of_Increase(i) + Cost_of_decrease(i).
Once we have this for all the buildings, just check for which one the cost is smallest, and that is the answer.
Hope it helps !
Upvotes: 2
Reputation: 70989
Do a ternary search or use an hill climbing algorithm on the height of all the buildings after the end of the algorithm. For each height you can compute the cost in linear time so the overall complexity will be O(N * log(H)) where H is the maximum possible resulting height.
EDIT: here is a pseudo code snippet that should work for you(this is hill climbing-like approach)
typedef long long ll;
ll get_cost(int h) {
if (h < 0 || h > MAX_HEIGHT) {
return MAX_VAL; // some unreachable positive value
}
...
}
int main() {
...
int current = 0;
int leftp, rightp;
ll cv, lv, rv;
ll step = SOME_BIG_ENOUGH_VALUE;
while (step > 0) {
leftp = current - step;
rightp = current + step;
cv = get_cost(current);
lv = get_cost(leftp);
rv = get_cost(rightp);
if (cv <= rv && cv <= lv) {
step /= 2;
} else if (lv < rv) {
current = leftp;
} else {
current = rightp;
}
}
...
}
Upvotes: 1