POJ 2002 Squares【值得摸索的一道二分+点旋转】
很好的一道二分,事实上本来我是没有思路的,看了基神的题解之后才似乎明确了点。
题意:给出最多有1000个点,问这些点能够组成多少个正方形
分析:先想想怎么推断正方形吧。假如我如今有四个点A,B,C,D,由于边不一定是平行于x轴的。可能是倾斜的,怎样推断四个点组成的四边形是正方形呢?以A点为中心,AB为轴,绕A点顺时针或者逆时针转动90度,就能够得到一个点D'
, 相同的方法。以B
点为中心,BA为轴。绕B点顺时针或者逆时针转动90度。就能够得到一个点C'
, 假设D' 的坐标与D的坐标同样。假设C' 的坐标与C的坐标同样 ,
那么就能够确定这个四边形是正方形了。
旋转能够用这个公式:
随意点(x,y)。绕一个坐标点(rx0,ry0)逆时针旋转a角度后的新的坐标设为(x0, y0)。有公式:
x0= (x - rx0)*cos(a) - (y - ry0)*sin(a) + rx0 ;
y0= (x - rx0)*sin(a) + (y - ry0)*cos(a) + ry0 ;
既然知道怎样推断正方形了,那么接下来,我仅仅须要在给定的点里面每次取出两个点。A。B。【这里O(N^2)的复杂度】,求出相应的C',D'【因为旋转方向不同。会有两个这种解】。然后推断在这些给定的点里面有没有点与C',D'重合就可以。假设我再暴力的去遍历,终于复杂度会变成O(N^3),TLE无疑,这个时候,二分来作用了,我刚開始就把点给排序。然后仅仅须要对点进行二分查找就OK了,那么这个时候我的复杂度是O(N^2*log(N))。当然,要注意不要反复推断了,也就是我取A,B的时候,还有二分的时候注意一下范围就能够了。
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000+5;
int N,ans;
struct SPoint {
double x,y;
SPoint() {}
SPoint(int xx,int yy) : x(xx), y(yy) {}
bool operator < (const SPoint& p) const {
if(x == p.x) return y < p.y;
return x < p.x;
}
bool operator == (const SPoint& p) const {
return x==p.x&&y==p.y;
}
}pnt[maxn];
//随意点(x,y),绕一个坐标点(rx0,ry0)逆时针旋转a角度后的新的坐标设为(x0, y0),有公式:
//x0= (x - rx0)*cos(a) - (y - ry0)*sin(a) + rx0 ;
//y0= (x - rx0)*sin(a) + (y - ry0)*cos(a) + ry0 ;
void transXY(SPoint A, SPoint B, SPoint &C, int f) {
int tx = B.x - A.x, ty = B.y - A.y;
C.x = A.x - ty * f;
C.y = A.y + tx * f;
}
int main() {
//freopen("input.in","r",stdin);
while(~scanf("%d",&N)&&N)
{
ans = 0;
for(int i = 0;i < N;i++) scanf("%lf %lf",&pnt[i].x,&pnt[i].y);
sort(pnt,pnt+N);
for(int i = 0;i < N-3;i++) //①
{
for(int j = i+1;j < N-2;j++) //②
{
SPoint C,D;
transXY(pnt[i],pnt[j],C,1);
transXY(pnt[j],pnt[i],D,-1);
if(binary_search(pnt+j,pnt+N,C)&&binary_search(pnt+j,pnt+N,D)) ans++; //③
transXY(pnt[i],pnt[j],C,-1);
transXY(pnt[j],pnt[i],D,1);
if(binary_search(pnt+j,pnt+N,C)&&binary_search(pnt+j,pnt+N,D)) ans++; //④ 注意这四个地方,避免反复推断
}
}
printf("%d\n",ans);
}
return 0;
}
POJ 2002 Squares【值得摸索的一道二分+点旋转】的更多相关文章
- poj 2002 Squares 几何二分 || 哈希
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 15137 Accepted: 5749 Descript ...
- POJ 2002 Squares 数学 + 必须hash
http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...
- POJ 2002 Squares
二分.... Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 14530 Accepted: 5488 Descr ...
- POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)
经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...
- POJ 2002 Squares 哈希
题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...
- POJ 2002 Squares 几何, 水题 难度: 0
题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...
- POJ 2002 Squares [hash]
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 16631 Accepted: 6328 Descript ...
- POJ 2723 Get Luffy Out(2-SAT+二分答案)
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8851 Accepted: 3441 Des ...
- Squares - poj 2002(hash)
枚举两个点作为一条边,求出正方形的另外两个点,利用hash查找另外两个点. #include<stdio.h> #include<string.h> #include<s ...
随机推荐
- Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts
题目原文: Nuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is t ...
- 玩游戏(dfs)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2566 #include <stdio.h ...
- 动态title
<html><head><meta charset="uft8"><title>测试title</title></ ...
- Oracle_备份整库
@echo off color 0b & cls echo echo 设置备份文件存放文件夹... echo set "tbuf=C:\OracleBackup" if n ...
- python 13:数字列表统计方法(min(list)、max(list)、sum(list))
numbers = list(range(1,11)) print(numbers) print(min(numbers)) #获得列表最小值 print(max(numbers)) #获得列表最大值 ...
- centos 修改网卡信息命令
vi /etc/sysconfig/network-scripts/ifcfg-eth0
- Tour UVA - 1347
John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts vi ...
- 努比亚(nubia) M2青春版 NX573J 解锁BootLoader 并进入临时recovery ROOT
努比亚(nubia) M2青春版 NX573J 解锁BootLoader 并进入临时recovery ROOT 工具下载链接:https://pan.baidu.com/s/1NfRTdXtdAZRi ...
- js-学习方法
1:多实践,找例子,看别人是如何实现的,然后自己去实现,然后谷歌百度,最后总结. 2:如何读js英文书:不是自己不会读,是被吓着了.自己吓自己. 英文不好的话,先不要挨着排的从头到尾读. 应该首先读目 ...
- Java多线程中常见的几个问题
我们都知道,在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 1.进程和线程的区别是什么? 进程是执行着的应用程序,而线程是进程内部的一个执行序列. ...