UVa1595

思想: 先将点按x的大小排列,再将x点相同的点按y轴大小进行排列(我用的冒泡法对称轴附近的x相同的点会排序不严格,但不会影响最终判断,正在找寻一种严格排序方式)。对于单数点,找到最中心的点作为对称点,然后从第一个点开始,看是否对应的点的横坐标与纵坐标相符合对称条件(注意如果点在对称轴上,则要判断相应点是否也在对称轴上)。对于双数点,也是先找到最中间的点,再从(a[0][0]+a[n-1][1])/2找到对称轴的x坐标(为了不使对称轴为小数,统一x乘以二),从第一个点遍历到最中间的点,看是否符合对称条件,同样,如果点在对称轴上也要判断对称点是否在对称轴上。

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

int a[1000][2];
int main()
{
    //freopen("in.txt","r",stdin);
    int place , T , n ,md ,R,MD1,x,y;
    int MD;
    scanf("%d",&T);
    start:while(T--){
      md = 0;
      scanf("%d",&n);
      for(int i = 0; i<n ; i++){
         scanf("%d%d",&x,&y);
         a[i][0] = 2*x;
          a[i][1] = y;

      }
      for(int i = 0 ; i<n-1 ; i++){
            for(int j = 0 ; j<n-i-1 ; j++){
              if(a[j][0]>a[j+1][0]){
                  place=a[j][0];
                  a[j][0] = a[j+1][0];
                  a[j+1][0] = place;
                  place = a[j][1];
                  a[j][1] = a[j+1][1];
                  a[j+1][1] = place;
               }
           }
      }
      MD1 = (n+1)/2;
      //printf("MD1=%d\n",MD1);
      for(int i = 0 ; i<MD1-1 ; i++){
           for(int j = 0 ; j<MD1-i-1 ; j++){
              if(a[j][0]==a[j+1][0]&&a[j][1]<a[j+1][1]){
                  place=a[j][0];
                  a[j][0] = a[j+1][0];
                  a[j+1][0] = place;
                  place = a[j][1];
                  a[j][1] = a[j+1][1];
                  a[j+1][1] = place;
               }
           }
      }
      for(int i = MD1 ; i<n-1 ; i++){
           for(int j = MD1  ; j<n-1 ; j++){
              if(a[j][0]==a[j+1][0]&&a[j][1]>a[j+1][1]){
                  place=a[j][0];
                  a[j][0] = a[j+1][0];
                  a[j+1][0] = place;
                  place = a[j][1];
                  a[j][1] = a[j+1][1];
                  a[j+1][1] = place;
               }
           }
      }
      /*for(int i = 0 ; i<n ; i++){
         printf("%d %d 是第%d个数字\n",a[i][0],a[i][1],i+1);
      }
      printf("\n");*/

      if(n%2>0){
          md = (n+1)/2 -1;
          for(int L = 0 ; L<md ; L++){
              R = md + (md-L);
              if(a[L][0]!=a[md][0]){
                   if( a[L][1]!=a[R][1] || a[R][0]-a[md][0]!=a[md][0]-a[L][0] ){
                         printf("NO\n");
                         goto start;
                   }
              }else{
                   if( a[R][0]!=a[md][0] ){
                         printf("NO\n");
                         goto start;
                   }
              }
           }
           printf("YES\n");
           goto start;
      }
      if(n%2==0){
           MD =( a[0][0]+a[n-1][0] )/ 2;
           md = (n+1)/2 -1;
           for(int L = 0 ; L<=md; L++){
              R = n-L-1;
              if(a[L][0]!=MD){
                   if( a[L][1]!=a[R][1] || a[R][0]-MD!=MD-a[L][0] ){
                         printf("NO\n");
                         goto start;
                   }
              }else{
                   if( a[R][0]!=MD){
                         printf("NO\n");
                         goto start;
                   }
              }
           }
           printf("YES\n");
           goto start;
      }
  }
  return 0;
}

/*
     知识点:
                        1. 弄懂sort()排序最后参数的排序方法
                              https://www.cnblogs.com/AlvinZH/p/6784862.html?utm_source=itdadao&utm_medium=referral
                              https://blog.csdn.net/leelitian3/article/details/79293058
                              快排代码:
                              https://blog.csdn.net/xdrt81y/article/details/24505859
                              sort()排序英文版
                              https://en.cppreference.com/w/cpp/algorithm/sort

                         2. UVa1595 - Symmetry 的另一种解法
                               https://www.cnblogs.com/AOQNRMGYXLMV/p/4453951.html
*/
 

猜你喜欢

转载自blog.csdn.net/m0_37632283/article/details/82118747
今日推荐