Garden Gathering Gym - 100792G(求距离最远的两个点)

题目链接:http://codeforces.com/gym/100792/problem/G

题意:给出n个坐标整数点,求出距离最远的两个点.两点之间的路径为只能走整数点.

思路:显然两点之间的路径为
sqrt(2)*min(|x1-x2|,|y1-y2|)+max(|x1-x2|,|y1-y2|)-min(|x1-x2|,|y1-y2|) =
(sqrt(2)-1)*min(|x1-x2|,|y1-y2|)+max(|x1-x2|,|y1-y2|)
去掉绝对值为: max((sqrt(2)-1)*|x1-x2|+|y1-y2|,(sqrt(2)-1)*|y1-y2|+|x1-x2|)
即在(sqrt(2)-1)*x+y和(sqrt(2)-1)*y+x 的最远曼哈顿距离运算中取最大值.

 

#include
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5+5;
const double eps = 1e-8;

struct node
{
	double p[2];
} a[maxn];

int n;
double es;

double solve(int x,int &u,int &v)
{
	for(int i = 1;i<= n;i++) a[i].p[x]*= es;
	double dis[5][2];
	int ans[5][2];
	for(int i = 0;i< 5;i++) dis[i][0] = -1e15,dis[i][1] = 1e15;
	for(int i = 1;i<= n;i++)
	{
		for(int j = 0;j< 1<<2;j++)
		{
			double tmp = 0.0;
			for(int k = 0;k< 2;k++)
			{
				if(j&(1< dis[j][0])
			{
				dis[j][0] = tmp;
				ans[j][0] = i;
			}
			if(tmp< dis[j][1])
			{
				dis[j][1] = tmp;
				ans[j][1] = i;
			}
		}
	}
	
	double ma = 0.0;
	for(int j = 0;j< 1<<2;j++)
	{
		if(ma< dis[j][0]-dis[j][1])
		{
			ma = dis[j][0]-dis[j][1];
			u = ans[j][0];
			v = ans[j][1];
		}
	}
	for(int i = 1;i<= n;i++) a[i].p[x]/= es;
	return ma;
}

int main()
{
	cin>>n;
	for(int i = 1;i<= n;i++)
		scanf("%lf %lf",&a[i].p[0],&a[i].p[1]);
	
	es = sqrt(2.0)-1.0;
	
	int a1,a2,a3,a4;
	double ans1 = solve(0,a1,a2);
	double ans2 = solve(1,a3,a4);
	
	if(ans1> ans2) printf("%d %d\n",a1,a2);
	else printf("%d %d\n",a3,a4);
	
	return 0;
} 

 

你可能感兴趣的:(几何问题)