您的位置:首页 > 其它

poj 1971 Parallelogram Counting 排序 + 计数

2015-10-14 08:58 211 查看
题意:给出平面上的n个点,求出组成的平行四边形个数。

考虑平行四边形的性质,对角线互相平分。

因为做之前提示可以哈希,所以想把所有中点哈希后计数,但是这样太麻烦。直接对所有点排序后,将重复的计数t,求所有t * (t - 1) / 2的和。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int maxn = 1010;
const int mod = 1e6 + 3;
const double eps = 1e-8;

int t, n;
int hash[mod];
int next[maxn];

struct Node {
double x, y;
} dot[maxn];

Node node[maxn * maxn / 2];
int cnt;
/*
inline int Hash(int x, int y) {
return ((ll)x * x + (ll)y * y) % mod;
}

inline void hashit(int i, int x, int y) {
int key = Hash(x, y);
if (hash[key] == -1) {
hash[key] = i;
next[i] = -1;
}
else {
int j = hash[key];
next[i] = next[j];
next[j] = i;
}
}
*/
bool cmp(Node n1, Node n2) {
return n1.x < n2.x || fabs(n1.x - n2.x) < eps && n1.y < n2.y;
}

int main() {
scanf("%d", &t);
while (t--) {
cnt = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%lf%lf", &dot[i].x, &dot[i].y);
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
node[cnt].x = (dot[i].x + dot[j].x) / 2;
node[cnt].y = (dot[i].y + dot[j].y) / 2;
cnt++;
}
sort(node, node + cnt, cmp);
int ans = 0, tem = 1;
for (int i = 1; i < cnt; i++) {
if (fabs(node[i].x - node[i - 1].x) < eps && fabs(node[i].y - node[i - 1].y) < eps) {
tem++;
}
else {
ans += (tem - 1) * tem / 2;
tem = 1;
}
}
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: