poj 1971 Parallelogram Counting 排序 + 计数
2015-10-14 08:58
211 查看
题意:给出平面上的n个点,求出组成的平行四边形个数。
考虑平行四边形的性质,对角线互相平分。
因为做之前提示可以哈希,所以想把所有中点哈希后计数,但是这样太麻烦。直接对所有点排序后,将重复的计数t,求所有t * (t - 1) / 2的和。
考虑平行四边形的性质,对角线互相平分。
因为做之前提示可以哈希,所以想把所有中点哈希后计数,但是这样太麻烦。直接对所有点排序后,将重复的计数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; }
相关文章推荐
- CLRS 7.4快速排序分析
- 解决编码问题
- excel选择符合条件的行
- 两个队列实现一个栈
- Linux下高并发socket最大连接数所受的各种限制
- 在 CentOS 7 中安装并使用自动化工具 Ansible
- win10中安装的第三方播放器播放视频总出现颜色失真怎么办?
- Nagios各组件简述及nrpe详解
- 设置3D模型在2DUI界面中的显示,要设置所有的layer
- tomcat apr安装
- 关于UIGestureRecognizer的认识
- 如何抓取Js动态生成数据且以滚动页面方式分页的网页
- IOS中WebView的使用
- #蓝懿iOSi学习的日子#2015年10月14日
- 真不知道起什么名字了
- epub3 in action: epub3文件格式简介
- 在 CentOS 7 中安装并使用自动化工具 Ansible
- 数据库操作类
- javascript与常用正则表达式
- 咨询的奥秘