Symmetry UVA - 1595

题目链接:Symmetry UVA - 1595

题目:给出平面上N(N <= 1000)个点,问是否可以找到一条竖线,使得所有点左右对称。如图:
这里写图片描述

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

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<iomanip>
#include<set>
#include<sstream>
#define ll long long

using namespace std;

struct note{
    int x,y;
    bool operator <(const note &n) const {
    return x<n.x || x==n.x&&y<n.y;        //set集合中用了结构体必须重写<运算符
    }
};

set<note> set1;


int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        note temp;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&temp.x,&temp.y);
            set1.insert(temp);
        }
        int flag=1;
        set<note>::iterator first=set1.begin(),last=set1.end();
        last--;
        double sym=0;
        while(true){                      //此循环用于寻找对称轴
        if((*last).y != (*first).y)last--;
        else {
        sym=((*first).x+(*last).x)/(double)2;              
        break;}
        }
        int count1=0;
        for(;first!=set1.end();first++){
            note nt=*first;
            nt.x=(int)(2*sym)-nt.x;                
            if(set1.count(nt))count1++;
            else {flag=0;
                break;}
            if(count1>= n/2+1)break;
        }
        if(flag)printf("YES\n");
        else printf("NO\n");
        set1.clear();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值