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

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

  1 //STATUS:G++_AC_10390MS_23804KB
  2 #include <functional>
  3 #include <algorithm>
  4 #include <iostream>
  5 //#include <ext/rope>
  6 #include <fstream>
  7 #include <sstream>
  8 #include <iomanip>
  9 #include <numeric>
 10 #include <cstring>
 11 #include <cassert>
 12 #include <cstdio>
 13 #include <string>
 14 #include <vector>
 15 #include <bitset>
 16 #include <queue>
 17 #include <stack>
 18 #include <cmath>
 19 #include <ctime>
 20 #include <list>
 21 #include <set>
 22 #include <map>
 23 using namespace std;
 24 //#pragma comment(linker,"/STACK:102400000,102400000")
 25 //using namespace __gnu_cxx;
 26 //define
 27 #define pii pair<int,int>
 28 #define mem(a,b) memset(a,b,sizeof(a))
 29 #define lson l,mid,rt<<1
 30 #define rson mid+1,r,rt<<1|1
 31 #define PI acos(-1.0)
 32 //typedef
 33 typedef __int64 LL;
 34 typedef unsigned __int64 ULL;
 35 //const
 36 const int N=500010;
 37 const int INF=0x3f3f3f3f;
 38 const int MOD=10007,STA=8000010;
 39 const LL LNF=1LL<<55;
 40 const double EPS=1e-8;
 41 const double OO=1e15;
 42 const int dx[4]={-1,0,1,0};
 43 const int dy[4]={0,1,0,-1};
 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 45 //Daily Use ...
 46 inline int sign(double x){return (x>EPS)-(x<-EPS);}
 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
 50 template<class T> inline T Min(T a,T b){return a<b?a:b;}
 51 template<class T> inline T Max(T a,T b){return a>b?a:b;}
 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
 56 //End
 57
 58 struct Node{
 59     LL x,y;
 60     LL id,index;
 61     Node(){}
 62     Node(LL _x,LL _y,LL _index):x(_x),y(_y),index(_index){}
 63 }p[N],nod[N],temp[N];
 64
 65 int n;
 66
 67 LL dist(Node &a,Node &b)
 68 {
 69     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
 70 }
 71
 72 int cmpxy(Node a,Node b)
 73 {
 74     return a.x!=b.x?a.x<b.x:a.y<b.y;
 75 }
 76
 77 int cmpy(Node a,Node b)
 78 {
 79     return a.y<b.y;
 80 }
 81
 82 pii Closest_Pair(int l,int r)
 83 {
 84     if(l==r || l+1==r)return pii(l,r);
 85     LL d,d1,d2;
 86     int i,j,k,mid=(l+r)/2;
 87     pii pn1=Closest_Pair(l,mid);
 88     pii pn2=Closest_Pair(mid+1,r);
 89     d1=(pn1.first==pn1.second?LNF:dist(nod[pn1.first],nod[pn1.second]));
 90     d2=(pn2.first==pn2.second?LNF:dist(nod[pn2.first],nod[pn2.second]));
 91     pii ret;
 92     d=Min(d1,d2);
 93     ret=d1<d2?pn1:pn2;
 94     for(i=l,k=0;i<=r;i++){
 95         if((nod[mid].x-nod[i].x)*(nod[mid].x-nod[i].x)<=d){
 96             temp[k++]=nod[i];
 97         }
 98     }
 99     sort(temp,temp+k,cmpy);
100     for(i=0;i<k;i++){
101         for(j=i+1;j<k && (temp[j].y-temp[i].y)*(temp[j].y-temp[i].y)<d;j++){
102             if(dist(temp[i],temp[j])<d){
103                 d=dist(temp[i],temp[j]);
104                 ret=make_pair(temp[i].id,temp[j].id);
105             }
106         }
107     }
108
109     return ret;
110 }
111
112 void Init()
113 {
114     int i;
115     LL x,y,Ax,Bx,Cx,Ay,By,Cy;
116     cin>>n>>Ax>>Bx>>Cx>>Ay>>By>>Cy;
117     x=y=0;
118     for(i=0;i<n;i++){
119         x=(x*Ax+Bx)%Cx;
120         y=(y*Ay+By)%Cy;
121         p[i]=Node(x,y,i);
122     }
123 }
124
125 int main(){
126  //   freopen("in.txt","r",stdin);
127     int T,i,j,k;
128     LL ans,hig;
129     scanf("%d",&T);
130     while(T--)
131     {
132         Init();
133
134         int end=n;
135         pii t;
136         ans=0;
137         while(end>0){
138             for(i=0;i<end;i++)nod[i]=p[i];
139             sort(nod,nod+end,cmpxy);
140             for(i=0;i<end;i++)nod[i].id=i;
141             t=Closest_Pair(0,end-1);
142             hig=Max(nod[t.first].index,nod[t.second].index);
143             ans+=(end-hig)*dist(nod[t.first],nod[t.second]);
144             end=hig;
145         }
146         cout<<ans<<endl;
147     }
148     return 0;
149 }

转载于:https://www.cnblogs.com/zhsl/p/3234692.html

HDU-4631 Sad Love Story 平面最近点对相关推荐

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

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

  2. HDU 5721 Palace(平面最近点对(分治))

    http://acm.hdu.edu.cn/showproblem.php?pid=5721 n个点,去掉一个点的情况下,最近距离平方之和. 平面最近点对模版题,先求出最近距离,然后找到是哪两个点,然 ...

  3. CF429D Tricky Function(求解公式、经分析转为求平面最近点对、思维)

    整理的算法模板合集: ACM模板 目录 CF429D Tricky Function 题意实际上就是给定长度为 nnn 的一串序列a1,a2,...,ana_1, a_2,...,a_na1​,a2​ ...

  4. POJ3714 Raid 平面最近点对

    利用分治来求平面最近点对 只需要查后面6个点就好了 原因在于https://blog.csdn.net/liufeng_king/article/details/8484284 两个集合的话就把不同集 ...

  5. 平面最近点对问题(分治)

    题目描述 在与联盟的战斗中屡战屡败后,帝国撤退到了最后一个据点. 依靠其强大的防御系统,帝国击退了联盟的六波猛烈进攻. 经过几天的苦思冥想,联盟将军亚瑟终于注意到帝国防御系统唯一的弱点就是能源供应. ...

  6. 平面最近点对问题求解—基于Java语言

    平面最近点对问题求解-基于Java语言 1. 问题描述: 本问题来自<编程之美2.11-寻找最近点对>,文中给出了两种解法:暴力解法,分治解法.其中,暴力解法很简单,求出所有点之间的距离并 ...

  7. 分治法求平面最近点对入门

    一.平面最近点对问题. 平面最近点对:在一个平面上有 n n n个点,求出距离最近的两个点. 平面最近点对是计算几何中一个十分经典且基础的问题,通常采用分治法来解决. 二.直线最近点对的分治法. 在用 ...

  8. 【算法分析与设计】平面最近点对(含最近距离、最近点对、第一次分割点集合的输出)

    [问题描述] 给定二维平面上n个点,找其中的一对点,使得在n个点组成的所有点对中,该点对间的距离最小.使用递归与分治策略求解二维平面上的最接近点对问题.假设所有点的集合为S,m为S中所有点的x坐标的中 ...

  9. HDU1007 查找平面最近点对

    求点集中的最近点对有以下两种方法: 设p1=(x1, y1), p2=(x2, y2), -, pn=(xn, yn)是平面上n个点构成的集合S,设计算法找出集合S中距离最近的点对. OJ题目:HDU ...

最新文章

  1. Python入门100题 | 第047题
  2. 运行命令对电脑的操作(Windows + R)
  3. Goodbye 2017 B
  4. 《openssl 编程》之 DH
  5. 修改GIT的user.name和user.email
  6. t-sql里Linked server跨数据库查询
  7. 微软称开源.NET吸引了更多开发者
  8. 【LeetCode笔记】207. 课程表(Java、图、BFS、队列)
  9. Qt实践| HTTP知识点-接入某图片验证码系统查询余额
  10. css sprite原理优缺点及使用
  11. php wiki搭建,wiki网站搭建
  12. 结合CmakeList来更好地理解windows下的动态库和静态库
  13. Codeforces Round #253 DIV1 C 馋
  14. cad插件_CAD插件自动标注安装教程
  15. 获取level2行情接口的功能详解
  16. 【心悟水浒】带团队懂管理
  17. 浩辰CAD 2019 v190128官方免费版
  18. 智慧园区一体化信息管理平台设计方案
  19. linux权限750什么意思,chmod 777是什么意思?为您解释chmod 777及切勿使用chmod 777的原因...
  20. windows 查找目录下文件中包含某个字符串

热门文章

  1. 创新数字音频处理技术带来消费电子产品差异化用户体验(转)
  2. UVA 11198 Dancing Digits
  3. python unittest断言_python unittest之断言及示例
  4. HBase之Region Compact流程分析
  5. (68)信号发生器DDS协议(第14天)
  6. (24)VHDL实现与或非(行为描述)
  7. (11)Verilog HDL变量:wire型
  8. (03)System Verilog 常用数据类型详解
  9. (50)FPGA面试技能提升篇(版本控制工具Clearcase、Git)
  10. python堆排序算法_Python算法学习之堆和堆排序