136. Erasing Edges
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
Little Johnny painted on a sheet of paper a polygon with N vertices. Then, for every edge of the polygon, he drew the middle point of the edge. After that, he went to school. When he came back, he found out that his brother had erased the polygon (both the edges and the vertices). The only thing left were the middle points of the edges of the polygon. Help Johnny redraw his polygon.
Input
The first line of the input contains the integer number N (3<=N<=10 000). Then, N lines will follow, each of them containing 2 real numbers, separated by blanks: xi and yi. (xi,yi) are the coordinates of the middle point of the edge #i. The coordinates will be given with at most 3 decimal places.
Output
Print a line containing the word "YES", if the polygon can be redrawn, or "NO", if there exists no polygon having the given coordinates for the middle points of its edges. If the answer is "YES", then you should print Nmore lines, each of them containing two real numbers, separated by a blank, representing the X and Y coordinates of the vetices of the polygon. The coordinates should be printed with at least 3 decimal places. You should output the cordinates for vertex #1 first, for vertex #2 second and so on.. In order to decide which vertex of the polygon is #1,#2,..,#N, you should know that for every 1<=i<=N-1, edge #i connects the vertices labeled iand i+1. Edge #N connects the vertices N and 1.
Hint
The polygon may contain self-intersections. Although in many geometric problems, self-intersections only make things more difficult, in this case, they make things a lot easier.
Sample Input #1
4 0 0 2 0 2 2 0 2
Sample Output #1
YES -1.000 1.000 1.000 -1.000 3.000 1.000 1.000 3.000
Sample Input #2
4 0 0 2 0 2 2 1 3
Sample Output #2
NO
我们已知每条边的中点,记为X1,X2,...,Xn,Y1,Y2,...,Yn; 设每个点坐标为(p,q),则有: p1+p2=2X1; p2+p3=2X2; ... pn+p1=2Xn; q1+q2=2Y1; q2+q3=2Y2; ... qn+q1=2Yn; 很显然,当n为奇数时,方程有唯一解; 当n为偶数时,该方程为不定方程,显然当2X1-2X2+2X3-...-2Xn != 0时, 即p1+p2-p2-p3+p3+p4-...-pn-p1 != 0时,矛盾,此时方程无解;若等于0,那么不妨设p1=X1-1,q1=Y1+1(其实随便只要(p1,q1)与(X1,Y1)不重合),然后解方程即可。
#include <cstdio> using namespace std; const int MAXN = 10005; int N; double x[MAXN] = {0}, y[MAXN] = {0}; double sumx = 0, sumy = 0; double xx[MAXN] = {0}, yy[MAXN] = {0}; int main() { scanf("%d", &N); for(int i = 1; i <= N; ++i) { scanf("%lf%lf", x+i, y+i); if(i&1) sumx += x[i]*2, sumy += y[i]*2; else sumx -= x[i]*2, sumy -= y[i]*2; } if(N&1) { xx[1] = sumx/2; for(int i = 2; i <= N; ++i) xx[i] = 2*x[i-1]-xx[i-1]; yy[1] = sumy/2; for(int i = 2; i <= N; ++i) yy[i] = 2*y[i-1]-yy[i-1]; } else { if(sumx != 0 || sumy != 0) { printf("NO\n"); return 0; } xx[1] = x[1]-1; for(int i = 2; i <= N; ++i) xx[i] = 2*x[i-1]-xx[i-1]; yy[1] = y[1]+1; for(int i = 2; i <= N; ++i) yy[i] = 2*y[i-1]-yy[i-1]; } printf("YES\n"); for(int i = 1; i <= N; ++i) printf("%.3lf %.3lf\n", xx[i], yy[i]); return 0; }