习题5-6 对称轴 UVa1595

1.题目描述:点击打开链接

2.解题思路:判断是否对称只需要看x坐标,因此可以先将每个y值分配一个ID;先判断是否自对称,如果都是,进一步判断所有自对称的对称轴是否都相同,注意用对称轴坐标的二倍来判断,尽量避免浮点数的使用。

3.代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<cassert>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<functional>
using namespace std;
const int maxn = 1000 + 10;
vector<int>dot[maxn];
vector<int>arr;
map<int,int>yid;
int sym[maxn];
int ID(int y)
{
	if (yid.count(y))
		return yid[y];
	else
	{
		arr.push_back(y);
		yid[y] = arr.size() - 1;
		return yid[y];
	}
}
bool is_sym(int p)
{
	int len = dot[p].size();
	if (len == 1 || len == 2) return true;
	else if (len % 2)
	{
		int mid = len / 2;
		int x = dot[p][mid] * 2;
		for (int i = 0; i < mid;i++)
		if (dot[p][i] + dot[p][len - i - 1] != x)
			return false;
		return true;
	}
	else
	{
		int mid = len / 2;
		int x = dot[p][0] + dot[p][len - 1];
		for (int i = 0; i < mid;i++)
		if (dot[p][i] + dot[p][len - 1 - i] != x)
			return false;
		return true;
	}
}

int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int n;
		int flag = 1;
		memset(sym, 0, sizeof(sym));
		arr.clear();
		yid.clear();
		for (int i = 0; i < maxn; i++)
			dot[i].clear();
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			int x, y;
			cin >> x >> y;
			dot[ID(y)].push_back(x);
		}
		int len = arr.size();
		for (int i = 0; i < len; i++)
			sort(dot[i].begin(), dot[i].end());
		for (int i = 0; i < len;i++)
		if (!is_sym(i))
		{
			flag = 0;
			break;
		}
		else
			sym[i] = dot[i][0] + dot[i][dot[i].size() - 1];
		if (flag)
		{
			int x = sym[0];
			for (int i = 0; i < len;i++)
			if (sym[i] != x)
			{
				flag = 0;
				break;
			}
		}
		if (flag)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}

你可能感兴趣的:(uva)