中南大学第十一届大学生程序设计竞赛-COJ1896-Symmetry

1896: Symmetry

Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 2 Solved: 2
Description
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
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
Hint
Source
中南大学第十一届大学生程序设计竞赛

题目大意:给定二维平面上的n个点,判断这n个点是否左右对称,不需要考虑旋转对称的情况。
解题思路:先确定图的对称轴,将点分别从左到右和从右到左排序,检查对应位置上的点是否对称即可。
考查内容:排序函数的使用
时间复杂度: O(nlogn)
题目难度:★★

#include
#include
using namespace std;

struct node
{
    int x,y;
}a[1005],b[1005];

bool cmp1(node u,node v)
{
    return u.x==v.x?u.ybool cmp2(node u,node v)
{
    return u.x==v.x?u.yv.x;
}

int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i].x>>a[i].y;
            b[i].x=a[i].x;
            b[i].y=a[i].y;
        }
        sort(a+1,a+n+1,cmp1);
        sort(b+1,b+n+1,cmp2);
        int sym=a[1].x+b[1].x;
        bool flag=true;
        for(int i=1;i<=n;i++)
        {
            if(a[i].x+b[i].x!=sym||a[i].y!=b[i].y)
            {
                flag=false;
                break;
            }
        }
        if(flag) cout<<"YES"<else cout<<"NO"<return 0;
}

你可能感兴趣的:(数据结构)