1058 - Parallelogram Counting

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

Input starts with an integer T (≤ 15), denoting the number of test cases.

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

For each case, print the case number and the number of parallelograms that can be formed.

Sample Input

Output for 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

Case 1: 5

Case 2: 6

分析:学了这么多年数学,然而只知道平行四边形对角线交于一点,却没想到只要存在两点的中点与另两点的中点相同,就能构成平行四边形。

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
#define N 510000

struct node
{
int x;
int y;
}arr[N], mid[N];
bool cmp(struct node a, struct node b)
{
if(a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
int main(void)
{
int T, cas, n;

scanf("%d", &T);
cas = 0;

while(T--)
{
cas++;
memset(mid, 0, sizeof(mid));

scanf("%d", &n);

for(int i = 0; i < n; i++)
{
scanf("%d%d", &arr[i].x, &arr[i].y);
}
int num = 0;

for(int i = 0; i < n-1; i++)
{
for(int j = i+1; j < n; j++)
{
mid[num].x = arr[i].x + arr[j].x;
mid[num].y = arr[i].y + arr[j].y;
num++;
}
}
sort(mid, mid+num, cmp);/// 排序是为了方便下面比较
int cnt = 1;
int ans = 0;
int flag = 0;

for(int i = 1; i < num; i++)
{
if(mid[i].x == mid[flag].x && mid[i].y == mid[flag].y)
{
cnt++;
}
else
{
ans += cnt * (cnt - 1) / 2;///计算就是组合数:从 n个数里面取出 2个数 ,就是 C(n,2)
cnt = 1;
flag = i;
}
}
if(cnt>1)
ans += (cnt - 1) * cnt / 2; /// 判断循环的最后一组数据,如果也存在相同的点,就加上
printf("Case %d: %d\n", cas, ans);
}
return 0;
}

1058 - Parallelogram Counting 计算几何的更多相关文章

  1. LightOJ - 1058 - Parallelogram Counting(数学,计算几何)

    链接: https://vjudge.net/problem/LightOJ-1058 题意: There are n distinct points in the plane, given by t ...

  2. Light OJ - 1058 Parallelogram Counting(判定平行四边形)

    Description There are n distinct points in the plane, given by their integer coordinates. Find the n ...

  3. LightOJ 1058 - Parallelogram Counting 几何思维

    http://www.lightoj.com/volume_showproblem.php?problem=1058 题意:给你顶点,问能够成多少个平行四边形. 思路:开始想使用长度来扫描有多少根,但 ...

  4. Parallelogram Counting(平行四边形个数,思维转化)

    1058 - Parallelogram Counting    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...

  5. 计算几何 + 统计 --- Parallelogram Counting

    Parallelogram Counting Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5749   Accepted: ...

  6. POJ 1971 Parallelogram Counting (Hash)

          Parallelogram Counting Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6895   Acc ...

  7. POJ 1971 Parallelogram Counting

    题目链接: http://poj.org/problem?id=1971 题意: 二维空间给n个任意三点不共线的坐标,问这些点能够组成多少个不同的平行四边形. 题解: 使用的平行四边形的判断条件:对角 ...

  8. POJ 1791 Parallelogram Counting(求平行四边形数量)

    Description There are n distinct points in the plane, given by their integer coordinates. Find the n ...

  9. POJ 1971-Parallelogram Counting,暴力1063ms!

    Parallelogram Counting 刚学hash还不会用,看到5000ms的时限于是想着暴力来一发应该可以过.以前做过类似的题,求平行四边形个数,好像是在CF上做的,但忘了时限是多少了,方法 ...

随机推荐

  1. python爬虫——scrapy的使用

    本文中的知识点: 安装scrapy scrapy的基础教程 scrapy使用代理 安装scrapy 由于小哥的系统是win7,所以以下的演示是基于windows系统.linux系统的话,其实命令都一样 ...

  2. NameError:name ‘xrange’ is not defined

    原因: 在Python 3中,range()与xrange()合并为range( ). 我的python版本为python3.5. 解决办法: 将xrange( )函数全部换为range( ).

  3. MySQL 行列相互转换

    行列相互转换 /*创建表*/ CREATE TABLE ic ( NAME ), Product ), amount INT ); INSERT INTO ic VALUES (), (), (), ...

  4. 通过示例学习rholang(上部:课程0-6)

    通过例子和实践来学习rho语言.下面的例子和练习都很值得去运行.阅读.修改和完善.修改练习和教程中任何你感到有意思的代码,这样能够获得最好的学习效果.该教程包含了rho语言最常见以及最重要的特性,足以 ...

  5. CQBZOJ 邮递员(直播剪枝技巧)

    题目描述 Mirko在一个山镇找到了一份邮递员的工作.这个镇可以看作一个N*N的矩形.每个区域可能是以下之一:房子K,邮政局P,草地 '.'.每个区域都有一个海拔. 每天早上,Mirko要送信给镇上所 ...

  6. GP工作室-团队项目Beta冲刺

    GP工作室-团队项目Beta冲刺 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/ 这个作业要求 ...

  7. 文艺平衡树(区间splay)

    文艺平衡树(luogu) Description 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列. 其中需要提供以下操作:翻转一个区间,例如原有序序列是 5\ 4\ 3\ 2\ ...

  8. Ganglia安装及配置

    Ganglia安装及配置root用户下yum安装所依赖的环境(实际生产环境都已安装)yum -y install apr-develapr-util check-devel cairo-devel p ...

  9. python下的selenium和chrome driver的安装

    selenium是一款支持多种语言.多种浏览器.多个平台的开源web自动化测试软件,测试人员可用python.java等语言编写自动化脚本,使得浏览器可以完全按照你的指令运行,大大节省了测试人员用鼠标 ...

  10. SpringMVC项目遇到406问题

    今天在写功能的时候,页面突然报出406错误,所有的静态资源都访问不到了 出现这样的原因是由于我Controller层的@RequestMapping没有加上地址,导致springmvc出错,修改过来页 ...