您的位置:首页 > 其它

Facebook Hacker Cup 2016 Qualification Round第一题

2016-09-14 20:40 218 查看
这个,题意就是,给一个图,让你找,两条线段的端点是一个,并且长度相同,这个为一组,然后就是找,有多少组这样的星系

题解这个就事这么回事,依次找距离,然后排序,排序就是找相同的距离的有cnt个,然后(cnt-1)*cnt就是得数,关键在于想到用排序,我一开始想的是map,但是不行。。。。。

会tle

最近好久没有刷题了,因为数学建模,希望我有一天能有足够的能力来拥抱爱我和我爱的人

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int MaxN = 2000 + 5;

struct node{
int x , y;
}a[MaxN];
double dis[MaxN * MaxN];
long long ans = 0;
double Dis(int i , int j)
{
double x = (a[i].x - a[j].x) * (a[i].x - a[j].x);
double y = (a[i].y - a[j].y) * (a[i].y - a[j].y);
return sqrt(x + y);
}
int main()
{
int t , cnt = 1;
scanf("%d", &t);
while(t--){
ans = 0;
int n;
scanf("%d",&n);
for(int i = 1 ; i <= n ; i++){
scanf("%d %d",&a[i].x , &a[i].y);
}
for(int i = 1 ; i <= n ; i++){
//  printf("*");
int tot = 0;
for(int j = 1 ; j <= n ; j++){
if(i != j) dis[++tot] = Dis(i , j);
}
sort(dis + 1 , dis + tot + 1);
int cnt = 0;dis[0] = dis[1] ,dis[tot + 1] = -1;//计算后面的,前面那句是为了把cnt = 1;很巧妙
for(int j = 1 ; j <= tot + 1 ; j++){
if(dis[j] != dis[j - 1]) ans += cnt*(cnt - 1) / 2 , cnt = 1;
else cnt++;
}
}
printf("Case #%d: ",cnt++);
printf("%lld\n",ans);
}
}
/*
5
3
0 0
0 1
0 3
5
0 0
0 1
0 2
0 3
0 4
4
0 0
0 100
100 0
100 100
4
0 0
-3 4
0 5
-5 0
6
5 6
6 5
7 6
6 7
7 8
8 7
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: