题意:给出一个点集,问这个集合有没有中心点使点集对称,这个点可以是点集中的点也可以不是点集的点。

解法:一开始我枚举每两个点连线的中点……结果T了orz当时也不知道怎么想的……

将点按横坐标排序,如果点集有中心点则中心点一定是排序后排在中间的那个点(n为奇数)或者中间两个点的连线中点(n为偶数),然后判断一下是不是中心点即可。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
struct node
{
int x, y;
bool operator < (const node& tmp) const
{
if(x == tmp.x)
return tmp.y < y;
return tmp.x < x;
}
} point[];
map <node, int> m;//打一个点的表
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
m.clear();
int n;
scanf("%d", &n);
for(int i = ; i < n; i++)
{
scanf("%d%d", &point[i].x, &point[i].y);
m.insert(pair <node, int> (point[i], ));
}
int ans = ;
sort(point, point + n);
node center;
if(n & )
{
center.x = point[n / ].x * ;
center.y = point[n / ].y * ;
}
else
{
center.x = point[n / - ].x + point[n / ].x;
center.y = point[n / - ].y + point[n / ].y;
}
for(int k = ; k < n; k++)//判断是否每个点都能找到对应对称点
{
node tmp;
tmp.x = center.x - point[k].x;
tmp.y = center.y - point[k].y;
if(m.find(tmp) == m.end())
{
ans = ;
break;
}
}
if(ans)
printf("yes\n");
else
printf("no\n");
}
return ;
}
05-08 15:14