您的位置:首页 > 其它

sgu 136 Erasing Edges

2012-11-02 18:20 162 查看
题目描述:


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

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

利用此题顺便测试了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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: