The figure shown on the left is left-right symmetric as it is possible to fold the sheet of paper along a vertical line, drawn as a dashed line, and to cut the figure into two identical halves. The figure on the right is not left-right symmetric as it is impossible to find such a vertical line.
Write a program that determines whether a figure, drawn with dots, is left-right symmetric or not. The dots are all distinct.
Input
The input consists of T test cases. The number of test cases T is given in the first line of the input file. The first line of each test case contains an integer N, where N (1 ≤ N ≤ 1, 000) is the number of dots in a figure. Each of the following N lines contains the x-coordinate and y-coordinate of a dot. Both x-coordinates and y-coordinates are integers between −10, 000 and 10, 000, both inclusive.
Output
Print exactly one line for each test case. The line should contain ‘YES’ if the figure is left-right symmetric, and ‘NO’, otherwise.
Sample Input
3
5
-2 5
0 0
6 5
4 0
2 3
4
2 3
0 4
4 0
0 0
4
5 14
6 10
5 10
6 14
Sample Output
YES
NO
YES
Regionals 2004 >> Asia - Seoul
问题链接:UVA1595 UVALive3226 Symmetry
问题简述:(略)
问题分析:
这个问题是给出平面上N(N<=1000)个点。问是否可以找到一条竖线,使得所有点左右对称。
如果点集存在对称轴,则对称轴为点集x坐标和的平均值。
程序说明:
输入是整数,为了使得计算不需要使用浮点数(使用的话可能造成不精确),则稍微做了一点数学处理。
平均值为(x1+x2+......+xn)/n。
若xi=(x1+x2+......+xn)/n,那么2(x1+x2+......+xn)-nxi=nxi。
所以程序中将坐标(nxi,y)保存在集合中,计算处理就不用浮点数了。
题记:(略)
参考链接:(略)
AC的C++语言程序如下:
/* UVA1595 UVALive3226 Symmetry */
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> Point;
int main()
{
int t, n, x, y;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
set<Point> s;
int sum = 0;
for(int i=1; i<=n; i++) {
scanf("%d%d", &x, &y);
sum += x;
s.insert(Point(x * n, y));
}
bool flag = true;
for(set<Point>::iterator iter=s.begin(); iter != s.end(); iter++) {
if(s.find(Point(2 * sum - iter->first, iter->second)) == s.end()) {
flag = false;
break;
}
}
printf("%s\n", flag ? "YES" : "NO");
}
return 0;
}