POJ 2002 Squares 数学 + 必须hash
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的更多相关文章
- POJ 2002 Squares【值得摸索的一道二分+点旋转】
id=2002">Squares 很好的一道二分,事实上本来我是没有思路的,看了基神的题解之后才似乎明确了点. 题意:给出最多有1000个点,问这些点能够组成多少个正方形 分析:先想想 ...
- POJ 2002 Squares [hash]
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 16631 Accepted: 6328 Descript ...
- POJ 2002 Squares 哈希
题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...
- POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)
经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...
- POJ 2002 Squares 几何, 水题 难度: 0
题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...
- poj 2002 Squares 几何二分 || 哈希
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 15137 Accepted: 5749 Descript ...
- POJ 2002 Squares
二分.... Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 14530 Accepted: 5488 Descr ...
- Squares - poj 2002(hash)
枚举两个点作为一条边,求出正方形的另外两个点,利用hash查找另外两个点. #include<stdio.h> #include<string.h> #include<s ...
- POJ 2002 统计正方形 HASH
题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...
随机推荐
- 使用python在极坐标中生成一条直线
在测试雷达时,往往需要测试雷达的数据是否准确,这时就需要在雷达图中显示一条标准的直线作为对比. "create a wall" import numpy as np import ...
- string 是值类型,还是引用类型(.net)[转]
转自http://hi.baidu.com/newfzks/item/b805f0f4edb0810dd89e7290 string 是值类型,还是引用类型(.net) 一. string 类型的用法 ...
- HDU 4277 USACO ORZ(暴力+双向枚举)
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 定时邮件 已经稳定运行10天+ 从局域网linux到外网邮箱
- 借助ltp语义分析提取特征,之后,文本生成
"""地点-哪里有做-业务-的(正规|靠谱)-公司?地点-做-业务-的(正规|靠谱)-公司(有哪些?|的联系方式是什么?|哪家口碑好值得信赖?)地点-做-业务-(怎么能省 ...
- Filter 详解
一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...
- javaScript复制粘贴
1.clipboard.js 实现了纯 JavaScript (无 Flash)的浏览器内容复制到系统剪贴板的功能.可以在浏览器和 Node 环境中使用.支持 Chrome 42+.Firefox 4 ...
- 有关 enum的重新理解
有关enum 的再次理解: 所有的枚举都继承自java.lang.Enum类. 说到底enum也只是一个java类,只不过他有几个特殊的点. 1.enum中的各个实例,就是enum的static实 ...
- caioj1522: [NOIP提高组2005]过河
状态压缩的经典题. 按照一般做法,DP一维时间O(n),显然跑不过.考虑到石子较少,实际上有很长一段是一定可以跳到的,设两个石头分别在i点和j点,跳跃的路程为S到T.那么从i点可以跳到i+S到i+T. ...
- codeforces 689A A. Mike and Cellphone(水题)
题目链接: A. Mike and Cellphone time limit per test 1 second memory limit per test 256 megabytes input s ...