sgu 136解题记录

7 篇文章 0 订阅

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 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 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

题目大意:

        小强尼在纸上画了一个多边形(允许自交),然后标出了每条线段的中点,然后去上学了。但是在放学回来后,他发现所有的线段和端点都被他弟弟擦掉了,只剩下线段的中点,请帮助他重画这个多边形。

解题记录:

第一想法:通过两个相邻的中点,可否确定端点。貌似实现不了;

再换个想法,中点连接起来构成一个新的多边形,可否通过转化得到原来的多边形。似乎我连正确的中点应当连起来的顺序都确定不了(有自交情况);

从答案的另一个角度考虑一下,如何判断“NO”呢?貌似= =,我也无法判断,是不是计算几何这条思路走错了。

请教题解:

        发现自己忽略了一个很重要的题目已知条件,给的点全部都是中点,那么如果知道其中的一个点和中点之间的顺序的话,就可以通过无限对称求出所有的点。

然而,我们又可以利用中点来求出某一个顶点。 设xn为线段的端点,cn是给出的线段的中点,我们以题目给出的样例为数据。

X1+X2=C1   X2+X3=C2  X3+X4=C3  X4+X1=C4

        这里,我们可以通过C1-C2+C3-C4=0,额这里我们似乎发现等于0了,如若给出的N是奇数呢? 则可以利用(这里以N=5为例)C1-C2+C3-C4+C5= 2X1求出X1这个点,也就是线段1中的其中一个顶点。这里我们可以得出一个结论,如若N为奇数,那就一定可以有唯一一个解,用这种方法就可以求出。

        接下来我们可以考虑N为偶数的情况,可以有无数个解,但是存在无解的情况,那就是C1+C2+C3...+CN != 0。


sub1 wa test2 实在不知道哪里错了,看了一下别人的代码,发现在判断浮点数是否等于0的情况下,直接用 x==0 而我用的是 x-0 < emp 其中emp=1e-4;

sub2 ac 62ms 202kb 难道是我精度没有把握好?

解题报告:

利用题目给的数据,都是中点,而且是按照顺序相邻的中点(详见output的英文)。按照N的奇偶来进行分别处理。

        如若是奇数,则必然有唯一一个解。可以通过C1-C2+C3-C4+...+CN= 2X1,也可以同理求出Y1。然后通过跟中点不断对称可以求出其他各个端点。

        如若是偶数,则可以任意赋值都可以求出解。但首先要保证c1-c2+c3-c4...+cn =0这个式子成立,不成立则无解。

代码:

#include <cstdio>
#define  emp 1e-4

int n;
double x[10010], y[10010];

void init() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%lf%lf", &x[i], &y[i]);
		x[0] += i&1?x[i]:-x[i];  //累加起来,如若是奇数则可以得出第一个顶点,如若是偶数则可以之后判断是否无解
		y[0] += i&1?y[i]:-y[i];
	} 
}

void solve() {
	if (!(n&1)) {
		if (x[0]==0 && y[0]==0) {  //有解情况,随意赋值,按照奇数的情况进行输出
			x[0] = 1;
			y[0] = 1;
		}
		else {
			printf("NO\n");
			return;
		}
	}

	printf("YES\n%.3lf %.3lf\n", x[0], y[0]);
	for (int i = 1; i <= n; i++) {
		printf("%.3lf %.3lf\n", x[i]=2*x[i]-x[i-1], y[i]=2*y[i]-y[i-1]);
	}
}

int main() {
	init();
	solve();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值