POJ 2002 Squares(二分)
2015-07-25 01:58
399 查看
Squares
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
Sample Output
给一个平面散点集,判断能够构成多少个正方形。虽然有3.5秒,但四层暴力循环的话肯定会超时循环。所以有这样一种思路:先把点排序,双层循环枚举前(n-2)个点,为了防止重复判断,第二层循环里的j要从i+1开始,二分查找后(n-j)个点中是否存在能与s[i],s[j]构成正方形的点,所以第二层循环结束的条件是j<=n-2,剩下2个点用来查找,二分查找的范围是[j+1,n]。
已知2个点,写出能与这2个点构成正方形的坐标的计算方法如下图(计算坐标的时候不要用double,否则很容易TLE或者WA)
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 17423 | Accepted: 6614 |
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
4 1 0 0 1 1 1 0 0 9 0 0 1 0 2 0 0 2 1 2 2 2 0 1 1 1 2 1 4 -2 5 3 7 0 0 5 2 0
Sample Output
1 6 1
给一个平面散点集,判断能够构成多少个正方形。虽然有3.5秒,但四层暴力循环的话肯定会超时循环。所以有这样一种思路:先把点排序,双层循环枚举前(n-2)个点,为了防止重复判断,第二层循环里的j要从i+1开始,二分查找后(n-j)个点中是否存在能与s[i],s[j]构成正方形的点,所以第二层循环结束的条件是j<=n-2,剩下2个点用来查找,二分查找的范围是[j+1,n]。
已知2个点,写出能与这2个点构成正方形的坐标的计算方法如下图(计算坐标的时候不要用double,否则很容易TLE或者WA)
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; const int MAXN=1e5+20; int n; long long ans; struct star { int x,y; star(){} star(int x,int y) { this->x=x; this->y=y; } bool operator<(const star& n)const { if(this->x==n.x) return this->y<n.y; return this->x<n.x; } }s[MAXN]; int searchh(int l,int r,star n) { while(l<=r) { int mid=(l+r)/2; if(s[mid].x==n.x&&s[mid].y==n.y) return 1; if(s[mid]<n) l=mid+1; else r=mid-1; } return 0; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE while(scanf("%d",&n)!=EOF&&n) { ans=0; for(int i=1;i<=n;i++) scanf("%d%d",&s[i].x,&s[i].y); sort(s+1,s+n+1); for(int i=1;i<=n-3;i++) { for(int j=i+1;j<=n-2;j++) { int xxx=s[j].x-s[i].x; int yyy=s[j].y-s[i].y; int sx1=s[i].x+yyy; int sy1=s[i].y-xxx; int sx2=s[j].x+yyy; int sy2=s[j].y-xxx; if(searchh(j+1,n,star(sx1,sy1))&&searchh(j+1,n,star(sx2,sy2))) ans++; int sx3=s[i].x-yyy; int sy3=s[i].y+xxx; int sx4=s[j].x-yyy; int sy4=s[j].y+xxx; if(searchh(j+1,n,star(sx3,sy3))&&searchh(j+1,n,star(sx4,sy4))) ans++; } } printf("%lld\n",ans); } return 0; }
相关文章推荐
- 使用UltraISO制作启动盘装Windows系统详细教程
- java 使用JNI调用C语言
- HTML--Note3--link
- ThinkPHP 3.2 行为扩展以及插件机制介绍
- Permutations
- ubuntu安装vsftpd后出现 530 Login incorrect
- 解决java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName'
- 黑马程序员-ios学习笔记 c语言 结构体
- Eclipse 日期和时间格式自定义
- 7月21日, 文化部下发通知: 游戏机生产销售全面解禁
- 1042 N!
- IOS开发学习记录第3天之C语言学习
- Code Blocks 配置 MinGW Distro 使用boost库
- IOS开发学习记录第2天之熟悉Xcode常用快捷键
- IOS开发学习记录第1天之熟悉Mac常用快捷键
- 学艺不精而羞愧--论C++模板类的使用
- HTML--Note2--list
- C# 发送邮件
- 一个nginx_reuseport 简单补丁实现
- Leetcode 9 Palindrome Number