计算几何 + 统计 --- Parallelogram Counting
Parallelogram Counting
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 5749 | Accepted: 1934 |
Description
There are n distinct points in the plane, given by their integer coordinates. Find the number of parallelograms whose vertices lie on these points. In other words, find the number of 4-element subsets of these points that can be written as {A, B, C, D} such that AB || CD, and BC || AD. No four points are in a straight line.
Input
The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases. It is followed by the input data for each test case.
The first line of each test case contains an integer n (1 <= n <= 1000). Each of the next n lines, contains 2 space-separated integers x and y (the coordinates of a point) with magnitude (absolute value) of no more than 1000000000.
The first line of each test case contains an integer n (1 <= n <= 1000). Each of the next n lines, contains 2 space-separated integers x and y (the coordinates of a point) with magnitude (absolute value) of no more than 1000000000.
Output
Output should contain t lines.
Line i contains an integer showing the number of the parallelograms as described above for test case i.
Line i contains an integer showing the number of the parallelograms as described above for test case i.
Sample Input
2 6 0 0 2 0 4 0 1 1 3 1 5 1 7 -2 -1 8 9 5 7 1 1 4 8 2 0 9 8
Sample Output
5 6
【题目来源】
Tehran Sharif 2004 Preliminary
http://poj.org/problem?id=1971
【题目大意】
在平面上有n个不同的点,用整数坐标给出。在这些点中寻找平行四边形的数目,也就是找到{A,B,C,D}这样的子集,这些子集满足条件:AB平行于CD,AC平行于BD。不会出现四个点都在一条直线上的情况。输出这样的平行四边形的个数。
【题目分析】
根据平行四边形的性质:对角线互相平分,即:AC与BD有共同的中点。首先求出任意两个点的中点坐标,然后进行排序,最后进行统计。
AC代码:
#include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> #include<bitset> #define MAX 1010 using namespace std; struct Node { int x; int y; }; Node node[MAX]; Node mid[500000]; bool cmp(Node a,Node b) { if(a.x==b.x) return a.y<b.y; return a.x<b.x; } int main() { int T; cin>>T; while(T--) { int n; cin>>n; int i,j,k; for(i=0;i<n;i++) scanf("%d%d",&node[i].x,&node[i].y); int m=0; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { mid[m].x=node[i].x+node[j].x; mid[m].y=node[i].y+node[j].y; m++; } } sort(mid,mid+m,cmp); int cnt=0; for(i=0;i<m;i++) { for(j=i+1;j<m;j++) { if(mid[i].x==mid[j].x&&mid[i].y==mid[j].y) cnt++; else break; } } printf("%d\n",cnt); } return 0; }
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3728741.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?