Description
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 ( 1N1, 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.
The following shows sample input and output for three test cases.
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
这题做的过程也是十分曲折呵呵呵 第一感觉 是用set存储 但是set不能下标访问元素 只能通过迭代器访问 用起来还不够熟悉 于是换了vector 但是vector就有两个问题 去重和排序 结构体里重载运算符<和== 先用sort排序 然后用erase unique 去重 :
struct point { int x,y; bool operator < (const point &rhs) //重载运算符 const{ return y<rhs.y||(y==rhs.y&&x<rhs.x); } bool operator == (const point &rhs) const{ return this->y==rhs.y; } }p; vector<point> po; sort(po.begin(),po.end());//排序 po.erase( unique(po.begin(), po.end() ), po.end() );//去重
这么做了之后呢 想法是按y来排序 然后在一段y相等的区间内 判断对应的x和是否相等 但是当vector里有两个元素时又出了问题 = = 然后改吧 改啊改 越改越乱 虽然自己想出的数据都过了 但是也觉得这种去 补漏洞 的方法 极大可能是过不了的 果然没过 后来受到别人的启发 应该按照x来排序 这样的话y必然是按照从大到小再到大 对称的形式来排序的 然后判断首位以及对应位置x的和是否相等即可 当然 还有个方法如下 即对x进行两次排序 一次正一次反 然后检查对应位置的x和是否相等 y是否相等。#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> P; const int maxn = 1100; struct P1 { int x, y; bool operator < (const P1& a) const { return x < a.x || (x == a.x && y > a.y); } }p1[maxn]; struct P2 { int x, y; bool operator < (const P2& a) const { return x > a.x || (x == a.x && y > a.y); } }p2[maxn]; int main() { int T; scanf("%d", &T); while(T--) { int n; scanf("%d", &n); for(int i=0; i<n; i++) { scanf("%d%d",&p1[i].x,&p1[i].y); p2[i].x = p1[i].x; p2[i].y = p1[i].y; } sort(p1,p1+n); sort(p2,p2+n); int flag=1; int sum=p1[0].x + p2[0].x; for(int i=0;i<n/2+1; i++) { if(p1[i].x + p2[i].x != sum || p1[i].y != p2[i].y) { flag=0; break; } } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }