这道题当时没做出来比较可惜,当时发现这道题的时候已经有点晚了,后来跟别人交流了一下,感觉也不算难,要是早点发现的话可能是可以做出来的。
这道题看起来是有点难的样子,但其实是个纸老虎。
我们直接贪心枚举就可以了:
n个点,每个点都与其它所有的点(包括它自己)连线,连成的线段中都会生成一个中点,然后我们从这些中点当中找出出现次数最多的那个就行,设它的出现次数为num,则答案就为n-num。
代码如下:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<map> //用map存中点,会比较方便
using namespace std;
typedef long long ll;
const int maxn=1e3+5;
const int inf=0x3f3f3f3f;
struct point
{
int x,y;
bool operator < (const point& a) const
{
if(a.x!=x)
return x>a.x;
return y>a.y;
};
}p[maxn];
map<point,int> mp;
main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d%d",&p[i].x,&p[i].y);
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j)
{
point temp;
temp.x=p[i].x+p[j].x;
temp.y=p[i].y+p[j].y;
if(mp.find(temp)==mp.end())
mp[temp]=1;
else mp[temp]++;
}
}
int ma=0;
for(map<point,int>:: iterator it=mp.begin();it!=mp.end();++it)
{
int num=(*it).second;
ma=max(ma,num);
}
printf("%d\n",n-ma);
return 0;
}
PS:这道题也可以不用map去做,直接另开一个数组记录所有的中点,然后排个序,记录下重复次数最多的点的出现个数就好了,这样做会快很多。题目时间要求是2000MS,map是1600MS,用这种方法应该可以1000MS以内,但是比较容易出错(本人试了一次)。所以在时间允许的前提下,还是采用一些比较稳的做法比较好。