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 计算几何的更多相关文章
- LightOJ - 1058 - Parallelogram Counting(数学,计算几何)
链接: https://vjudge.net/problem/LightOJ-1058 题意: There are n distinct points in the plane, given by t ...
- Light OJ - 1058 Parallelogram Counting(判定平行四边形)
Description There are n distinct points in the plane, given by their integer coordinates. Find the n ...
- LightOJ 1058 - Parallelogram Counting 几何思维
http://www.lightoj.com/volume_showproblem.php?problem=1058 题意:给你顶点,问能够成多少个平行四边形. 思路:开始想使用长度来扫描有多少根,但 ...
- Parallelogram Counting(平行四边形个数,思维转化)
1058 - Parallelogram Counting PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...
- 计算几何 + 统计 --- Parallelogram Counting
Parallelogram Counting Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5749 Accepted: ...
- POJ 1971 Parallelogram Counting (Hash)
Parallelogram Counting Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6895 Acc ...
- POJ 1971 Parallelogram Counting
题目链接: http://poj.org/problem?id=1971 题意: 二维空间给n个任意三点不共线的坐标,问这些点能够组成多少个不同的平行四边形. 题解: 使用的平行四边形的判断条件:对角 ...
- POJ 1791 Parallelogram Counting(求平行四边形数量)
Description There are n distinct points in the plane, given by their integer coordinates. Find the n ...
- POJ 1971-Parallelogram Counting,暴力1063ms!
Parallelogram Counting 刚学hash还不会用,看到5000ms的时限于是想着暴力来一发应该可以过.以前做过类似的题,求平行四边形个数,好像是在CF上做的,但忘了时限是多少了,方法 ...
随机推荐
- C#反射与特性(八):反射操作的示例大全
目录 1,InvokeMember 1.1 InvokeMember 参数 1.2 实践使用 InvokeMember 和成员的重载方法 微信平台,此文仅授权<NCC 开源社区>订阅号发布 ...
- java面试题-spring篇
这次是关于spring的面试题,和上次一样依旧挑了几个具有代表性的. 一. 谈谈你对 Spring 的理解 Spring 是一个开源框架,为简化企业级应用开发而生.Spring 可以是使简单的 Ja ...
- error while loading shared libraries: libevent-2.1.so.6 的解决办法
执行 memcached 启动命令时,报错,提示:error while loading shared libraries: libevent-2.1.so.6: cannot open shared ...
- 分析一下 原型模式的 UML 类图 。 复制对象, 深浅拷贝 月经贴 ,请回避
- unity调试native c/c++ dll
最近使用xlua,需要添加自定义的c lua库.研究了一下unity调试native c/c++ dll.方法如下: 通过Unity打开VS工程 VS菜单栏[工具]-> [选项] 在选项对话框中 ...
- 使用Jenkins持续集成
本篇文章主要说明的是如何使用Jenkins持续集成自己的代码. 1.Jenkins的安装与配置 使用Jenkins之前需要安装和配置Jenkins,具体安装和配置方法参照这个博客:http://www ...
- Java lambda 表达式常用示例
实体类 package com.lkb.java_lambda.dto; import lombok.Data; /** * @program: java_lambda * @description: ...
- Nginx核心模块
error_log 语法:error_log file [ debug | info | notice | warn | error | crit ]默认值:${prefix}/logs/error. ...
- qq机器人 python实现 自动回复
我以前写的代码我现在贴在了下面,下面的连接是我自己的博客,有问题希望大家提出来,一起进步...我以前试过,没啥问题.可以实现聊天. https://realwuxiong.github.io/blog ...
- Docker基础内容之镜像
概念 镜像是一个包含程序运行必要依赖环境和代码的只读文件,它采用分层的文件系统,将每一次改变以读写层的形式增加到原来的只读文件上.镜像是容器运行的基石. 下图展示的是Docker镜像的系统结构.其中, ...