HDU 4631 Sad Love Story

本文讨论了一种算法,用于计算在二维平面上,随着点集的动态增加,每步后点集中最近点对的距离平方之和。通过随机生成参数计算点坐标,并利用STL库进行高效查找,最终输出所有步骤的和。

Sad Love Story

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

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
 

题意:一个二维空间上,每次增加一个点,其坐标根据上一个点算出:(x[i-1] * Ax + Bx )  mod Cx,(y[i-1] * Ay + By )  mod Cy求出现有点集中的最近点对的距离的平方,共增加n个点,求每次求得的平方的和( n <= 5*100000, T(T <= 10)组测试数据)。

思路:在已有点的集合中点按x坐标排序,每增一个点,就从这个点开始往右扫,扫到单单横坐标的差的平方 >= Min就跳出,因为加上纵坐标的差的平方肯定 >= Min,且越往右          越大;同理,从这个点的左边第一个点开始往左扫,扫完后统计结果。还是STL的使用,考察的过多的还是编码水平。STL里有很多lower_bound的借口,这个可要搞清楚哦。

#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <set>
#include <limits.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
LL x[500008] ,y[500008] ;
struct Me{
   struct Point{
       LL x ;
       LL y ;
       Point(){}
       Point(int i ,int j):x(i),y(j){}
       friend bool operator <(const Point A ,const Point B){
           return A.x<B.x ;
       }
   };
   int N ;
   Me(){}
   Me(int n):N(n){}
   void read(){
       int a_x ,b_x ,c_x ,a_y ,b_y ,c_y ;
       cin>>a_x>>b_x>>c_x>>a_y>>b_y>>c_y ;
       x[0]=0 ;
       y[0]=0 ;
       for(int i=1;i<=N;i++){
           x[i]=(x[i-1]*a_x+b_x)%c_x ;
           y[i]=(y[i-1]*a_y+b_y)%c_y ;
       }
   }
   LL gao_qi(){
      read() ;
      LL ans=0  ;
      LL Min_dist=100000000000000000LL ;
      multiset<Point>st ;
      st.clear() ;
      multiset<Point>::iterator it ,now;
      st.insert(Point(x[1],y[1])) ;
      for(int i=2;i<=N;i++){
         it=st.lower_bound(Point(x[i],y[i])) ;
         for(now=it;now!=st.end();now++){
            LL d=((now->x)-x[i])*((now->x)-x[i]) ;
            if(d>=Min_dist)
                break ;
            Min_dist=Min(Min_dist,d+((now->y)-y[i])*((now->y)-y[i])) ;
         }
         for(now=it;now!=st.begin();){
            now-- ;
            LL d=((now->x)-x[i])*((now->x)-x[i]) ;
            if(d>=Min_dist)
                break ;
            Min_dist=Min(Min_dist,d+((now->y)-y[i])*((now->y)-y[i])) ;
         }
         st.insert(Point(x[i],y[i])) ;
         ans+=Min_dist ;
      }
      return ans ;
   }
};
int main(){
   int T ,n;
   cin>>T ;
   while(T--){
      cin>>n  ;
      Me me(n) ;
      cout<<me.gao_qi()<<endl ;
   }
   return 0 ;
}

 

    

    

      

转载于:https://www.cnblogs.com/liyangtianmen/p/3227574.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值