ZOJ2347 Squares (POJ2002 二分查找)

给 N 个点,任意取其中4个,是否能构成正方形,能的话算一组,求有多少组这样的点,也就是说最多可以找出多少个正方形
方法很简单,确定两个点,必然可以求出正方形的另外两个点,在点集中查找,另外两个点是否存在就可以了。用二分查找,中间加上剪枝,时间可以压到 1s 一下,但是那些 100ms+ 的神人是怎么做到的,就不得而知了……无限Orz……
PS: ZOJ 和 POJ 输入输出格式不太一样,注意啊,好坑爹的……

递归版二分查找,最慢的一种了, ZOJ 跑了4800ms+ 才过,POJ 直接 TL ,掩面啊,泪奔啊……

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define N 1005

struct point 
{
	double x,y;
};
point p[N];
int n;

bool operator ==(point a,point b)//运算符重载
{
	if( a.x==b.x && a.y==b.y )return true;
	return false;
}
bool operator >(point a,point b)
{
	if(a.x>b.x || a.x==b.x && a.y>b.y)return true;
	return false;
}
bool operator <(point a,point b)
{
	if(a.x<b.x || a.x==b.x && a.y<b.y)return true;
	return false;
}

int cmp(const void *a,const void *b)
{
	return *(point *)a > *(point *)b ? 1:-1;
}

int find(point p[],int low,int high,point v)//递归的二分
{
	if(low==high)return -1;
	if(p[low] == v)return low;
	if(p[high] == v)return high;
	int mid=(low+high)/2;
	if( p[mid] == v ) return mid;
	if( v > p[mid] ) return find(p,mid+1,high,v);
	return find(p,low,mid,v);
}

point Whirl(double cosl, double sinl, point a, point b)
// ab 绕 a 逆时针转过角度 A 得到的点,sinl=sinA,cosl=cosA 
{    
	b.x -= a.x; b.y -= a.y;
    point c;
    c.x = b.x * cosl - b.y * sinl + a.x;
    c.y = b.x * sinl + b.y * cosl + a.y;
    return c;
}

int judge(int i,int j)
{
	point pa,pb,po;
	if(i==j)return 0;
	po.x=(p[i].x+p[j].x)/2.0;
	po.y=(p[i].y+p[j].y)/2.0;
	pa=Whirl(0.0,1.0,po,p[i]);//逆时针90度旋转
	pb=Whirl(0.0,1.0,po,p[j]);//顺时针旋转
	if(find(p,0,n,pa)==-1) return 0;
	else if(find(p,0,n,pb)==-1) return 0;
	return 1;
}

int main()
{
	int t,i,j;
	scanf("%d",&t);
	while(t--)
	{
		while(scanf("%d",&n),n)
		{
			for(i=0;i<n;i++)
			scanf("%lf%lf",&p[i].x,&p[i].y);

			qsort(p,n,sizeof(p[0]),cmp);

			int cnt=0;
			for(i=0;i<n;i++)
			{
				for(j=i+1;j<n;j++)
				if(judge(i,j)==1)cnt++;
			}
			printf("%d\n",cnt/2);
		}
		if(t)puts("");
	}
	return 0;
}

后来看人家的二分查找都用 while() 整,换了下,快好多,ZOJ 是 3400ms+ ,POJ 跑了 2900ms+

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define N 1005

struct point 
{
	double x,y;
};
point p[N];
int n;

bool operator ==(point a,point b)//运算符重载
{
	if( a.x==b.x && a.y==b.y )return true;
	return false;
}
bool operator >(point a,point b)
{
	if(a.x>b.x || a.x==b.x && a.y>b.y)return true;
	return false;
}
bool operator <(point a,point b)
{
	if(a.x<b.x || a.x==b.x && a.y<b.y)return true;
	return false;
}

int cmp(const void *a,const void *b)
{
	return *(point *)a > *(point *)b ? 1:-1;
}
bool find(point a,int m)
{
	int begin = 0,end = m-1;
	while( begin <= end )
	{
		int mid = ( begin + end )/2;
		if( a.x==p[mid].x && a.y==p[mid].y )return true;
		if( a.x>p[mid].x || a.x==p[mid].x && a.y>p[mid].y )
			begin = mid + 1;
		else end = mid - 1;
	}
	return false;
}

point Whirl(double cosl, double sinl, point a, point b)
// ab 绕 a 逆时针转过角度 A 得到的点,sinl=sinA,cosl=cosA 
{    
	b.x -= a.x; b.y -= a.y;
    point c;
    c.x = b.x * cosl - b.y * sinl + a.x;
    c.y = b.x * sinl + b.y * cosl + a.y;
    return c;
}

int judge(int i,int j)
{
	point pa,pb,po;
	if(i==j)return 0;
	po.x=(p[i].x+p[j].x)/2.0;
	po.y=(p[i].y+p[j].y)/2.0;
	pa=Whirl(0.0,1.0,po,p[i]);//逆时针90度旋转
	pb=Whirl(0.0,1.0,po,p[j]);//顺时针旋转
	if(!find(pa,n)) return 0;
	else if(!find(pb,n)) return 0;
	return 1;
}

int main()
{
	int i,j;
		while(scanf("%d",&n),n)
		{
			for(i=0;i<n;i++)
				scanf("%lf%lf",&p[i].x,&p[i].y);

			qsort(p,n,sizeof(p[0]),cmp);

			int cnt=0;
			for(i=0;i<n;i++)
			{
				for(j=i+1;j<n;j++)
					if(judge(i,j)==1)cnt++;
			}
			printf("%d\n",cnt/2);
		}
	return 0;
}

也有人用 STL 里面的二分,也挺快的,然后人又加了个剪枝,正方形只算一遍,直接压到 1000ms 左右,贴下学习下

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;
struct node
{
	int x, y;
} p[1001];
bool op(node xx,node yy)
{
	if (xx.x == yy.x)
		return xx.y < yy.y;
	else return xx.x < yy.x;
}
int main()
{
	int points;
	while (scanf("%d", &points), points)
	{
		int sum = 0;
		for (int k = 0; k < points; ++k)
			scanf("%d%d", &p[k].x, &p[k].y);
		sort(p, p + points, op);
		for (int i = 0; i < points; ++i)
		{
			for (int j = i + 1; j < points; ++j)
			{
				if(p[i].x <= p[j].x && p[i].y >= p[j].y)
				{
					node p0, p1;
					p0.x = p[i].x + p[j].y - p[i].y;
					p0.y = p[i].y + p[i].x - p[j].x;
					p1.x = p[j].x + p[j].y - p[i].y;
					p1.y = p[j].y + p[i].x - p[j].x;
					if (!binary_search(p, p+points, p0, op))
						continue;
					if (!binary_search(p, p+points, p1, op))
						continue;
					sum++;
				}
			}
		}
		printf("%d\n", sum);
	}
	return 0;
}


你可能感兴趣的:(c,struct,search,ini)