题目链接: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个点,共能组成多少个正方形 二分 + 哈希的更多相关文章

  1. POJ 2774 后缀数组 || 二分+哈希

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 35607   Accepted: 14 ...

  2. poj 2002 Squares 几何二分 || 哈希

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15137   Accepted: 5749 Descript ...

  3. POJ 3273-Monthly Expense 求分组和的最小的最大值【二分答案】

    题目链接:http://poj.org/problem?id=3273 题目大意:给出一个有n个数据的数组,将其分为连续的m份,找到一种分法,是的m份中最大一份总和最小 解题思路: 直接在答案的区间内 ...

  4. POJ 3294 Life Forms [最长公共子串加强版 后缀数组 && 二分]

    题目:http://poj.org/problem?id=3294 Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submiss ...

  5. POJ 3155 Hard Life 最大密度子图 最大权闭合图 网络流 二分

    http://poj.org/problem?id=3155 最大密度子图和最大权闭合图性质很相近(大概可以这么说吧),一个是取最多的边一个是取最多有正贡献的点,而且都是有选一种必须选另一种的限制,一 ...

  6. 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 ...

  7. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

  8. POJ 3525 Most Distant Point from the Sea (半平面交+二分)

    Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3476   ...

  9. POJ 2785 4 Values whose Sum is 0(折半枚举+二分)

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25675   Accep ...

随机推荐

  1. RES协议

    MFC 通过HTML访问内部资源 资源导入JPG的图片,资源对应ID是137 <img src="res:/JPG/#137" width="100%" ...

  2. Nginx" upstream prematurely closed connection while reading response header from upstream"问题排查

    问题背景 我们这边是一个基于Nginx的API网关(以下标记为A),最近两天有调用方反馈,偶尔会出现502错误,我们从Nginx的error日志里看,就会发现有" upstream prem ...

  3. unity linear work flow

    看了下unity linear space的工作流 srgb read tex deferred gbuffer01  srgb rt float rt----pps float rt 最后 blit ...

  4. 深入理解JS函数节流和去抖动

    一.什么是节流和去抖? 1.节流 节流就是拧紧水龙头让水少流一点,但是不是不让水流了.想象一下在现实生活中有时候我们需要接一桶水,接水的同时不想一直站在那等着,可能要离开一会去干一点别的事请,让水差不 ...

  5. http://blog.csdn.net/zxl315/article/details/10830105

    http://blog.csdn.net/zxl315/article/details/10830105

  6. mysql 跟踪sql执行方法

    摘 自: http://bbs.jee-soft.cn:8086/showtopic-166.aspx 日志是调试程序非常有用的工具. 1  配置my.ini文件(在安装目录,linux下文件名为my ...

  7. Win7如何开启Telnet服务

    http://jingyan.baidu.com/article/870c6fc3cd6fa9b03fe4bee4.html telnet 192.168.1.10 2181

  8. postgresql 如何导入sql文件

    (1)不能使用pgadmin 执行Copy语句,目前客户端还不支持这种写法. (2)打开sql shell,执行如下操作 \i C:/Users/Peter_Youny/Desktop/ischool ...

  9. PHP中is_*() 函数用法

    PHP中is_*() 函数用法 is_a - 如果对象属于该类或该类是此对象的父类则返回 TRUE is_array - 检测变量是否是数组 is_bool - 检测变量是否是布尔型 is_calla ...

  10. vue 仿QQ 开发流程

    技术客栈: vue-cli vue2 vue-router vuex axios stylus webpack2 muse-ui 1.安装脚手架 npm install -g vue-cli 2.开始 ...