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 ...
随机推荐
- Shadow Map 原理和改进 【转】
http://blog.csdn.net/ronintao/article/details/51649664 参考 1.Common Techniques to Improve Shadow Dept ...
- 十个书写Node.js REST API的最佳实践(下)
收录待用,修改转载已取得腾讯云授权 5. 对你的Node.js REST API进行黑盒测试 测试你的REST API最好的方法之一就是把它们当成黑盒对待. 黑盒测试是一种测试方法,通过这种方法无需知 ...
- 纯 PHP 代码最好在文件末尾删除 PHP 结束标记
如果文件内容是纯 PHP 代码,最好在文件末尾删除 PHP 结束标记.这可以避免在 PHP 结束标记之后万一意外加入了空格或者换行符,会导致 PHP 开始输出这些空白,而脚本中此时并无输出的意图. & ...
- 突破防盗链机制:使用referrer-killer
在开发it博客汇的过程中遇到一个难题:很多图片链接设置了防盗链机制,从我的网站请求图片会返回403错误,但直接在浏览器中打开图片的url时却又正常. 使用fiddler抓包发现,从我的网站请求图片会带 ...
- 2017.12.14 Mybatis物理分页插件PageHelper的使用(一)
参考来自: http://www.360doc.com/content/15/0728/15/12642656_487954693.shtml https://www.cnblogs.com/digd ...
- CocoSourcesCS 1
CocoSourcesCS 1 /*------------------------------------------------------------------------- Compiler ...
- 接口测试框架开发(二):extentreports报告中文乱码问题
转载:http://www.cnblogs.com/lin-123/p/7146935.html 问题:中文乱码 问题解决:eclipse设置编码为utf-8 结果:
- .Net Framework 之 框架图
.Net Framework框架图,如下图: 它表明了这么一种编写软件的方式或者说表明了.Net平台下开发软件的思想和规范. .Net Framework框架实际只包含两部分: 1.公共语言运行时( ...
- Eclipse中,快捷键使用总结
(1)Alt+shift+L:new ReadItem().readItems(file);的返回对象是Map<String,String>用这个快捷键有两个效果示例1:输入光标停在new ...
- Firefly 其他博客
http://www.cnblogs.com/9miaoshetuan/tag/Firefly/ http://www.cnblogs.com/9miaoshetuan/p/3853124.html ...