UVA1595_Symmetry

来源:互联网 发布:躬匠精神知乎 编辑:程序博客网 时间:2024/06/06 01:14

给出平面上n个点,问你能不能找到一个竖线让他们对称

这道题后面发现真的不难,又不止一种方法

我当时写的很挫,死脑筋的就找一个点的对称点存不存在,用结构体存点信息,在排序用find找,,然后不知道一堆wa

后面发现排序之后,如果位置i和n-i-1这两个点不对称就一定不存在!!!!

可以用反证法得知

////    Created by Zeroxf on 2015-08-19-15.36//    Copyright: (c) 2015 Zeroxf. All rights reserved//#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<queue>#include<cstdlib>#include<algorithm>#include<stack>#include<map>#include<queue>#include<vector>using namespace std;struct node{    int x,y;    node(int x,int y):x(x),y(y){}    bool operator < (const node& rhs)const{        return x < rhs.x||(x==rhs.x&&y<rhs.y);    }    bool operator != (const node& rhs)const{        return x != rhs.x || y != rhs.y;    }};vector<node> v;const int maxn = 1e5;long long t,n,sum,ok,mid;int x,y;int main(){    cin>>t;    while(t--){        cin>>n;        sum = 0; ok =true;v.clear();        for(int i = 0; i < n; i++){            scanf("%d%d",&x,&y);            sum += x;            v.push_back(node(x,y));        }        if((sum*2)%n != 0) ok = false;        else {            sort(v.begin(),v.end());            mid = sum *2 /n;            for(int i = 0;i < v.size(); i++){                node findv(mid-v[i].x,v[i].y);                int pos = lower_bound(v.begin(), v.end(), findv) - v.begin();                if(pos>=v.size()||v[pos]!=findv){                    ok = false;break;                }            }        }        if(ok) cout<<"YES\n";        else cout<<"NO\n";    }    return 0;}

0 0
原创粉丝点击