标签:
---恢复内容开始---
题意:给n个点,问有多少组四个点能组成正方形。
题解:枚举两个点,通过公式算出另外两个点,然后通过哈希查找另外两个点存不存在。
//突然发现好弱,好多基础的算法竟然都不会,哈希这种经典的算法,我貌似基本没怎么做过相关的题0.0
公式是抄网上的,哈希直接用了vector存的,反正时限3500ms
点的哈希就是(x^2+y^2)%MOD
AC代码:
/************************************** Memory: 924 KB Time: 969 MS Language: G++ Result: Accepted **************************************/ #include <cstdio> #include <cstring> #include <vector> using namespace std; const int MOD = 20007; const int N = 1005; struct Point { int x, y; int key; } p[N]; int cal(int x, int y) { return (x*x+y*y) % MOD; } vector<int> vec[MOD]; void insert(int x) { vec[p[x].key].push_back(x); } bool find(int x, int y) { int k = cal(x, y); for (unsigned i = 0; i < vec[k].size(); ++i) { int v = vec[k][i]; if (x == p[v].x && y == p[v].y) return true; } return false; } int main() { //freopen("in", "r", stdin); int n; while (~scanf("%d", &n) && n) { for (int i = 0; i < MOD; ++i) vec[i].clear(); for (int i = 0; i < n; ++i) { scanf("%d%d", &p[i].x, &p[i].y); p[i].key = cal(p[i].x, p[i].y); insert(i); } int ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j) continue; int x1 = p[i].x-p[j].y+p[i].y; int y1 = p[i].y+p[j].x-p[i].x; int x2 = p[j].x-p[j].y+p[i].y; int y2 = p[j].y+p[j].x-p[i].x; if (find(x1,y1) && find(x2,y2)) ++ans; } } printf("%d\n", ans/4); } return 0; }
标签:
原文地址:http://www.cnblogs.com/wenruo/p/5794781.html