Symmetry(对称轴)UVA 1595

来源:互联网 发布:人工智能顶级会议论文 编辑:程序博客网 时间:2024/04/30 03:40

解题思路:本题的突破点在于通过最左边和最右边的点寻找对称轴,我的想法是利用set集合排序(由于用了结构体,故要重写<运算符),此时set集合中为从小到大,因为最右边x相同的点可能有多个(同一条轴上,但是最小的点是固定的),所有通过迭代器(last)向前遍历,直到y相同的点求得对称轴;接下来就好办了,使用迭代器遍历set集合中所有元素(因为是对称的,所以只需要找到n的一半对数就可以了(用count记数))。

  1. #include<cstdio>
  2. #include<set>
  3. using namespace std;
  4. struct note{
  5.     int x,y;
  6.     bool operator <(const note &n) const {
  7.     return x<n.x || x==n.x&&y<n.y;        //set集合中用了结构体必须重写<运算符
  8.     }
  9. };
  10. set<note> set1;
  11. int main(){
  12.     int T;
  13.     scanf("%d",&T);
  14.     while(T--){
  15.         int n;
  16.         scanf("%d",&n);
  17.         note temp;
  18.         for(int i=0;i<n;i++)
  19.         {
  20.             scanf("%d%d",&temp.x,&temp.y);
  21.             set1.insert(temp);
  22.         }
  23.         int flag=1;
  24.         set<note>::iterator first=set1.begin(),last=set1.end();
  25.         last--;
  26.         double sym=0;
  27.         while(true){                      //此循环用于寻找对称轴
  28.         if((*last).y != (*first).y)last--;
  29.         else {
  30.         sym=((*first).x+(*last).x)/(double)2;              
  31.         break;}
  32.         }
  33.         int count1=0;
  34.         for(;first!=set1.end();first++){
  35.             note nt=*first;
  36.             nt.x=(int)(2*sym)-nt.x;                
  37.             if(set1.count(nt))count1++;
  38.             else {flag=0;
  39.                 break;}
  40.             if(count1>= n/2+1)break;
  41.         }
  42.         if(flag)printf("YES\n");
  43.         else printf("NO\n");
  44.         set1.clear();
  45.     }
  46.     return 0;
  47. }

0 0
原创粉丝点击