题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631

  数据是随机的,没有极端数据,所以可以分段考虑,最小值是一个单调不增的函数,然后每次分治算平面最近点对就可以了。。。

 //STATUS:G++_AC_10390MS_23804KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Node{
LL x,y;
LL id,index;
Node(){}
Node(LL _x,LL _y,LL _index):x(_x),y(_y),index(_index){}
}p[N],nod[N],temp[N]; int n; LL dist(Node &a,Node &b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
} int cmpxy(Node a,Node b)
{
return a.x!=b.x?a.x<b.x:a.y<b.y;
} int cmpy(Node a,Node b)
{
return a.y<b.y;
} pii Closest_Pair(int l,int r)
{
if(l==r || l+==r)return pii(l,r);
LL d,d1,d2;
int i,j,k,mid=(l+r)/;
pii pn1=Closest_Pair(l,mid);
pii pn2=Closest_Pair(mid+,r);
d1=(pn1.first==pn1.second?LNF:dist(nod[pn1.first],nod[pn1.second]));
d2=(pn2.first==pn2.second?LNF:dist(nod[pn2.first],nod[pn2.second]));
pii ret;
d=Min(d1,d2);
ret=d1<d2?pn1:pn2;
for(i=l,k=;i<=r;i++){
if((nod[mid].x-nod[i].x)*(nod[mid].x-nod[i].x)<=d){
temp[k++]=nod[i];
}
}
sort(temp,temp+k,cmpy);
for(i=;i<k;i++){
for(j=i+;j<k && (temp[j].y-temp[i].y)*(temp[j].y-temp[i].y)<d;j++){
if(dist(temp[i],temp[j])<d){
d=dist(temp[i],temp[j]);
ret=make_pair(temp[i].id,temp[j].id);
}
}
} return ret;
} void Init()
{
int i;
LL x,y,Ax,Bx,Cx,Ay,By,Cy;
cin>>n>>Ax>>Bx>>Cx>>Ay>>By>>Cy;
x=y=;
for(i=;i<n;i++){
x=(x*Ax+Bx)%Cx;
y=(y*Ay+By)%Cy;
p[i]=Node(x,y,i);
}
} int main(){
// freopen("in.txt","r",stdin);
int T,i,j,k;
LL ans,hig;
scanf("%d",&T);
while(T--)
{
Init(); int end=n;
pii t;
ans=;
while(end>){
for(i=;i<end;i++)nod[i]=p[i];
sort(nod,nod+end,cmpxy);
for(i=;i<end;i++)nod[i].id=i;
t=Closest_Pair(,end-);
hig=Max(nod[t.first].index,nod[t.second].index);
ans+=(end-hig)*dist(nod[t.first],nod[t.second]);
end=hig;
}
cout<<ans<<endl;
}
return ;
}

HDU-4631 Sad Love Story 平面最近点对的更多相关文章

  1. HDU 4631 Sad Love Story 平面内最近点对

    http://acm.hdu.edu.cn/showproblem.php?pid=4631 题意: 在平面内依次加点,求每次加点后最近点对距离平方的和 因为是找平面最近点对...所以加点以后这个最短 ...

  2. hdu 1007 Quoit Design(平面最近点对)

    题意:求平面最近点对之间的距离 解:首先可以想到枚举的方法,枚举i,枚举j算点i和点j之间的距离,时间复杂度O(n2). 如果采用分治的思想,如果我们知道左半边点对答案d1,和右半边点的答案d2,如何 ...

  3. HDU 4631 Sad Love Story (2013多校3 1011题 平面最近点对+爆搞)

    Sad Love Story Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others ...

  4. hdu 4631 Sad Love Story

    http://acm.hdu.edu.cn/showproblem.php?pid=4631 没想到这道题需要用“平均时间复杂度” 计算   一直没有想到解法  因为不符考虑了最坏情况的理念 方法一: ...

  5. kd树解平面最近点对

    早上起来头有点疼,突然就想到能不能用kd树解平面最近点对问题,就找了道题试了一下,结果可以,虽然效率不高,但还是AC了~ 题目链接:http://acm.hdu.edu.cn/showproblem. ...

  6. 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点

    平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...

  7. HDU1007--Quoit Design(平面最近点对)

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

  8. Vijos 1012 清帝之惑之雍正 平面最近点对(分治)

    背景 雍正帝胤祯,生于康熙十七年(1678)是康熙的第四子.康熙61年,45岁的胤祯继承帝位,在位13年,死于圆明园.庙号世宗. 胤祯是在康乾盛世前期--康熙末年社会出现停滞的形式下登上历史舞台的.复 ...

  9. 『Raid 平面最近点对』

    平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...

随机推荐

  1. 宏 #,##,_ _VA_ARGS_ _

    宏里面使用: 一.#  转为字符串 #define PSQR(x) printf("the square of" #x "is %d.\n",(x)*(x)) ...

  2. Debugging with GDB 用GDB调试多线程程序

    Debugging with GDB http://www.delorie.com/gnu/docs/gdb/gdb_25.html GDB调试多线程程序总结 一直对GDB多线程调试接触不多,最近因为 ...

  3. HDU4631+Set+最近点对

    题意:一个空平面,每次增加一个点, 其坐标根据上一个点算出:(x[i-1] * Ax + Bx ) mod Cx,(y[i-1] * Ay + By ) mod Cy 求出现有点集中的最近点对的距离的 ...

  4. 【疯狂Java讲义学习笔记】【流程控制与数组】

    [学习笔记]1.switch语句后的expression表达式的数据类型只能是byte.short.char.int四个整数类型.String(Java 7后才支持)和枚举类型. 2.数组的长度不可变 ...

  5. jdbc操作mysql

    本文讲述2点: 一. jdbc 操作 MySQL .(封装一个JdbcUtils.java类,实现数据库表的增删改查) 1. 建立数据库连接 Class.forName(DRIVER); connec ...

  6. 转:数据包经由路由转发时源、目的IP地址及MAC地址变化情况

    数据包经由路由转发时源.目的IP地址及MAC地址变化情况.  IP数据包经由路由转发的时候源ip,目的ip,源MAC,目的mac是否发生改变,如何改变?   A—–(B1-B2)—–(C1-C2)—— ...

  7. python学习笔记六--用户自定义类

    一.类: 1. 面向对象. 2. 定义了新的对象类型. 定义了两个属性:name,pay 定义了两个方法:lastName,giveRaise

  8. AndroidManifest.xml 详情对应介绍

    ——————————————————————————————————————————————————————————————————————————SETTING设置功能消息免打搅 com.tence ...

  9. linux 进程间消息队列通讯

    转自:http://blog.csdn.net/lifan5/article/details/7588529 http://www.cnblogs.com/kunhu/p/3608589.html 前 ...

  10. php 类 成员变量 $this->name='abc'

    <?php class test { public function getName() { $this->name='abc'; echo $this->name; } }$a=n ...