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 ...
随机推荐
- sso 自动化运维平台
单点登录SSO(Single Sign-On)是身份管理中的一部分.本文中作者开发了一个自动化运维平台中的统一认证接口,单点登录平台通过提供统一的认证平台,实现单点登录.因此,应用系统并不需要开发用户 ...
- latex不能识别eps图片
latex中可以使用.eps的图片,许多文档都介绍了怎么引用这种格式的图片,但没有给出使用过程中的注意事项.我在使用MIKTEX的时候,latex文档中引入.eps图片遇到了这样的问题.编译的时候显示 ...
- iOS: 工具栏控件UIToolBar和工具栏按钮控件UIBarButtonItem的使用
一.工具栏控件:UIToolBar:UIView 介绍: ToolBar工具栏是视图View的属性,可以在工具栏上添加工具栏按钮Bar Button Item(可以是自定义的Custom.也可以是系统 ...
- LeetCode——3Sum & 3Sum Closest
3Sum 题目 Given an array S of n integers, are there elements a,b,c in S such that a + b + c = 0? Find ...
- SQL通过身份证获取信息
SELECT t.identity_number '身份证号',SUBSTR(t.identity_number,1,2) AS "省份",SUBSTR(t.identity_nu ...
- js 在表单提交前进行操作
最近在写页面的时候,需要手动写一些在表单进行提交前的验证操作,正好看到了2种阻止表单提交的方法,可以进行一些逻辑处理 方法一:使用return false 原生js写法: <form id=&q ...
- liunx系统安装jdk的方法
1.下载jdk 下载地址: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads ...
- async -- await 解决数据异步获取
在React组件中,也比较一下 Promise 和 Async/Await 的方法异同. 传统地使用 Promise : import React, { Component } from 'react ...
- 禁止chrome自动更新
删除C:\Program Files (x86)\Google文件夹下面的updata文件夹
- JavaScript | window浏览器对象模型
Js Window - 获取浏览器窗口 全局变量是window对象的属性 全局函数是window对象的方法 HTML DOM的document是window对象属性之一 window.document ...