hdu1007(二分法)

Sad Love Story

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1572    Accepted Submission(s): 493


Problem Description
There's a really sad story.It could be about love or about money.But love will vanish and money will be corroded.These points will last forever.So this time it is about points on a plane.
We have a plane that has no points at the start.
And at the time i,we add point p i(x i, y i).There is n points in total.
Every time after we add a point,we should output the square of the distance between the closest pair on the plane if there's more than one point on the plane.
As there is still some love in the problem setter's heart.The data of this problem is randomly generated.
To generate a sequence x 1, x 2, ..., x n,we let x 0 = 0,and give you 3 parameters:A,B,C. Then x i = (x i-1 * A + B) mod C.
The parameters are chosen randomly.
To avoid large output,you simply need output the sum of all answer in one line.
 

Input
The first line contains integer T.denoting the number of the test cases.
Then each T line contains 7 integers:n A x B x C x A y B y C y.
A x,B x,C x is the given parameters for x 1, ..., x n.
A y,B y,C y is the given parameters for y 1, ..., y n.
T <= 10.
n <= 5 * 10 5.
10 4 <= A,B,C <= 10 6.
 

Output
For each test cases,print the answer in a line.
 

Sample Input
  
2 5 765934 377744 216263 391530 669701 475509 5 349753 887257 417257 158120 699712 268352
 

Sample Output
  
8237503125 49959926940
Hint
If there are two points coincide,then the distance between the closest pair is simply 0.
 

Source
 

Recommend

zhuyuanchen520   |   We have carefully selected several similar problems for you:  4812 4811 4810 4809 4808 

      本题是个典型的最小点对问题。当问题规模较小时,可以直接二重循环模拟,当本题数据量较大,应该二分做。先二分再合并,注意优化,本题时间复杂度控制在O(N*log(N)),结果也1s多过了。

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

double const eps=1e-8;
double const INF=1e20;
const int MAXN=100000+100;
struct point
{
	double x,y;
	point()
	{}
	point(double _x,double _y)
	{
		x=_x;
		y=_y;
	}
}MyPoint[MAXN];
int n;
int Tarray[MAXN];

bool cmp(point a,point b)
{
	if(fabs(a.x-b.x)>eps)
		return a.x<b.x;
	return a.y<b.y;
}

bool cmpX(point a,point b)
{
	return a.x<b.x;
}

bool cmpY(int a,int b)
{
	return MyPoint[a].y< MyPoint[b].y;
}


double GetDistance(point a,point b)
{
	double dx=a.x-b.x,dy=a.y-b.y;
	return sqrt(dx*dx+dy*dy);
}


double Solve(int left,int right)
{
	if(0==right-left)
		return INF;
	else if(1==right-left)
		return  GetDistance(MyPoint[left],MyPoint[right]);
	int mid=(left+right)>>1;
	double ret=min(Solve(mid+1,right),Solve(left,mid));
	int cnt=0;
	for(int i=left;i<=right;i++)
	{
		if(fabs(MyPoint[mid].x-MyPoint[i].x)<=ret)
			Tarray[cnt++]=i;
	}

	sort(Tarray,Tarray+cnt,cmpY);

	for(int i=0;i<cnt;i++)
	{
		for(int j=i+1;j<cnt;j++)
		{
			if(GetDistance(MyPoint[Tarray[i]],MyPoint[Tarray[j]])>ret)break;
			ret=min(ret,GetDistance(MyPoint[Tarray[i]],MyPoint[Tarray[j]]));
		}
	}
	/*
	int low=mid+1,high=right,midmid;
	while(low<=high)
	{
		midmid=(low+high)>>1;
		if(MyPoint[midmid].x-MyPoint[mid+1].x>ret)
			high=midmid-1;
		else if(MyPoint[midmid].y-MyPoint[mid+1].y>ret)
			high=midmid-1;
		else low=midmid+1;
	}
	if(GetDistance(MyPoint[mid+1],MyPoint[midmid]))
	*/
	return ret;
}

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

		sort(MyPoint,MyPoint+n,cmpX);
		printf("%0.2lf\n",Solve(0,n-1)/2.0);
	}
	system("pause");
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值