sgu 136 Erasing Edges

57 篇文章 0 订阅
39 篇文章 0 订阅

题目描述:

136. Erasing Edges

time limit per test: 0.5 sec.
memory limit per test: 4096 KB

Little Johnny paintedon a sheet of paper a polygon with N vertices. Then, for every edgeof the polygon, he drew the middle point of the edge. After that, he wentto school. When he came back, he found out that his brother had erasedthe polygon (both the edges and the vertices). The only thing left werethe middle points of the edges of the polygon. Help Johnny redraw his polygon.

Input

The first line ofthe input contains the integer number N (3<=N<=10 000).Then, N lines will follow, each of them containing 2realnumbers, separated by blanks: xi and yi. (xi,yi)arethe coordinates of the middle point of the edge #i. The coordinateswill be given with at most 3 decimal places.

Output

Print a line containingthe word "YES", if the polygon can be redrawn, or "NO", ifthere exists no polygon having the given coordinates for the middle pointsof 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 thepolygon. The coordinates should be printed with at least 3 decimalplaces. You should output the cordinates for vertex#1 first, forvertex #2 second and so on.. In order to decide which vertex ofthe polygon is #1,#2,..,#N, you should know that forevery 1<=i<=N-1, edge #i connects the vertices labeledi and i+1. Edge #N connects the verticesN and1.

Hint

The polygonmay contain self-intersections. Although in many geometric problems, self-intersectionsonly 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


n为奇数时可以直接求解,n为偶数时可能有无穷多解或无解。

原因如下: 我们仅考虑横坐标,假设解是 x1 ,x2,x3,x4.........,xn

那么我们有以下方程 x1+x2=2*c[1]

                                 x2+x3=2*c[2]

                                ...................

                                ..................

                               xn+x1=2*c[n]

这样的方程相信大家都会解吧。。。。


利用此题顺便测试了std::ios::sync_with_stdio(false);这句语法的效果,时间果然是有较大差距的。

分别是156ms 和218ms。


贴上代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>

#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)

using namespace std;
int const nMax = 10010;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;
double const eps=1e-4;

double C[2][nMax];
double ans[2][nMax];
int n;

int main(){
    std::ios::sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++)cin>>C[0][i]>>C[1][i];
    for(int i=0;i<2;i++){
        int d=1;
        double B=2*C[i][0];
        for(int j=1;j<n-1;j++){
            if(d==1){
                B-=2*C[i][j];
                d=-1;
            }else {
                B+=2*C[i][j];
                d=1;
            }
        }
        //cout<<B<<" "<<2*C[i][n-1]<<endl;
        if(d==1){
            //if(fabs(B-2*C[i][n-1])>eps){
            if(B!=2*C[i][n-1]){
                cout<<"NO"<<endl;
                return 0;
            }
            //ans[i][0]=1.2345;
            if(i&1)ans[i][0]=1;
            else   ans[i][0]=-1;
        }else{
            ans[i][0]=(B+2*C[i][n-1])/2.0;
        }
        for(int j=0;j<n-1;j++)ans[i][j+1]=2*C[i][j]-ans[i][j];
    }
    cout<<"YES"<<endl;
    for(int i=0;i<n;i++)//cout<<ans[0][i]<<" "<<ans[1][i]<<endl;
        printf("%.3lf %.3lf\n",ans[0][i],ans[1][i]);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值