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图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
随机推荐
- ControlTemple样式
1.TextBox 样式 1.1 style <Window.Resources> <Style x:Key="aa" TargetType="{x:T ...
- [PHP] 安装和配置
Apachehttpd-2.2.19-win64mysql5.6Phphttp://www.php.net/downloads.php 5.4Phpeclipsehttp://www.phpeclip ...
- RLE行程长度编码压缩算法
在看emWIN的时候看到一个图片压缩的算法可以有效的对二值图(简单的2中颜色或者更多)进行压缩,压缩的效果可以节省空间而且不丢失信息! 特点 一种压缩过的位图文件格式,RLE压缩方案是一种极其成熟的压 ...
- HDU 5772 String problem
最大权闭合子图.建图巧妙. 最大权闭合子图: #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...
- VirtualBox 不能为虚拟电脑打开一个新的任务 可能的解决方案
1. 在虚拟机上右键,清除保存状态 2.Cannot load R0 module C:\Program Files\Oracle\VirtualBox/VBoxDD2R0.r0: SUPR3Load ...
- Chapter 2 Open Book——10
I sent that, and began again. 我发送了它,然后又一次重新开始写了. Mom,Everything is great. Of course it's raining. I ...
- 在选定的数据源上未找到名为“TitleSub”的字段或属
在.NET开发过程中时常会遇到“在选定的数据源上未找到名为“TitleSub”的字段或属性”的错误”,导致这类错误的原因有很多,在我的项目中,详细情况是这样:1.有两个控件:DropDownList类 ...
- 认识cookie与session的区别与应用
通常我们所说的浏览器自动保存密码,下次不用登陆,网页换皮肤,用户引导,提示一次就不再出现的内容,大部分通过cookie或者session来实现的,在这次制作用户引导中,本人就用到了cookie的内容, ...
- 把perl脚本编译成exe
来源:http://www.veryhuo.com/a/view/38338.html 今天想把 perl 脚本编译成 exe 以便脱离 perl 解释器独立运行.都可以生成PERL的PE文件,在PE ...
- iOS中的多线程基础
NSThread NSThread是一个苹果封装过的,面向对象的线程对象.但是它的生命周期需要我们自己来手动管理,所以使用不是很常见,比如[NSThread currentThread],它可以获取当 ...