题目链接: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. spoj 390

    简单题  记得uva上有个一样的  画个图就好了 #include <cstdio> #include <cmath> const double pi = acos(-1); ...

  2. Objective-C 协议(protocol)

    协议(protocol)是Objective-c中一个非常重要的语言特性,从概念上讲,非常类似于JAVA中接口. 一个协议其实就是一系列有关联的方法的集合(为方便后面叙述,我们把这个协议命名为myPr ...

  3. hdu 4558 剑侠情缘

    思路:dp[i][j][k]表示在点(i,j)处能量的差值为k的方案数 转移的时候把差值取相反数就实现轮流了 代码如下: #include<iostream> #include<st ...

  4. Git教程之使用GitHub

    我们一直用GitHub作为免费的远程仓库,如果是个人的开源项目,放到GitHub上是完全没有问题的.其实GitHub还是一个开源协作社区,通过GitHub,既可以让别人参与你的开源项目,也可以参与别人 ...

  5. autocapticalize和autocorrect

    首字母自动大写autocapitalize 在 iOS 中,用户可以手动开启「首字母自动大写」功能,这样输入英文的时候,首字母便会自动大写.但是,有些时候并不希望一直是首字母大写的.比如用户名这个字段 ...

  6. PHP判断日期是不是今天 判断日期是否为当天

    <?php /** * PHP判断一个日期是不是今天 * 琼台博客 */ echo '<meta charset="utf-8" />'; // 拟设一个日期 $ ...

  7. HBase 的安装与配置

    实验简介 本次实验学习和了解 HBase 在不同模式下的配置和安装,以及 HBase 后续的启动和停止等. 一.实验环境说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shi ...

  8. Shell中判断字符串是否为数字的6种方法分享

    #!/bin/bash ## 方法1 a=1234;echo "$a"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ] &&a ...

  9. apache开源项目--Mahout

    Apache Mahout 是 Apache Software Foundation (ASF) 开发的一个全新的开源项目,其主要目标是创建一些可伸缩的机器学习算法,供开发人员在 Apache 在许可 ...

  10. Java [leetcode 38]Count and Say

    题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...