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.
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
- POJ 2002(hash||二分,数学)
- POJ 2002 Squares(hash+二分查找)
- [POJ 2002]Squares[二分][数学]
- POJ-2002,3432(搜点,hash或二分)
- POJ 1759(数学递推+二分)
- POJ - 1905 Expanding Rods (数学+二分)
- poj 2785 二分或者hash
- Poj 2774 二分+字符串hash
- POJ 2002 hash(枚举+哈希) 或者 枚举+二分
- POJ 2002 Squares 几何 + 二分搜索 或 hash
- poj 2758: Checking the Text(二分+Hash)
- POJ 2002 - Squares(hash)
- POJ 2002 Squares(hash)
- POJ-2002-Squares(hash)
- POJ 题目1905 Expanding Rods(二分,数学几何)
- POJ:2507 Crossed ladders(二分+几何数学)
- POJ 1743 Musical Theme Hash+二分
- poj 3104 Drying 二分+基本数学
- 删除字符串中的空格
- HDOJ(HDU) 1860 统计字符
- 【HDU】 4135 Co-prime
- AAA spring 事务控制配置
- Android webview使用详解 WebSettings
- POJ 2002(hash||二分,数学)
- 初识io.js
- tar使用
- Unity Shader Tags
- JAVA多线程的一个简单案例
- 十四、原型模式Prototype(创建型模式)
- 常用命令——权限处理命令
- UVA_10154_Weights and Measures
- MD5加密算法(salt盐加密)