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组坐标,判断这些坐标能组成的正方形的个数. 思路:参考某大神的想法,先枚举两个点,然后利用公式表示出另外两个点,判断这两个点是 ...
随机推荐
- CentOS 6.3 源码安装LAMP(Linux+Apache+Mysql+Php)环境
一.简介 什么是LAMP LAMP是一种Web网络应用和开发环境,是Linux, Apache, MySQL, Php/Perl的缩写,每一个字母代表了一个组件,每个组件就其本身而>言都是在它所 ...
- MySQL——修改root密码的4种方法(以windows为例)
方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...
- 动态添加子视图 UIView 的正确方法
很多时候哥比较喜欢用代码添加视图,特别是要同时加很多UIView时,而且跟 xib 比起来代码更容易管理,在多人的项目中代码不容易 conflict. 但小牛哥最近发现很多新人都不太清楚正确的使用方法 ...
- HTTP could not register URL http://+:8000/.... Your process does not have access rights to this namespace
windows 7, Visual Studio 2013 在托管 Windows 服务中承载 WCF 服务时报错: HTTP could not register URL http://+:8000 ...
- oracle使用LEFT JOIN关联产生的问题在查询结果中使用CASE WHEN 无法判断
oracle使用LEFT JOIN关联产生的问题在查询结果中使用CASE WHEN 无法判断 查询方式一: SELECT CASE WHEN (SELECT CAST(SUM(CASE ) THEN ...
- Swift中共有74个内建函数
Swift中共有74个内建函数,但是在Swift官方文档(“The Swift Programming Language”)中只记录了7中.剩下的67个都没有记录. 本文将列举Swift所有的内建 ...
- ubuntu libreOffice设置为中文界面
在终端输入: sudo apt-get install libreoffice-l10n-zh-cn libreoffice-help-zh-cn 上面的命令是下载中文包,并安装,再次打开libreo ...
- 在用VC编译下debug和release的什么区别
DEBUG和RELEASE 版本差异及调试相关问题:. 内存分配问题 1. 变量未初始化.下面的程序在debug中运行的很好. thing * search(thin ...
- Android 学习手札(备注)
1.在Android 应用程序中不能使用System.out.println(..)来输出信息,而要使用Log类中的静态方法输出调试信息. Log.d("onStart", &qu ...
- 一个IP支持多个网站实例(Apache2、Ubuntu相关)
http://www.blogjava.net/Andyluo/archive/2009/08/24/21821.html http://blog.csdn.net/zltianhen/article ...