Greetings,
I just solved the Skyline problem on the online judge. I have to say, this is a very enjoyable problem after that Arbitrage problem. It is very, very straightforward and the only problem is actually getting how the output works. Once that is understood, though, things are easy
In the skyline problem, you are supposed to compute the skyline of a city, given the starts, ends and heights of buildings. The skyline is segmented into 10.000 discrete segments, which all have a certain integer height. Your input consists of triples (L, H, R). Inside such a tripel, L is the lower inclusive coordinate of the building described, R is the exclusive upper coordinate of the building described and H is the height of the building. Common sense applies, as if there are two buildings overlapping in certain segments of the skyline, you will see the higher building.
Given this, you are supposed to compute a description vector for the resulting skyline. I found the ACM-description of the output to be pretty unclear and thus, it took some time to actually see what they wanted. Basically, the output O = <o1, o2, o3, o4, …, o2n-1, o2n> has to be read as: (1) walk to the right until cooridinate o1, and walk to height o2, walk to the right until coordinate o3, walk to height o4, … and so on. Thus, one needs to count the amount of steps to the right one has to take until the height changes, output this amount and output the new height (and repeat this for the entire distance).
Being armed with this understandings, the implementation is straightforward. It is not even necessary to store the buildings, because in order to compute the output, we just need an array which contains the maximum heights at each coordinate of the skyline. Given this, the output can be easily computed via:
function print_result(array height of int with n elements) {
for i = 1 ... n {
if (height[i] != height[i-1]) {
print(i + " " + height[i]);
}
}
Given this, it suffices to compute this height array, given the input triples mentioned above. This can be done initializiing the array heights with 0′s and whenever a triple (L, H, R), you compute max(H, heights[i]) for all L <= i < R.
Regards, tetha.