1. 程式人生 > >UVA1595 UVALive3226 Symmetry【set】

UVA1595 UVALive3226 Symmetry【set】

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

問題簡述:(略)

問題分析

  這個問題是給出平面上N(N<=1000)個點。問是否可以找到一條豎線,使得所有點左右對稱。

  如果點集存在對稱軸,則對稱軸為點集x座標和的平均值。

程式說明

  輸入是整數,為了使得計算不需要使用浮點數(使用的話可能造成不精確),則稍微做了一點數學處理。

  平均值為(x1+x2+......+xn)/n。

  若xi=(x1+x2+......+xn)/n,那麼2(x1+x2+......+xn)-nxi=nxi。

  所以程式中將座標(nxi,y)儲存在集合中,計算處理就不用浮點數了。

題記:(略)

參考連結:(略)

AC的C++語言程式如下:

/* UVA1595 UVALive3226 Symmetry */

#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> Point;

int main()
{
    int t, n, x, y;

    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);

        set<Point> s;
        int sum = 0;
        for(int i=1; i<=n; i++) {
            scanf("%d%d", &x, &y);
            sum += x;
            s.insert(Point(x * n, y));
        }

        bool flag = true;
        for(set<Point>::iterator iter=s.begin(); iter != s.end(); iter++) {
            if(s.find(Point(2 * sum - iter->first, iter->second)) == s.end()) {
                flag = false;
                break;
            }
        }

        printf("%s\n", flag ? "YES" : "NO");
    }

    return 0;
}