快速切题 sgu136. Erasing Edges

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 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 N more 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 3decimal 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 i and 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
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct pnt{
    double x,y;
};
const int maxn=10001;
const double eps=1e-9;
pnt p[maxn];
int n;
int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%lf%lf",&p[i].x,&p[i].y);
    }
    double lvaluex=0,lvaluey=0;
    int cnt=1;
    for(int i=n-1;i>0;i--){
        lvaluex+=cnt?p[i].x:-p[i].x;
        lvaluey+=cnt?p[i].y:-p[i].y;
        cnt^=1;
    }
    if((n&1)==0&&n>2&&(fabs(lvaluex-p[0].x)>eps||fabs(lvaluey-p[0].y)>eps)){puts("NO");return 0;}
    puts("YES");
    double x=(p[0].x+lvaluex);
    double y=(p[0].y+lvaluey);
    for(int i=0;i<n;i++){
        printf("%.3f %.3f
",x,y);
        x=p[i].x*2-x;
        y=p[i].y*2-y;
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/xuesu/p/4028342.html