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 ...
随机推荐
- ALCHEMY 2 (FLASCC)新手入门 (WINDOWS 版)
Adobe Alchemy(炼金术) 2的预发布版本已经对开发者开放,并且已经更名为 FlasCC.炼金术简单来说就是把c/c 代码编译成swf文件,它吸收了c/c 高效的执行效率,比传统开发的swf ...
- Unity-EasyTouch插件之One Finger
这节课,我们主要讲下单个手指的测试.比如单击啊,双击啊,拖动,单手滑动等. 单击: public class TouchTest : MonoBehaviour { // Subscribe to e ...
- saltstack常用语法
一.常用语法 1.添加用户 示例1: #添加zabbix用户和组 zabbix: group.present: - name: zabbix - gid: 1001 user.present: - f ...
- 非意外的PDB错误 OK(0)
用ib编项目会出现这个error 用vs重新编译全部 就没有问题 ib的设置改下 Visual Studio Builds--Advanced --PDB File Allocation Force ...
- Mac 清除/修改SSH的私钥密码
我之前在Mac下用命令ssh-keygen生成密钥,并且安全起见为密钥设置了密码,这样导致后来每次我git push时,都要输入一次密码: Enter pass phrase for /Users/z ...
- AllJoyn 的JoinSession() 返回timeout问题
在项目中AllJoyn一直有个问题困扰着我们:client在加入session时调用JoinSession()函数有时会timeout失败. 注意:是“有时”失败,而有时又运行的很好.这种不确定性问题 ...
- [Android Pro] Android中全局Application的onCreate多次调用问题
一般来说Application的onCreate方法只会执行一次, 如果应用中采用多进程方式,oncreate方法会执行多次,根据不同的进程名字进行不同的初始化, 就是在application中多添加 ...
- (六)SSO之CAS框架扩展 改动CAS源代码实现与ESS动态password验证对接
题记: 偶尔的偶尔我们会听到这个站点的数据泄露了,那个站点的用户数据泄露了.让用户又一次改动登录password,所以,对于用户数据安全性越发的引起我们的重视了,尤其是一些保密性要求高的站点.更须要添 ...
- VLC播放RTSP视频延迟问题
VLC播放RTSP视频延迟问题 配置 VLC 以播放 RTSP/RTP 流 实测发现RTP都不如TCP快? vlc播放rtp封装的h.264延时很大是什么原因? 开启打印: VLC的工具->消息 ...
- Java调用本地接口:java.lang.UnsatisfiedLinkError
Java调用本地接口:java.lang.UnsatisfiedLinkError 我的问题不在这篇文章描述中, 而是因为jni原来是c实现, 现在切换到cpp了, 需要在对应的cpp文件中加入ext ...