uva1595对称轴 STL模拟

来源:互联网 发布:阿里云服务器视频教程 编辑:程序博客网 时间:2024/04/30 03:19

一开始zz了没想到用set存储和查找对称点……想了想这 是哪一章的习题……emmm
The figure shown on the left is left-right symmetric as it is possible to fold the sheet of paper along a
vertical line, drawn as a dashed line, and to cut the figure into two identical halves. The figure on the
right is not left-right symmetric as it is impossible to find such a vertical line.
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

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<set>using namespace std;struct point {    double x, y;    bool operator<(const point& rhs) const {        return x - rhs.x < 0 || (x - rhs.x == 0 && y - rhs.y < 0);    }};double mid;/*struct cmp {    inline bool operator() (const point &a, const point &b) const {         return a.x < b.x||a.x==b.x&&a.y<b.y;    }};*/set<point> pe;int n,t;int readint() {    int x; cin >> x;return x;}int main() {    cin >> t;    while (t--) {        bool f = 1;        int cnt = 0;//对称轴上的点        point e[1001];        n = readint();        mid=0;        for (int i = 1; i <= n; i++)        {            e[i].x = readint(); e[i].y = readint();            pe.insert(e[i]);            mid += e[i].x;        }        sort(e + 1, e + n + 1);        mid /= n;        for (int i = 1; i <= n; i++) {            point p2;            p2.x = mid * 2 - e[i].x; p2.y = e[i].y;            if (!pe.count(p2) )                f = false;        }        if(f) printf("YES\n");        else printf("NO\n");    }    //system("pause");    return 0;}
原创粉丝点击