poj Squares(stl 二分查找 + sort)

#include <algorithm>
#include <cstdio>
using namespace std;
structnode
{
	int x, y;
} p[1005];
bool Greater(const structnode &a, const structnode &b)
{
	if (a.x == b.x)
		return a.y < b.y;
	else
		return a.x < b.x;
}
int main()
{
	int T,i,j,sum;
	node p1, p2;
	while ( scanf("%d", &T)!=EOF && T )
	{
		sum = 0;
		for (i = 0; i < T; ++i)
			scanf("%d%d", &p[i].x , &p[i].y);
		sort(p, p + T, Greater);
		for (i = 0; i < T; i++)
			for (j = i + 1; j < T; j++)
			{
			    if (p[i].y>=p[j].y)    //重复的正方形不要
                {
                    p1.x = p[i].x + p[j].y - p[i].y;
                    p1.y = p[i].y + p[i].x - p[j].x;
                    p2.x = p[j].x + p[j].y - p[i].y;
                    p2.y = p[j].y + p[i].x - p[j].x;
                    if (!binary_search(p, p+T, p1, Greater))    //看看计算出来的点是否在已经排好的点集中存在
                        continue;
                    if (!binary_search(p, p+T, p2, Greater))
                        continue;
                    sum++;
                }
			}
		printf("%d\n", sum );
	}
	return 0;
}

poj Squares(stl 二分查找 + sort)

上一篇:「性能提升」扩展 Spring Cache 支持多级缓存


下一篇:深入剖析阿里巴巴云梯YARN集群