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图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
随机推荐
- webservice整合spring
接口HelloWorld需要添加webservice注解 package com.cs.webservice; import java.util.List; import javax.jws.WebP ...
- ECOS-Ecstore 后台管理地址修改
ECStore默认出厂的后台管理地址是: http://域名/index.php/shopadmin http://域名/shopadmin [配置过rewrite后,并开启伪静态] 如果想要更个性的 ...
- 星语硬件检测专家 V4.3 官方版
软件名称: 星语硬件检测专家 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 15.8MB 图片预览: 软件简介: 星语硬件检测专家是一款功能非常强大的硬件 ...
- Smarty 注册变量
关于smarty类的一些解析 特别注意左右分隔符<{}>,display------显示模板的内容(里面是.html文件),assign-------注册变量 <?php //是一个 ...
- discuz论坛小记
客服要改sitemap.php要展示最新的回帖内容,但是我不懂discuz,对数据库和php不是特别熟练,PHP后台开发的程序猿太忙了!!~没办法硬着头皮上~ 哎呀数据库对应的是哪个啊?让我看看con ...
- 免费开源的boostrap模板
百度查不到,奶奶的,百度好多国外技术类文章的都查不到 https://colorlib.com/wp/free-html5-admin-dashboard-templates/ 有没有FQ软件介绍呀?
- php 链接 sqlserver 2005以上版本数据库
<?php /** * 数据库管理 * * @author wangaibo168@163.com * @charset utf-8 * 不支持sqlserver2005(包括)以下的版本 */ ...
- apt-get 总结
转自: apt-get 总结 1.apt-get install <package_name> install a new package. 2.apt-get build-dep < ...
- 一个机器学习博客 ,包括 Standford公开课machine learning
http://blog.csdn.net/abcjennifer/article/category/1173803/4 http://blog.csdn.net/abcjennifer/article ...
- Java is Pass-by-Value!
Java is strictly pass-by-value. which means, when you pass a variable to a method, the method will j ...