Who's in the middle?

The solution to http://dsalgo.openjudge.cn/dsmoochw9/2/.