Light OJ - 1058 Parallelogram Counting(判定平行四边形)
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
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, contains2 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 - - Sample Output
Case :
Case : 6
题目链接:http://lightoj.com/volume_showproblem.php?problem=1058
*******************************************************
题意:给出T组实例,每组实例给出n各个点的坐标,判断能后成多少个平行四边形。
分析:使用向量法判断,只要满足x1+x3=x2+x4,y1+y3=y2+y4,则说明可以构成平行四边行
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std; #define N 1200 struct node
{
int x,y;
}p[N]; node q[]; 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 ,kk=,i,j,n; scanf("%d", &T); while(T--)
{
scanf("%d", &n); for(i=;i<n;i++)
scanf("%d%d", &p[i].x,&p[i].y); int u=;
for(i=;i<n-;i++)///保存好向量判断要用到的值
{
for(j=i+;j<n;j++)
{
q[u].x=p[i].x+p[j].x;
q[u++].y=p[i].y+p[j].y;
}
} sort(q,q+u,cmp); int ans=,f=,sum=;
for(i=;i<u;i++)///使用向量法判定是否满足为平行四边形
{
if(q[i].x==q[f].x&&q[i].y==q[f].y)
sum++;
else
{
f=i;///
ans+=sum*(sum-)/;///可以构成平行四边形的数目
sum=;
}
} printf("Case %d: %d\n",kk++,ans);
}
return ;
}
附上一个RE的代码:(使用的斜率和b值判断的)指出一些错误和注意事项
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std; #define N 12000
#define INF 0x3f3f3f3f struct node
{
int x,y;
}p[N]; struct no
{
double k,l,b;
}q[N];///应为q[500000],对于储存统计比较条件的这个数组一定要开的更大,依旧为N的话RE无止境。。。 double Len(node a,node b)
{
double x=b.x-a.x;
double y=b.y-a.y;
double len=sqrt(x*x+y*y);
return len;
} int main()
{
int T ,kk=,i,j,n; scanf("%d", &T); while(T--)
{
scanf("%d", &n); memset(p,,sizeof(p));
memset(q,,sizeof(q)); for(i=;i<n;i++)
scanf("%d%d", &p[i].x,&p[i].y); int u=;
for(i=;i<n-;i++)
{
for(j=i+;j<n;j++)
{
if(p[j].x-p[i].x!=)
{
q[u].k=1.0*(p[j].y-p[i].y)/(p[j].x-p[i].x);
q[u].b=p[i].y-q[u].k*p[i].x;
q[u++].l=Len(p[i],p[j]);
}
else
{
q[u].k=INF;
q[u].b=p[i].x;
q[u++].l=Len(p[i],p[j]);
}
}
} int ans=;
for(i=;i<u-;i++)///上面的N改了之后就知道这双重for提交,TLE妥妥的,所以这里思路必须得换一下
{
for(j=i+;j<u;j++)///然后我就直接改方法了,也不知道这个路子能被改出正确答案不%>_<%
{
if((q[i].k==q[j].k)&&(q[i].l==q[j].l)&&q[i].b != q[j].b)
ans++;
}
} printf("Case %d: %d\n",kk++,ans/);///这里显然也天真了
}
return ;
}
Light OJ - 1058 Parallelogram Counting(判定平行四边形)的更多相关文章
- Parallelogram Counting(平行四边形个数,思维转化)
1058 - Parallelogram Counting PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...
- 1058 - Parallelogram Counting 计算几何
1058 - Parallelogram Counting There are n distinct points in the plane, given by their integer coord ...
- LightOJ 1058 - Parallelogram Counting 几何思维
http://www.lightoj.com/volume_showproblem.php?problem=1058 题意:给你顶点,问能够成多少个平行四边形. 思路:开始想使用长度来扫描有多少根,但 ...
- light oj 1148 - Mad Counting
1148 - Mad Counting PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB M ...
- LightOJ - 1058 - Parallelogram Counting(数学,计算几何)
链接: https://vjudge.net/problem/LightOJ-1058 题意: There are n distinct points in the plane, given by t ...
- Light OJ 1058
题意: 简单的就组合数 C(m,n): 数据多,大, 要预处理: #include<bits/stdc++.h> using namespace std; typedef long lon ...
- 计算几何 + 统计 --- Parallelogram Counting
Parallelogram Counting Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5749 Accepted: ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
随机推荐
- 20_学生选课数据库SQL语句练习题
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- <meta http-equiv="X-UA-Compatible" content="IE=Edge">
1.X-UA-Compatible X-UA-Compatible是IE8的一个专有<meta>属性,它告诉IE8采用何种IE版本去渲染网页,在html的<head>标签中使用 ...
- CSS的标签类型
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- HDU 5806 NanoApe Loves Sequence Ⅱ
将大于等于m的数改为1,其余的改为0.问题转变成了有多少个区间的区间和>=k.可以枚举起点,二分第一个终点 或者尺取法. #pragma comment(linker, "/STACK ...
- Linux设置开机服务自动启动
[root@localhost ~]# chkconfig --list 显示开机可以自动启动的服务[root@localhost ~]# chkconfig --add *** 添加开机自 ...
- JDBC事务和JTA事务的区别
转自:JDBC和JTA事务的区别 一.事务概述事务表示一个由一系列的数据库操作组成的不可分割的逻辑单位,其中的操作要么全做要么全都不做.与事务相关的操作主要有:BEGIN TRANSACTION: 开 ...
- apt-get 总结2
本文列举了常用的APT命令参数: apt-cache search package 搜索软件包 apt-cache show package 获取包的相关信息,如说明.大小.版本等 sudo apt ...
- hdu_4823_Energy Conversion
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4823 题意:中文题,很清楚,要注意的是乘起来会爆int 题解: #include<cstdio& ...
- 让AutoMapper在你的项目里飞一会儿(转)
出处:http://www.cnblogs.com/WeiGe/p/3835523.html 先说说DTO DTO是个什么东东? DTO(Data Transfer Object)就是数据传输对象,说 ...
- Entity Framework技巧系列之八 - Tip 29 – 34
提示29. 怎样避免延迟加载或Load()阅读器问题 如果你有如下这样的代码: 1 var results = from c in ctx.Customers 2 where c.SalesPerso ...