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图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
随机推荐
- genymotion模拟器配置X86模拟器加速器
网上下载zip包 http://download.csdn.net/download/we5868123/9430140 直接拖进去即可,虚拟机不能使用管理员权限启动 名字为:解决genymotion ...
- awk中{print $1}什么意思
给你举个例子,echo "aa bb cc" | awk -F '{print $1}' 结果就是aa,意思是把字符串按空格分割,取第一个,自己做个测试就明白了!awk是用来提取列 ...
- sql语句判断两个时间段是否有交集
场景: 数据库有有两个字段.开始时间<startTime>,和结束时间<endTime>,指定一个时间段(a,b),a表示开始时间,b表示结束时间.看数据库中有没有与(a,b ...
- cookie的简单应用
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- uploadifive源码学习
一.简介 Uploadify是一个基于JQuery的多文件上传JS组件,高度定制,两个版本可供选择.flash版本在最新的Safari等不再支持flash的浏览器或者一些手机浏览器中就无法正常的加载使 ...
- 东芝MIPI解码TC358746AXBG和OV6211使用及配置
Camera Sensor常见的接口类型: 1.有并口信号(D0~D7.PCLK.HSYNC.VSYNC),一般的处理器有DCMI接口,如ST32F207x系列,直接相连就可以使用. 2.MIPI接口 ...
- Java C# .net 和 C C++ 跨平台的区别
当前主流的手机平台很多,而各个主流的平台的语言支持大同小异:如 1.windows系统WP8上主要支持 VB, C#, c/c++, 2.苹果系统ios上支持 object-c, c/c++ 3.an ...
- 注解 @ 或者 Alt+/ 不提示 或者提示 no default propsals 解决方案
- LD_LIBRARY_PATH vs LIBRARY_PATH
LIBRARY_PATH is used by gcc before compilation to search for directories containing libraries that n ...
- 给table加边框的两种方法
<!DOCTYPE html><html><head><style>.a { border-spacing: 1px; background-color ...