Symmetry CSU

来源:互联网 发布:双系统windows连不上网 编辑:程序博客网 时间:2024/05/16 11:43

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.

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
35-2 50 06 54 02 342 30 44 00 045 146 105 106 14
Sample Output
YESNO

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");    }}

原创粉丝点击