hdu1007(二分法)

来源:互联网 发布:苏州 软件开发个体户 编辑:程序博客网 时间:2024/06/06 07:45

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 pi(xi, yi).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 x1, x2, ..., xn,we let x0 = 0,and give you 3 parameters:A,B,C. Then xi = (xi-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 Ax Bx Cx Ay By Cy.
Ax,Bx,Cx is the given parameters for x1, ..., xn.
Ay,By,Cy is the given parameters for y1, ..., yn.
T <= 10.
n <= 5 * 105.
104 <= A,B,C <= 106.
 

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

Sample Input
25 765934 377744 216263 391530 669701 4755095 349753 887257 417257 158120 699712 268352
 

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

Source
2013 Multi-University Training Contest 3
 

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;}


 

0 0
原创粉丝点击