POJ 2002(hash||二分,数学)

来源:互联网 发布:网络语高达是什么意思 编辑:程序博客网 时间:2024/04/24 19:28

Squares
Time Limit: 3500MS Memory Limit: 65536KTotal Submissions: 18453 Accepted: 7101

Description

A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property. 

So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates. 

Input

The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.

Output

For each test case, print on a line the number of squares one can form from the given stars.

Sample Input

41 00 11 10 090 01 02 00 21 22 20 11 12 14-2 53 70 05 20

Sample Output

161

Source

Rocky Mountain 2004



题意:给你n个点,这些点可以组成多少个正方形



题解:

http://blog.csdn.net/lyy289065406/article/details/6647405

直接四个点四个点地枚举肯定超时的,不可取。

普遍的做法是:先枚举两个点,通过数学公式得到另外2个点,使得这四个点能够成正方形。然后检查散点集中是否存在计算出来的那两个点,若存在,说明有一个正方形。

但这种做法会使同一个正方形按照不同的顺序被枚举了四次,因此最后的结果要除以4.

 

已知: (x1,y1)  (x2,y2)

则:   x3=x1+(y1-y2)   y3= y1-(x1-x2)

x4=x2+(y1-y2)   y4= y2-(x1-x2)

x3=x1-(y1-y2)   y3= y1+(x1-x2)

x4=x2-(y1-y2)   y4= y2+(x1-x2)

 

据说是利用全等三角形可以求得上面的公式

有兴趣的同学可以证明下。。。



我是用map直接二分查找,跑了3350,题目3500,压线过了这一题可怜



#include<cstdio>  #include<cstring>  #include<cstdlib>  #include<cmath>  #include<iostream>  #include<algorithm>  #include<vector>  #include<map>  #include<set>  #include<queue>  #include<string>  #include<bitset>  #include<utility>  #include<functional>  #include<iomanip>  #include<sstream>  #include<ctime>  using namespace std;#define N int(1e5)  #define inf int(0x3f3f3f3f)  #define mod int(1e9+7)  typedef long long LL;#ifdef CDZSC  #define debug(...) fprintf(stderr, __VA_ARGS__)  #else  #define debug(...)   #endif  set<pair<int,int> >s;vector<pair<int, int> >v;int main() {#ifdef CDZSC  freopen("i.txt", "r", stdin);//freopen("o.txt","w",stdout);  int _time_jc = clock();#endif  //ios::sync_with_stdio(false);  int n,x,y;while (~scanf("%d", &n)&&n){v.clear();s.clear();for (int i = 0; i < n; i++){scanf("%d%d", &x, &y);v.push_back(make_pair(x, y));s.insert(make_pair(x, y));}pair<int, int>c, d;int ans = 0;for (int i = 0; i < v.size(); i++){for (int j = i + 1; j < v.size(); j++){int x1 = v[i].first;int y1 = v[i].second;int x2 = v[j].first;int y2 = v[j].second;int x3 = x1 + (y1 - y2);int y3 = y1 - (x1 - x2);int x4 = x2 + (y1 - y2);int y4 = y2 - (x1 - x2);if (s.count(make_pair(x3, y3)) && s.count(make_pair(x4, y4)))ans++; x3 = x1 - (y1 - y2); y3 = y1 + (x1 - x2); x4 = x2 - (y1 - y2); y4 = y2 + (x1 - x2); if (s.count(make_pair(x3, y3)) && s.count(make_pair(x4, y4))) ans++;}}printf("%d\n", ans/4);}#ifdef CDZSC  debug("time: %d\n", int(clock() - _time_jc));#endif  return 0;}


0 0
原创粉丝点击