poj2002 Squares(hash+折半枚举)
Description
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
Output
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
题意:在二维平面上,给你n个点的坐标,求任取四点构成正方形的个数
题解:好吧,暴力O(n^4)又t了……
那么折半枚举吧
枚举两个点连成一条线,然后根据这条线可以得到正方形的另外两个点,根据hash可以快速确定点是否在n个点中,即可以计算出正方形的个数
代码如下:
#include<map>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std; struct node
{
int x,y;
} a[]; vector<long long> g[];
int n; int cmp(node x,node y)
{
if(x.x==y.x)
{
return x.y<y.y;
}
return x.x<y.x;
} int main()
{ while(scanf("%d",&n)==&&n)
{
int ans=;
vector<long long> g[];
for(int i=; i<=n; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a+,a+n+,cmp);
for(int i=; i<=n; i++)
{
long long key=(a[i].x*a[i].x+a[i].y*a[i].y)%;
g[key].push_back(i);
}
for(int i=; i<=n; i++)
{
for(int j=i+; j<=n; j++)
{
int x1,y1,x2,y2;
int dx=(a[j].x-a[i].x),dy=(a[j].y-a[i].y);
// if(dx<0||dy<0)
// {
// continue;
// }
x1=a[i].x-dy;
y1=a[i].y+dx;
x2=a[j].x-dy;
y2=a[j].y+dx;
int flag=;
long long key=(x1*x1+y1*y1)%;
for(int k=; k<g[key].size(); k++)
{
if(a[g[key][k]].x==x1&&a[g[key][k]].y==y1)
{
flag+=;
}
}
key=(x2*x2+y2*y2)%;
for(int k=; k<g[key].size(); k++)
{
if(a[g[key][k]].x==x2&&a[g[key][k]].y==y2)
{
flag+=;
}
}
if(flag==)
{
// if(a[i].x==x1&&a[i].y==y1||a[j].x==x2&&a[j].y==y2||a[i].x==x2&&a[i].y==y2||a[j].x==x1&&a[j].y==y1)
// {
// continue;
// }
// printf("%d %d\n",dx,dy);
// printf("%d %d %d %d %d %d %d %d\n",a[i].x,a[i].y,a[j].x,a[j].y,x1,y1,x2,y2);
ans++;
}
}
}
printf("%d\n",ans/);
} }
poj2002 Squares(hash+折半枚举)的更多相关文章
- poj1840 Eqs(hash+折半枚举)
Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...
- 折半枚举+Hash(HDU1496升级版)
题目链接:N - 方程的解 给定一个四元二次方程: Ax1^2+Bx2^2+Cx3^2+Dx4^2=0 试求−1000≤x1,x2,x3,x4≤1000非零整数解的个数. −10000≤A,B,C,D ...
- Load Balancing 折半枚举大法好啊
Load Balancing 给出每个学生的学分. 将学生按学分分成四组,使得sigma (sumi-n/4)最小. 算法: 折半枚举 #include <iostrea ...
- CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。
1514: Packs Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 61 Solved: 4[Submit][Status][Web Board] ...
- NYOJ 1091 超大01背包(折半枚举)
这道题乍一看是普通的01背包,最最基础的,但是仔细一看数据,发现普通的根本没法做,仔细观察数组发现n比较小,利用这个特点将它划分为前半部分和后半部分这样就好了,当时在网上找题解,找不到,后来在挑战程序 ...
- Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))
888E - Maximum Subsequence 思路:折半枚举. 代码: #include<bits/stdc++.h> using namespace std; #define l ...
- Codeforces 912 E.Prime Gift (折半枚举、二分)
题目链接:Prime Gift 题意: 给出了n(1<=n<=16)个互不相同的质数pi(2<=pi<=100),现在要求第k大个约数全在所给质数集的数.(保证这个数不超过1e ...
- poj_3977 折半枚举
题目大意 给定N(N<=35)个数字,每个数字都<= 2^15. 其中一个或多个数字加和可以得到s,求出s的绝对值的最小值,并给出当s取绝对值最小值时,需要加和的数字的个数. 题目分析 需 ...
- POJ 3977 Subset(折半枚举+二分)
SubsetTime Limit: 30000MS Memory Limit: 65536KTotal Submissions: 6754 Accepted: 1277 D ...
随机推荐
- ssh面试题2
1. BeanFactory的作用是什么? [中] BeanFactory是配置.创建.管理bean的容器,有时候也称为bean上下文.Bean与bean的依赖关系,也是由BeanFactory负责维 ...
- 关于Homebrew出现GitHub API rate limit错误的解决方法
参考博文: http://havee.me/mac/2013-12/how-to-install-and-use-homebrew.html Error: GitHub API rate limit ...
- Java课程设计——坦克大战
坦克大战——坦克类 一. 团队课程设计博客链接 https://www.cnblogs.com/chenhuilin/p/10275664.html 二.个人负责模块和任务说明 模块:坦克类(玩家坦克 ...
- 找回mysql root用户的密码
1.停掉mysql服务ps -ef | grep mysqldkill -9 mysql进程的pid2.vi /etc/my.cnf找到[mysqld]在下面添加一行skip-grant-tables ...
- AppCan使用注意问题
1.文件上传的时候尽量使用uexUploadMsg,然后注意文件名,文件名一定要正确才能传上去.
- 使用YCSB测试mongodb
项目里面需要对mongodb的性能进行测试,看了下网上很多做法都是使用YCSB进行测试,因此开始学习使用YCSB. 参考资料: YCSB github地址:https://github.com/bri ...
- CommonJS、CMD和AMD规范分别是什么
CommonJS.CMD和AMD规范分别是什么 Commonjs是用在服务器端的,同步的,如nodejs amd, cmd是用在浏览器端的,异步的,如requirejs和seajs 其中,amd先提出 ...
- 小程序App方法
App() 注册一个小程序 小程序的入口方法 //app.js App({ onLaunch: function(options) { console.log("onLaunch" ...
- day5心得
import 模块 1.定义: 模块:用来从逻辑上组织python代码(变量.函数.类.逻辑:实现一个功能),本质就是.py结尾的python文件(文件名test.py 模块名:test) 2导入方法 ...
- 配置key认证登陆Ubuntu (上)
每一个看似轻松的结果背后都有不为人知的辛酸.又是一件小事,结果折腾了一天. 上接配置好SSH 和Samba后,开始了Python编程实践.由于实在不大会用Vim, 所以最后的编程环境实际上在Windo ...