#K83772. Convex Polygon Checker
Convex Polygon Checker
Convex Polygon Checker
Given the number of vertices n and a list of points in counter-clockwise order, determine whether these points form a convex polygon. A polygon is convex if, for every three consecutive vertices \(p_1, p_2, p_3\), the cross product of the vectors \(p_2 - p_1\) and \(p_3 - p_2\) has a consistent sign.
The cross product between vectors \(\vec{v}\) and \(\vec{w}\) is computed by the formula:
\[ (p_{2x} - p_{1x})(p_{3y} - p_{2y}) - (p_{2y} - p_{1y})(p_{3x} - p_{2x}) \]
If all computed cross products are positive or all are negative, then the polygon is convex; otherwise it is not.
inputFormat
The input is read from stdin and consists of multiple lines:
- The first line contains an integer n — the number of vertices.
- Each of the next n lines contains two space-separated integers representing the coordinates \(x\) and \(y\) of a vertex.
outputFormat
Output to stdout a single line with YES
if the vertices form a convex polygon, or NO
otherwise.
4
0 0
2 0
2 2
0 2
YES