poj Squares n个点,共能组成多少个正方形 二分 + 哈希
题目链接:http://poj.org/problem?id=2002
测试数据:
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
有两种解法,第一种用二分查找,第二种用到hash存储:
解法一:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int T;
struct TT
{
int x,y;
}node[];
bool cmp(TT a,TT b)
{
if(a.x<b.x ||(a.x==b.x && a.y<b.y)) return true;
return false;
}
bool judge(int x,int y)
{
int L = ,R =T;
while(L<=R)
{
int mid = (L+R)/;
if(node[mid].x == x && node[mid].y == y)
{
return true;
}
if(x == node[mid].x ) //这个地方老是出错,就是当x值相同的时候,会有两种情况的,我只是考虑了一种
{
if(y>node[mid].y) L = mid +;
else R = mid-;
}
else if(x>node[mid].x) L = mid+;
else R = mid-;
}
return false;
}
int main()
{
int ans;
int x1,y1,x2,y2,mx,my;
while(scanf("%d",&T) && T)
{ ans = ;
for(int i =;i<=T;i++)
scanf("%d %d",&node[i].x,&node[i].y);
sort(node+,node+T+,cmp);//乱了一下,开始从0,开始排序了
for(int i = ; i<T; i++)
for(int j = i+; j<=T; j++)
{
mx = node[j].x - node[i].x;
my = node[j].y - node[i].y;
x1 = node[i].x+my;
y1 = node[i].y-mx;
x2 = node[j].x+my;
y2 = node[j].y-mx;
if( judge(x1,y1) && judge(x2,y2))
{
ans++;
}
}
printf("%d\n",ans/);//因为每次都有两个重复;
}
return ;
}
解法二:本来想着哈希简简单单就过了呢,咋说也比二分省时间吧,可是呢,整了两个小时才搞定,还发现了一个问题;有待于求证:结构体数组赋值时间小于数组赋值时间,
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
struct TT
{
int x,y,next;
}node[],hash[];
const int N = ;
int vis[N][N];
int T;
const int MOD = ;
int next[N],head[MOD],top = ;
void creath(int x,int y)
{
int key = abs(x)%MOD;
hash[top].next = head[key];//这样写超时: next[top] = head[key];
hash[top].x = x;
hash[top].y = y;
head[key] = top;
top++;
}
bool cmp(TT a,TT b)
{
if(a.x<b.x || (a.x == b.x && a.y<b.y)) return true;
return false;
}
bool judge(int x,int y)
{
int key = abs(x)%MOD;
int ans = ;
for(int i = head[key];i>=;i = hash[i].next)//换成 i = next[i] 超时
{
if(hash[i].x==x && hash[i].y == y)
return true;
}
return false;
}
int main()
{
int ans;
int x1,y1,x2,y2,mx,my;
while(scanf("%d",&T) && T)
{ memset(head,-,sizeof(head));
ans = ;
for(int i =;i<=T;i++)
{
scanf("%d %d",&node[i].x,&node[i].y);
creath(node[i].x,node[i].y);
}
sort(node+,node++T,cmp);
for(int i = ; i<T; i++)
for(int j = i+; j<=T; j++)
{
mx = node[j].x - node[i].x;
my = node[j].y - node[i].y;
x1 = node[i].x+my;
y1 = node[i].y-mx;
x2 = node[j].x+my;
y2 = node[j].y-mx;
if( judge(x1,y1) && judge(x2,y2))
{
ans++;
}
}
printf("%d\n",ans/);
}
return ;
}
poj Squares n个点,共能组成多少个正方形 二分 + 哈希的更多相关文章
- POJ 2774 后缀数组 || 二分+哈希
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 35607 Accepted: 14 ...
- poj 2002 Squares 几何二分 || 哈希
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 15137 Accepted: 5749 Descript ...
- POJ 3273-Monthly Expense 求分组和的最小的最大值【二分答案】
题目链接:http://poj.org/problem?id=3273 题目大意:给出一个有n个数据的数组,将其分为连续的m份,找到一种分法,是的m份中最大一份总和最小 解题思路: 直接在答案的区间内 ...
- POJ 3294 Life Forms [最长公共子串加强版 后缀数组 && 二分]
题目:http://poj.org/problem?id=3294 Life Forms Time Limit: 5000MS Memory Limit: 65536K Total Submiss ...
- POJ 3155 Hard Life 最大密度子图 最大权闭合图 网络流 二分
http://poj.org/problem?id=3155 最大密度子图和最大权闭合图性质很相近(大概可以这么说吧),一个是取最多的边一个是取最多有正贡献的点,而且都是有选一种必须选另一种的限制,一 ...
- POJ 3525/UVA 1396 Most Distant Point from the Sea(二分+半平面交)
Description The main land of Japan called Honshu is an island surrounded by the sea. In such an isla ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- POJ 3525 Most Distant Point from the Sea (半平面交+二分)
Most Distant Point from the Sea Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3476 ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
随机推荐
- iOS开发--地图与定位
概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用 和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一 ...
- [Linux] Linux软连接和硬链接
转载自:http://www.cnblogs.com/itech/archive/2009/04/10/1433052.html 1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard ...
- asp.net membership 修改密码
aspnet_Applications 存储数据库所涉及应用程序有关信息. aspnet_Membership 存储与用户相关信息例如:用户登陆密码,创建时间. aspnet_Paths 存储应用程序 ...
- RMAN 还原与恢复
一. RMAN 还原与恢复基础 在RMAN 用于中,还原与恢复是两个不同的概念.还原(restore):指访问先前生成的备份,从中得到一个或多个对象,然后在磁盘上的某个位置还原这些对象.恢复(reco ...
- python中常用的推导(字典推导和列表推导)
在python开发中经常需要书写这样的代码 result = list() for data in datas: if data not in ['a', 'b']: result.append(da ...
- DevExpress控件使用小结
摘自: http://blog.sina.com.cn/s/blog_95cfa64601019wex.html .TextEditor(barEditItem)取文本 string editValu ...
- (转)NIO 文件锁定
文件锁定 概述 文件锁定初看起来可能让人迷惑.它 似乎 指的是防止程序或者用户访问特定文件.事实上,文件锁就像常规的 Java 对象锁 ― 它们是 劝告式的(advisory) 锁.它们不阻止任何形式 ...
- 在weblogic上配置数据源
转自:http://blog.csdn.net/weijie_search/article/details/2756585 旁白 这是在weblogic9.0+mysql5.1的环境下配置的数据源.其 ...
- Win7开机提示group policy client无法登陆怎么办
1 开机按F8,进入安全模式 2 进入系统之后运行注册表,定位到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Prof ...
- ffmpeg相关资源
FFPLAY的原理(一) http://blog.csdn.net/shenbin1430/article/details/4291893 ubuntu12.04下命令安装ffplay等: sudo ...