Uva1595 axis of symmetry

Uva 1595 symmetry axis

Subject description:

N given points in the plane, and asked whether to find a vertical line, so that all point-symmetrical around.

Ideas:

This problem is all my idea height (y) averaging the same point each coordinate x height, if the total number N is an even number, so long as the same as the average height of each of x, can find a symmetric axis, and N is an odd number, it is necessary to find the same based on the average of the median point of the coordinate x of all, if the same average and median, it can be.

When I wanted to do some complicated conditions, some pay more useless judgment. Two sets of test data provided herein, by reference.

2
4
-1 0
1 0
0 0 
0 -1
5
0 -1
0 10000
0 -10000
0 2
0 5
Code:
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int T;
    //freopen("uva1595_in.txt", "r", stdin);
    //freopen("uva1595_out.txt", "w", stdout);
    cin >> T;
    while(T--){
        int n, N;
        cin >> n; N = n;
        int x, y;
        map<int, long long> height;
        map<int, float> hnums;
        vector<int> xvec;
        while(n--){
            cin >> x >> y;
            xvec.push_back(x);
            if(!height.count(y)) height[y] = x;
            else height[y] += x;
            if(!hnums.count(y)) hnums[y] = 1;
            else hnums[y] = hnums[y] + 1.0;
        }
        vector<float> means;
        int failed = 0;
        for(auto it = height.begin(); it != height.end(); ++it){
            float avrg = (*it).second / hnums[(*it).first];
            means.push_back(avrg);
        }
        
        for(int i = 0; i < means.size()-1; ++i){
            if( means[i] != means[i+1]){
                failed = 1;
                break;
            } 
        }       
        float avgr = means[0];
        if(!failed && N%2){
            sort(xvec.begin(), xvec.end());
            int mid = xvec[N/2];
            if(mid*2 != (int)(avgr*2)) failed = 1;  
        }
        int i;
        for(i = 0; i < xvec.size()-1; ++i){
            if(xvec[i] != xvec[i+1]) break;
        }
        if(i == xvec.size()-1) failed = 0; 
        if(!failed) cout << "YES\n";
        else cout << "NO\n";
        
    }
}
PS:

In addition, I also see two online methods than the above me this simple lot.

One is to find the maximum and minimum x two points, taking them to the midpoint as the axis of symmetry, as if the entire graphics symmetrical, so the midpoint of the shaft must point out two of the most symmetrical. Then for each point of traverse, to see if a point-symmetrical thereto.

See link: https://blog.csdn.net/GuoZLH/article/details/53141335

Second, direct all points x size press are arranged in ascending and descending order once, then sequentially to see point1[i].x + point2[i].xwhether the axis twice, and then see if y can be the same. Axis and a consistent method of approach. This method should be the most ingenious.

See link: https://blog.csdn.net/Amateur_DP/article/details/81255412

Guess you like

Origin www.cnblogs.com/patrolli/p/11291723.html