http://poj.org/problem?id=2002

只能说hash比二分快很多。随便一个hash函数都可以完爆二分。

判断是否存在正方形思路如下:

1、枚举任意两个点,作为正方形的一条边,那么,整个正方形就确定了,有两个方向。

因为,

设枚举的坐标为(x1, y1) & (x2, y2),所求的坐标是和x1,y1这个点相连,那么有方程如下。

1、垂直,向量积是0

2、边长相等,然后距离公式化简。

即可解出剩下的两个点。

然后要注意两个点要在正方形的同一侧,不然变了平行四边形了。

唤醒了我hash的回忆。

首先hash的时候,需要先把坐标平移,因为要避免出现负数的情况,所以平移40000个单位。然后再进行简单hash

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
struct node {
int x, y;
bool operator < (const struct node & rhs) const {
if (x != rhs.x) {
return x < rhs.x;
} else return y < rhs.y;
}
bool operator != (const struct node & rhs) const {
return !(x == rhs.x && y == rhs.y);
}
} arr[];
int n;
const int MOD = ;
struct Edge {
int val, id, tonext;
}e[maxn * ];
int first[maxn];
int num;
void add(int val, int id) {
++num;
int tval = val;
val %= MOD;
e[num].id = id;
e[num].val = tval;
e[num].tonext = first[val];
first[val] = num;
}
bool tofind(int val, int one, int two) {
assert(val >= );
int tval = val;
val %= MOD;
for (int i = first[val]; i; i = e[i].tonext) {
if (e[i].val == tval && e[i].id != one && e[i].id != two) return true;
}
return false;
}
bool check(struct node t1, struct node t2, int one, int two) {
return tofind(t1.x * + t1.y, one, two) &&
tofind(t2.x * + t2.y, one, two);
}
void work() {
// cout << (20000 + 20000) * 20001 << endl;
num = ;
memset(first, , sizeof first);
for (int i = ; i <= n; ++i) {
scanf("%d%d", &arr[i].x, &arr[i].y);
arr[i].x += ;
arr[i].y += ;
add(arr[i].x * + arr[i].y, i);
assert(arr[i].x * + arr[i].y >= );
}
// cout << tofind(arr[1].x * 20001 + arr[1].y, 3, 2) << endl;
// sort(arr + 1, arr + 1 + n);
// for (int i = 1; i <= n; ++i) {
// cout << arr[i].x << " " << arr[i].y << endl;
// }
// cout << endl;
LL ans = ;
for (int i = ; i <= n; ++i) {
for (int j = i + ; j <= n; ++j) {
struct node t[];
t[].x = arr[i].y - arr[j].y + arr[i].x;
t[].y = arr[j].x - arr[i].x + arr[i].y;
t[].x = arr[j].y - arr[i].y + arr[j].x;
t[].y = arr[i].x - arr[j].x + arr[j].y;
// cout << i << " " << j << endl;
// cout << "*********" << endl;
// for (int k = 0; k <= 1; ++k) {
// cout << t[k].x << " " << t[k].y << endl;
// }
// cout << "**********" << endl;
if (t[].x < arr[i].x || t[].x == arr[i].x && t[].y < arr[i].y) {
t[].x = arr[j].y - arr[i].y + arr[i].x;
t[].y = arr[i].x - arr[j].x + arr[i].y;
}
if (t[].x < arr[j].x || t[].x == arr[j].x && t[].y < arr[j].y) {
t[].x = arr[i].y - arr[j].y + arr[j].x;
t[].y = arr[j].x - arr[i].x + arr[j].y;
}
if (check(t[], t[], i, j)) ++ans; t[].x = arr[i].y - arr[j].y + arr[i].x;
t[].y = arr[j].x - arr[i].x + arr[i].y;
t[].x = arr[j].y - arr[i].y + arr[j].x;
t[].y = arr[i].x - arr[j].x + arr[j].y; if (t[].x > arr[i].x || t[].x == arr[i].x && t[].y > arr[i].y) {
t[].x = arr[j].y - arr[i].y + arr[i].x;
t[].y = arr[i].x - arr[j].x + arr[i].y;
}
if (t[].x > arr[j].x || t[].x == arr[j].x && t[].y > arr[j].y) {
t[].x = arr[i].y - arr[j].y + arr[j].x;
t[].y = arr[j].x - arr[i].x + arr[j].y;
}
// for (int k = 0; k <= 1; ++k) {
// cout << t[k].x << " " << t[k].y << endl;
// }
// cout << endl;
if (check(t[], t[], i, j)) ans++;
}
}
assert(ans >= );
printf("%lld\n", ans / );
return;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
while (scanf("%d", &n) != EOF && n) work();
return ;
}

POJ 2002 Squares 数学 + 必须hash的更多相关文章

  1. POJ 2002 Squares【值得摸索的一道二分+点旋转】

    id=2002">Squares 很好的一道二分,事实上本来我是没有思路的,看了基神的题解之后才似乎明确了点. 题意:给出最多有1000个点,问这些点能够组成多少个正方形 分析:先想想 ...

  2. POJ 2002 Squares [hash]

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 16631   Accepted: 6328 Descript ...

  3. POJ 2002 Squares 哈希

    题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...

  4. POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)

    经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...

  5. POJ 2002 Squares 几何, 水题 难度: 0

    题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...

  6. poj 2002 Squares 几何二分 || 哈希

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15137   Accepted: 5749 Descript ...

  7. POJ 2002 Squares

    二分.... Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 14530 Accepted: 5488 Descr ...

  8. Squares - poj 2002(hash)

    枚举两个点作为一条边,求出正方形的另外两个点,利用hash查找另外两个点. #include<stdio.h> #include<string.h> #include<s ...

  9. POJ 2002 统计正方形 HASH

    题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...

随机推荐

  1. JAVA学习第六十四课 — 反射机制

       Java反射机制是在执行状态中,对于随意一个类,都可以知道这个类的全部属性和方法,对于随意一个对象,都可以调用它的随意一个方法和属性,这样的动态获取的信息以及动态调用对象的方法的功能称为java ...

  2. 在EasyUI的DataGrid中嵌入Combobox

    在做项目时,须要在EasyUI的DataGrid中嵌入Combobox,花了好几天功夫,在大家的帮助下,最终看到了它的庐山真面: 核心代码例如以下: <html> <head> ...

  3. centos 安装mysql时错误unknown variable &#39;defaults-file=/opt/redmine-2.6.0-2/mysql/my.cnf&#39;

    找到my.cnf所在目录.运行 chmod 664 my.cnf,再启动mysql成功

  4. 用bis和bic实现位级操作

    20世纪70年代末至80年代末,DigitalEquipment的VAX计算机是一种非常流行的机型.它没有布尔运算AND和OR指令,仅仅有bis(位设置)和bic(位清除)这两种指令.两种指令的输入都 ...

  5. visio 2010 修改 默认字体 字号大小 方法[整理]

    [转自]http://www.cnblogs.com/vegaliming/archive/2012/08/09/2630568.html 1.新建一个模具 2.将常用的图形放到这个模具中 3.对每个 ...

  6. ABAP FORM打印转PDF/pdf 预览

    function ZSTXBC_SSFCOMP_PDF_PREVIEW. *"-------------------------------------------------------- ...

  7. POJ3694 Network —— 边双联通分量 + 缩点 + LCA + 并查集

    题目链接:https://vjudge.net/problem/POJ-3694 A network administrator manages a large network. The networ ...

  8. mac多线程下载神器

    本文参考:https://blog.csdn.net/orangleliu/article/details/46834429 神器:axel 安装(已经安装homebrew前提下,没有请参考:http ...

  9. Java 内存管理、JVM 工作原理与 Java 运行时系统

    Java 虚拟机规范中说明:所有的对象实例(all class instances)以及数组都要在堆上分配: the heap is the runtime data area from which ...

  10. Spring Ioc容器核心类继承图

    Spring IOC容器其实就是BeanFactory的实例,Spring中BeanFactory的类关系结构如下图: 从上图可以看出Beanfactory作为根接口又细化出三个二级接口,最后又有Co ...