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. poj 2253 最短路 or 最小生成树

    Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sit ...

  2. head查询

    • must子句:文档必须匹配must查询条件:• should子句:文档应该匹配should子句查询的一个或多个:• must_not子句:文档不能匹配该查询条件:• filter子句:过滤器,文档 ...

  3. WordPress使用PHPMailer发送gmail邮件

    wordpress使用phpmailer发送gmail邮件 0.保证用于gmail账号已经开启imap服务,且你能正常访问到gmail的smtp服务.(需要climb over the wall) 1 ...

  4. python 学习爬虫教程~

    思路:: (本文没有用xpath定位,xpath需要导入第三方库   from lxml import etree) 1.首先通过urllib类获取到网页的所有内容 2.通过partition获取其中 ...

  5. kubernetes concepts -- Pod Lifecycle

    Pod Lifecycle This page describes the lifecycle of a Pod. Pod phase A Pod’s status field is a PodSta ...

  6. newSQL 到底是什么?

    数据库发展至今已经有3代了: SQL,传统关系型数据库,例如 MySQL noSQL,例如 MongoDB newSQL SQL 的问题 互联网在本世纪初开始迅速发展,互联网应用的用户规模.数据量都越 ...

  7. Java入门 - 高级教程 - 09.文档注释

    原文地址:http://www.work100.net/training/java-documentation.html 更多教程:光束云 - 免费课程 文档注释 序号 文内章节 视频 1 概述 2 ...

  8. Docker和Kubernetes

    Docker和Kubernetes Docker Docker是一个容器的开放平台,但它不是最早的.自20世纪70年代以来,容器平台一直存在.他们的开发可以追溯到Unix中的chroot系统调用.在2 ...

  9. chrome浏览器无法开启同步功能 request cancel

    解决办法 添加代理规则*.googleapis.com

  10. .net core 认证与授权(一)

    前言 .net core web并不是一个非常新的架构,很多文章提及到认证与授权这个过程,但是一般都会提及到里面的方法怎么用的,而不是模拟一个怎样的过程,所以我打算记录自己的理解. 什么是认证?我们大 ...