Cut the Cake!
InterviewTime limit1sMemory limit1024 MB
Given a convex polygon and a horizontal cut y=Y, find the perimeters of the two pieces, smaller value first.
- Level
Medium4 of 10
- Topics
- Geometry, Implementation
- Solved
- No attempts yet
Problem
On the faraway planet of Gastronomia, the native Gastronomes consider polygonal pancakes to be the highest form of art (as well as the tastiest). Every year, they celebrate the Polycake Festival, in which thousands of ninja chef monasteries perform the Thousand Slices, a series of rituals of tremendous importance to Gastronome society. In each of these rituals, a polycake is carefully placed on a ceremonial altar, and a ninja chef solemnly cuts the cake with a single, perfectly straight slice, dividing it into two pieces, one on the North part of the altar (representing the past) and the other on the South part (representing the future). The two pieces are carefully separated, and their perimeters are measured. The results determine the proportions of the ingredients used in polycakes planetwide, until the next Polycake Festival.
As the foremost Novice at the Temple of the Promised Cosmic Polycake, you are entrusted with the task of measuring the perimeters of the two slices. Take care, for any mistakes will be mercilessly mocked by the Brotherhood of the Illusory Polycake. It would be best if you wrote a program to do this.
Given a polygon representing the polycake in the Cartesian plane and a line parallel to the X-axis indicating the position of the slice, compute the perimeter of each of the two pieces thus formed. The perimeter of a polygon is the sum of the lengths of all its sides. The input polygons are convex and simple.
Input
The first input line contains a positive integer, , the number of polycakes used in the ritual. This is followed by data sets, each representing a single slicing of a polycake.
The first line of each set has two integers, () and (), the number of vertices in the polycake and the y-coordinate of the horizontal cut. The equation of a line parallel to the X-axis has the form .
The next lines each contain two integers, and (), the Cartesian coordinates of the vertices of the polycake, in counter-clockwise order.
The cut always goes through the cake and never passes through a vertex.
Output
For each test case, first output "Case #i:" where is the test case number, starting with 1. Then output " ", the perimeters of the two slices, in increasing order. Output the results to 3 decimal places, rounded to the nearest thousandth (for example, 77.0113 rounds to 77.011, 88.0115 rounds to 88.012, and 99.0117 rounds to 99.012). Leave a blank line after the output for each test case. Follow the format illustrated in the sample output.