UVa1595 - Symmetry

基本思路:先找出一组数据的对称轴,再得到每个横坐标到对称轴的距离,再比较距离相等的点的纵坐标。

//#define LOCAL
#include<iostream>
#include<array>
#include<queue>
#include<set>
#include<vector>
#include<map>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<algorithm>
using namespace std;
 
typedef pair<int, int> P ;
P p[1005];
int a[1005];
map<int, int> M;

int main(){
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int T;
	cin >> T;
	int m, Sym;
	int flag = 0;
	while (T--){
		flag = 0;
		M.clear();
		memset(a, 0, sizeof(a));
		memset(p, 0, sizeof(p));
		cin >> m;
		for (int i = 0; i < m; i++){
			cin >> p[i].first >> p[i].second;
			a[i] = p[i].first;
			M[p[i].second]++;
		}
		if (M[p[0].second] == 1) {
			Sym = p[0].first;
		}
		else{
			int minn = 10005, maxn =-10005;
			for (int i = 0; i < m; i++){
				if (p[0].second == p[i].second){
					maxn = max(maxn, p[i].first);
					minn = min(minn, p[i].first);
				}
			}
			Sym = maxn + minn;
			if (Sym % 2 == 0) Sym = Sym / 2;
			else{ flag = 1; Sym = Sym / 2; }
		}
		//现在得到了一组数据的对称轴Sym
		if (!flag){
			for (int i = 0; i < m; i++){
			 a[i] = abs(a[i] - Sym);				
			}
		}
		else {
			if (m % 2 != 0){
				cout << "NO" << "\n";
				continue;
			}
			for (int i = 0; i < m; i++){
				if (a[i]>Sym) a[i] = a[i] - Sym-1;
				else a[i] = Sym - a[i];
			}
		}
		int flag1 = 0;
		for (int i = 0; i < m; i++){			
			if (Sym == p[i].first&&!flag) { a[i]=10005; continue; }			
			for (int j = i + 1; j < m; j++){
				if (a[i] == a[j]&&p[i].second==p[j].second){
					a[i] = a[j] = 10005; break;
				}
			}
		}
		int i = 0;
		for ( i;i<m;i++){
			if (a[i]!=10005) break;
		}
		if (i == m) flag1 = 1;
		if (!flag1) cout << "NO" << "\n";
		else cout << "YES" << "\n";
 	}
		//	printf("Time used=%.3f\n", (double)clock() / CLOCKS_PER_SEC);
	return 0;
}

你可能感兴趣的:(uva,刘汝佳,算法竞赛入门经典第二版,C++与STL入门)