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. javascript 语法规范错误提示代码

    “Missing semicolon.” : “缺少分号.”, “Use the function form of \”use strict\”.” : “使用标准化定义function.”, “Un ...

  2. 【Mongodb教程 第七课 】MongoDB 查询文档

    find() 方法 要从MongoDB 查询集合数据,需要使用MongoDB 的 find() 方法. 语法 基本的find()方法语法如下 >db.COLLECTION_NAME.find() ...

  3. OpenTK的glutBitmapCharacter的替代方法

    由于openTK并没有打包集成GLUT,字体显示就成了一个问题. 办法1:采用QuickFont 优点:可以使用系统自带的字体 缺点:代码尚未成熟.只能2D显示,无法随物体旋转.平移 办法2:同时调用 ...

  4. DBENV->open

    https://stuff.mit.edu/afs/sipb/project/sandbox/golem/db-3.0.55/docs/api_c/env_open.html #include < ...

  5. Nginx——静态资源服务器(一)

    java web的项目中,我们经常将项目部署到Tomcat或者jetty上,可以通过Tomcat或者jetty启动的服务来访问静态资源.但是随着Nginx的普及,用Nginx来作为静态资源服务器,似乎 ...

  6. mongo12---手动预先分片

    手动预先分片:(每个片上的数据是不一样的,是分开存,不是做备份) 自动分片有可能短期内某个片的数据过大,硬盘不够用了.能否100000-30000就到1号片. //以shop.user表为例,先声明s ...

  7. Masonry tableviewCell布局

    前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...

  8. MkDocs -- Project documentation with Markdown

    /************************************************************************ * MkDocs -- Project docume ...

  9. openwrt 设置samba服务器与pc共享文件

    1,安装samba36-server和luci-app-samba opkg install samba36-server luci-app-samba Openwrt的samba配置是先读取/etc ...

  10. win10下的jdk1.8安装

    1 下载jdk 下一步就好 默认C盘最好 不要又中文  http://pan.baidu.com/s/1eSwvFA6(这里面有两个jdk 32位 64位) 看图 CLASSPATH: .;%JAVA ...