Symmetry CSU - 1896
We call a figure made of points is left-right symmetric as it is possible to fold the sheet of paper along a vertical line and to cut the figure into two identical halves.For example, if a figure exists five points, which respectively are (-2,5),(0,0),(2,3),(4,0),(6,5). Then we can find a vertical line x = 2 to satisfy this condition. But in another figure which are (0,0),(2,0),(2,2),(4,2), we can not find a vertical line to make this figure left-right symmetric.Write a program that determines whether a figure, drawn with dots, is left-right symmetric or not. The dots are all distinct.
InputThe 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.
OutputPrint exactly one line for each test case. The line should contain 'YES' if the figure is left-right symmetric,and 'NO', otherwise.
Sample Input3 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 14Sample Output
YES NO
YES
这个题意思是给你一些点的坐标,问你这些点是否关于其中一个点对称。做的时候再纠结怎么一次排序才好,看了别人的思路才知道其实排两次就好。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
struct X
{
int a,b;
};
X x1[1003],x2[1003];
bool cmp(X m,X n)
{
if(m.a==n.a)
return m.b>n.b;
return m.a<n.a;
}
bool cmpp(X m,X n)
{
if(m.a==n.a)
return m.b>n.b;
return m.a>n.a;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(x1,0,sizeof(x1));
memset(x2,0,sizeof(x2));
int n,A=10001,B=-10001;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d%d",&x1[i].a,&x1[i].b);
x2[i].a=x1[i].a;
x2[i].b=x1[i].b;
A=min(A,x1[i].a);
B=max(B,x1[i].a);
}
if(A==B)
{
printf("YES\n");
continue;
}
int flag=0;
sort(x1+1,x1+n+1,cmp);
sort(x2+1,x2+n+1,cmpp);
for(int i=1; i<=n; i++)
{
//cout<<x1[i].a<<x2[i].a<<x1[i].b<<x2[i].b<<endl;;
if(x1[i].a+x2[i].a!=(A+B))
{
flag=1;
break;
}
if(x1[i].b!=x2[i].b)
{
flag=1;
break;
}
}
if(flag==1)
printf("NO\n");
else
printf("YES\n");
}
}

浙公网安备 33010602011771号