POJ2002 Squares(枚举)
题目链接。
分析:
普遍的做法是:先枚举两个点,通过数学公式得到另外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)
直接hash太麻烦,使用set简单些.
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set> using namespace std; const int maxn = + ;
const int VAL = ; typedef long long LL; set<LL> st; struct Pos {
int x, y;
}pos[maxn]; int main() {
int n, x3, y3, x4, y4; while(scanf("%d", &n) == && n) {
st.clear();
for(int i=; i<n; i++) {
scanf("%d %d", &pos[i].x, &pos[i].y);
st.insert((LL)pos[i].x*VAL+pos[i].y);
} int ans = ; for(int i=; i<n; i++) {
int x1 = pos[i].x, y1 = pos[i].y;
for(int j=i+; j<n; j++) {
int x2 = pos[j].x, y2 = pos[j].y;
x3 = x1+(y1-y2); y3 = y1-(x1-x2);
x4 = x2+(y1-y2); y4 = y2-(x1-x2); if( st.count((LL)x3*VAL+y3) && st.count((LL)x4*VAL+y4)) {
ans++;
} x3 = x1-(y1-y2); y3 = y1+(x1-x2);
x4 = x2-(y1-y2); y4 = y2+(x1-x2); if( st.count((LL)x3*VAL+y3) && st.count((LL)x4*VAL+y4)) {
ans++;
}
}
} printf("%d\n", ans/);
} return ;
}
POJ2002 Squares(枚举)的更多相关文章
- poj2002 Squares(hash+折半枚举)
Description A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-d ...
- POJ-2002 Squares,哈希模板+数学公式!
Squares 题意:二维坐标轴给出n个点求有多少个正方形. 要是平时做比赛的话毫无疑问会 ...
- [POJ2002]Squares(计算几何,二分)
题目链接:http://poj.org/problem?id=2002 给定一堆点,求这些点里哪些点可以构成正方形,题目给定n<=1000,直接枚举四个点是肯定会超时的,因此要做一些优化. 有公 ...
- Poj2002 Squares
题意描述:有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少.相同的四个点,不同顺序构成的正方形视为同一正方形. 思路变迁: 1.最简单的方法,直接暴力搜索,即依次取四个顶点,根据其坐标 ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares 数学题枚举
D. Spongebob and Squares Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- UVA12113-Overlapping Squares(二进制枚举)
Problem UVA12113-Overlapping Squares Accept:116 Submit:596 Time Limit: 3000 mSec Problem Descripti ...
- poj2002 hash+邻接表优化Squares
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 17487 Accepted: 6643 Descript ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)
http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\su ...
- Squares(枚举+set 查找)
http://poj.org/problem?id=2002 题意:给出n组坐标,判断这些坐标能组成的正方形的个数. 思路:参考某大神的想法,先枚举两个点,然后利用公式表示出另外两个点,判断这两个点是 ...
随机推荐
- JAAS authentication in Tomcat example--reference
In this tutorial you will learn how to configure JAAS authentication in Tomcat using the HTTP Basic ...
- Service 如何知道caller
重写Binder的onTransact方法 1 you need to do that in Binder#onTransact method, this is a good place for ...
- super() extends() private总结demo
public class TestService { private String name; public TestService(String name) { this.name=name; } ...
- 别人走的路--uap
首先,我先谈谈我个人的经历,我今年34岁了,做了10多年的ERP实施顾问,大学刚毕业的时候是做ERP软件开发的,后来转岗做了实施顾问.根据我的个人经验,我给你几点建议.1.既然是很大的公司,那么ERP ...
- HTML5 文件处理之FileAPI简介整理
在众多HTML5规范中,有一部分规范是跟文件处理有关的,在早期的浏览器技术中,处理小量字符串是js最擅长的处理之一.但文件处理,尤其是二进制文件处理,一直是个空白.在一些情况下,我们不得不通过Flas ...
- 测测你适合从事Web前端开发吗
一般初创的互联网公司最烧钱的时候往往都是刚刚获得风投或融资的时候,因为他们要把钱砸向前端,因为那时候没有客户访问,对于企业来说只有先做好前端技 术.做好客户体验一切才有可能.用户体验做好,才有人访问, ...
- 灵活运用绑定变量---declare匿名块使用绑定变量
declare type cur01 is ref cursor; v_cur cur01; v_match123 varchar2(2000); v ...
- NSDate,NSCalendar,NSTimer,NSTimeZone
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. ...
- 查看Jquery版本
1. $.fn.jquery > "1.11.1" 2. 通过这样可以 判断一个对象是否是jquery对象!!
- HDU 2295.Radar (DLX重复覆盖)
2分答案+DLX判断可行 不使用的估计函数的可重复覆盖的搜索树将十分庞大 #include <iostream> #include <cstring> #include < ...