poj 2002
2014-08-16 14:37
288 查看
#include <iostream> #include <cmath> #include <algorithm> using namespace std; #define N 40001 int n; struct point { int index;//记录v的下标 point *next; point(){index = -1; next = NULL;} point(int i){index = i; next = NULL;} }; struct p { int x, y; }v[4001]; bool cmp(p a, p b) { if (a.x < b.x) return true; if (a.x == b.x && a.y < b.y) return true; return false; } bool found(point *hash, int& x, int& y) { int i = abs(x + y); point *p; if (hash[i].index == -1) return false; p = &hash[i]; while (p != NULL) { if (v[p->index].x == x && v[p->index].y == y) return true; p = p->next; } return false; } int main() { int i, j, sum, index, x1_x2, y1_y2, x1, y1, x2, y2; point *p; while (cin >> n) { point hash ; for (i = 0; i < n; i++) cin >> v[i].x >> v[i].y; sort(v, v + n, cmp); for (i = 0; i < n; i++) { index = abs(v[i].x + v[i].y); if (hash[index].index == -1) hash[index].index = i; else { p = &hash[index]; while (p->next != NULL) p = p->next; p->next = new point(i); } } for (sum = 0, i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (v[i].x <= v[j].x && v[i].y >= v[j].y) { x1_x2 = abs(v[i].x - v[j].x); if (x1_x2 == 0) continue; y1_y2 = abs(v[i].y - v[j].y); x1 = v[i].x + y1_y2; y1 = v[i].y + x1_x2; x2 = v[j].x + y1_y2; y2 = v[j].y + x1_x2; if (found(hash, x1, y1) && found(hash, x2, y2)) sum++; } } } cout << sum << endl; } return 0; }
以前一直觉得哈希无用,没有什么必须用到哈希的,
但是最近做这两个题都要用。
哈希可以用来做什么呢?
哈希可以在常数时间内插入和查找,简直吊炸天。。
用一些树也可以维护吧。。效率没有那么高。。代码量也会略大。。
也可以二分。。千万不要听discuss里那帮傻逼瞎说。。
虽然我有看discuss的习惯。。
不管了不管了。。拿别人代码刷过了。。还挺快的。。
相关文章推荐
- POJ 2002 Squares
- [POJ2002]Squares【平面几何】
- POJ 2002 哈希
- poj 2002 Squares
- POJ 2002 Squares
- POJ 2002 Squares hash
- POJ 2002
- POJ 2002 Squares
- POJ 2002(hash||二分,数学)
- [USACO2002][poj1944]Fiber Communications(枚举)
- POJ 2002 Squares(已知平面n点枚举正方形)
- [POJ2002]Squares(计算几何,二分)
- (2017多校训练第二场)HDU - 6055 & POJ - 2002 Regular polygon 哈希
- POJ 2002-Squares(hash拉链法-正方形个数)
- poj 2002 Squares
- poj2002 哈希链地址法
- Poj 2002 Squares
- POJ 2002 Squares(哈希表)
- POJ 2002 Squares 几何, 水题 难度: 0
- POJ 2002