Diamond Dealer
InterviewTime limit1sMemory limit128 MB
For each simple polygon given clockwise, count concave vertices (dents) and edges not touching any dent, then print max(-a*p + b*q, 0).
- Level
Medium4 of 10
- Topics
- Geometry, Implementation, Math, Brute force
- Solved
- No attempts yet
Problem
Mr. Chou is a diamond dealer in a two-dimensional world. To be a successful businessman, he must know the exact value of the (two-dimensional) diamonds he trades. Mr. Chou is tired of computing these values by hand, so you must write a program that does it for him.

The value of a diamond is determined by the smoothness of its surface. Smoothness depends on the number of faces on the surface: the more faces, the smoother the surface. If the surface has dents (shown in red in the figure), the value of the diamond decreases.
A diamond is given as a simple polygon defined by its vertices. Each concave vertex of the polygon (a vertex whose interior angle is greater than 180 degrees) is a dent, and each edge of the polygon is a face. A face is considered to be in a dent if at least one of its two endpoints is a concave vertex.
Let be the number of dents and let be the number of faces that are not in any dent. The value of the diamond is
where is the penalty for a dent and is the value of a face. If is negative, the diamond is worthless and its value is .
Input
The first line contains the number of test cases .
Each test case consists of:
- One line with three integers , , and : the penalty for a dent (), the value of a face (), and the number of vertices describing the diamond ().
- lines, each with two integers and (), giving the vertices of the polygon in clockwise order: .
No three vertices of a single diamond are collinear.
Output
For each test case, print a single line containing one integer: the value of the diamond.