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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章