poj_2002

源链接:http://poj.org/problem?id=2002

题目大意就是给出n个二维平面上的坐标,求出这n个坐标最多能够构成多少个正方形。

通过n最大为1000可以推测,应该是n^2左右的效率,所以我们先枚举两个点,然后通过这两个点再求出另外两个点的坐标,在输入的集合中查找是否存在这两个点,如果存在的话就可以构成一个正方形。

但是枚举也有一定的方法,我们不能随便枚举,因为有很多种形状的正方形,我们也不知道这两个点是正方形的哪两个点。首先,我们要先将所有的点按照x,y的大小排好序,然后遍历的时候取a[i]和a[j]两个点,且i<j,即a[i].x<a[j].x || a[i].x==a[j].x&&a[i].y<a[j].y。这样子我们就可以自定义是哪两个点:


比如这么一个正方形,我们已知1,2两个点的坐标为(a1,a2),(b1,b2),那么我们怎么求得3,4两个点的坐标那?其实正方形有它的特殊性。


我们可以看出,上下做的两个三角形是全等的,所以说对于点3,点3的坐标为(a1+(b2-a2),a2-(b1-a1)),同理,点4的坐标为(b1+(b2-a2),b2-(b1-a1))。知道了这两个点,就可以去找了。

但是如果会问,那么如果遍历到的是1,3怎么办?其实不用管,因为这个是按从小到大来遍历的,那么1,2两个点肯定会被遍历到,就不用管1,3了。还有一点,如果我们遍历到的是2,4两个顶点,那么计算1,3两个定点的时候,计算公式是和知道1,3一样的。所以,对于每个四边形,我们都遍历了两遍,最后除以2即可。

对于查找的算法,我们可以用哈希或者二分即可。

先是哈希:

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<map>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
#define ll __int64
int n,m,t;
#define Mod 1000000007
#define N 110
#define M 1000100
struct Squares{
	int x,y;
	bool operator <(const Squares &s) const{
			return (x<s.x || (x==s.x && y<s.y));
	}
}a[1010];
int hash[M];
int next[1010];
bool check(int x,int y){
	int key = abs(x+y);
	int id = hash[key];
	while(id != -1){
		if(a[id].x == x && a[id].y == y)
			return true;
		id = next[id];
	}
	return false;
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("out.txt","w",stdout);
#endif
    	while(sf(n)!=EOF && n){
    		for(int i=0;i<n;i++)
    			sfd(a[i].x,a[i].y);
    		memset(hash,-1,sizeof hash);
    		memset(next,-1,sizeof next);
    		sort(a,a+n);
    		for(int i=0;i<n;i++){
    			int tmp = abs(a[i].x+a[i].y); //用两个坐标的绝对值和作为哈希值
    			next[i] = hash[tmp];<span style="white-space:pre">	</span>//这个是模仿链式向前星的
    			hash[tmp] = i;
    		}
    		int ans = 0;
    		for(int i=0;i<n;i++){
    			for(int j=i+1;j<n;j++){
    				int py = a[j].y-a[i].y;
    				int px = a[j].x-a[i].x;
    				int xx = a[i].x+py;
    				int yy = a[i].y-px;
    				if(!check(xx,yy))<span style="white-space:pre">	</span>//
    					continue;
    				xx = a[j].x+py;
    				yy = a[j].y-px;
    				if(!check(xx,yy))<span style="white-space:pre">	</span>//
    					continue;
    				ans++;
    			}
    		}
    		printf("%d\n",ans/2);
    	}
return 0;
}

然后是二分法:

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<map>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
#define ll __int64
int n,m,t;
#define Mod 1000000007
#define N 110
#define M 1000100
struct Squares{
	int x,y;
	bool operator <(const Squares &s) const{
			return (x<s.x || (x==s.x && y<s.y));
	}
}a[1010];
bool cmp(int x,int y,Squares node){
	if(x<node.x)
		return true;
	if(x==node.x && y<node.y)
		return true;
	return false;
}
bool check(int x,int y){
	int l=0,r = n;
	int mid;
	while(l<=r){
		mid = (l+r)>>1;
		if(a[mid].x == x &&a[mid].y == y)
			return true;
		if(cmp(x,y,a[mid]))
			r = mid-1;
		else
			l = mid+1;
	}
	return false;
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("out.txt","w",stdout);
#endif
    	while(sf(n)!=EOF && n){
    		for(int i=0;i<n;i++)
    			sfd(a[i].x,a[i].y);
    		sort(a,a+n);
    		int ans = 0;
    		for(int i=0;i<n;i++){
    			for(int j=i+1;j<n;j++){
    				int py = a[j].y-a[i].y;
    				int px = a[j].x-a[i].x;
    				int xx = a[i].x+py;
    				int yy = a[i].y-px;
    				if(!check(xx,yy))
    					continue;
    				xx = a[j].x+py;
    				yy = a[j].y-px;
    				if(!check(xx,yy))
    					continue;
    				ans++;
    			}
    		}
    		printf("%d\n",ans/2);
    	}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值