BestCoder 2nd Anniversary 1004Hdu 5721 Palace
题意:给定n个点,对于每次消失一个点,问剩下的点中的最短距离是多少,然后所有最短距离的和。
分析:1、模版题,然而没模版的我码了半天。
2、(1)只要不删掉与最短距离相关的两个点,那么最短距离不变。
(2)若与最短距离的点相关,删掉点后,重新算一遍。
/************************************************ Author :DarkTong Created Time :2016/7/18 14:01:14 File Name :d.cpp *************************************************/#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <ctime>#define INF 0x3fffffffffffffff #define esp 1e-9 typedef long long LL; using namespace std; const int maxn = 100000 + 100;typedef LL Int; struct Node {Int x, y;int id;Node(Int x=0, Int y=0, int i=-1):x(x), y(y), id(i){}bool operator==(Node &a){return x==a.x&&y==a.y;} }; vector<Node> poi; //求两点的距离 Int dis(Node &a, Node &b){ return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y); } //各种按不同优先级排序的排序函数 int cmpx(const Node &a, const Node &b){ return a.x==b.x ? a.y<b.y : a.x<b.x;} int cmpy(const Node &a, const Node &b){ return a.y==b.y ? a.x<b.x : a.y<b.y;} int cmpi(const Node &a, const Node &b){ return a.id<b.id;}int ans1, ans2; int n, xxx; LL ansd;/*****************************************/ /*合并两个集合的点,找出最短距离的两个点 */ /*****************************************/ void Merger(int L, int R, int m, Int &ans) {int tl, tr, t;//选出右边离中心线距离<=max(dl, dr)的点tl=tr=m;while(tr<R&&poi[tr].x-poi[tl].x<=ans) tr++;sort(poi.begin()+tl, poi.begin()+tr, cmpy);int ta1, ta2;//枚举左边的点,选出与其最近的点。for(int i=L;i<m;++i){if(poi[i].id==xxx) continue;int l, r;r = upper_bound(poi.begin()+tl, poi.begin()+tr, poi[i], cmpy) - poi.begin();l = max(tl, r-3); r = min(tr, r+3);for(int j=l; j<r;++j){if(poi[j].id==xxx) continue;if(dis(poi[j], poi[i]) < ans){ans = dis(poi[j], poi[i]);ta1=poi[i].id; ta2=poi[j].id;}}}//为了得到最近两点的id值if(ans < ansd) { ansd = ans; ans1 = ta1; ans2 = ta2;}//恢复sort(poi.begin()+tl, poi.begin()+tr, cmpx); } Int MergerSolve(int L, int R) {int m;Int dl, dr;//边界条件if(R-L<=3){int ta1, ta2;Int ans=INF;for(int i=L;i<R;++i) {if(poi[i].id==xxx) continue;for(int j=i+1;j<R;++j){if(poi[j].id==xxx) continue;if(dis(poi[i], poi[j])<ans) { ans=dis(poi[i], poi[j]); ta1=poi[i].id; ta2=poi[j].id;}}}//用于记录最短距离的那两个点的坐标if(ans < ansd){ ansd=ans; ans1=ta1; ans2=ta2;}return ans;}m = (L+R)>>1;dl=MergerSolve(L, m);dr=MergerSolve(m, R);Int ans = min(dl, dr);//合并操作 Merger(L, R, m, ans);return ans; }int main() {int ca;scanf("%d", &ca);while(ca--){scanf("%d", &n);int x, y;for(int i=0;i<n;++i){scanf("%d%d", &x, &y);poi.push_back(Node(x, y, i));}sort(poi.begin(), poi.end(), cmpx);xxx = -1; ansd = INF;LL ttans = MergerSolve(0, n)*(n-2);int ta1 = ans1, ta2 = ans2;xxx = ta1; ansd=INF;ttans += MergerSolve(0, n);xxx = ta2; ansd=INF;ttans += MergerSolve(0, n);cout<<ttans<<endl;poi.clear();}return 0; }
转载于:https://www.cnblogs.com/DarkTong/p/5683138.html
BestCoder 2nd Anniversary 1004Hdu 5721 Palace相关推荐
- BestCoder 2nd Anniversary
http://acm.hdu.edu.cn/search.php?field=problem&key=BestCoder+2nd+Anniversary&source=1&se ...
- BestCoder 1st Anniversary ($) 1002.Hidden String
Hidden String Accepts: 437 Submissions: 2174 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 26 ...
- HDU 5721 Palace(平面最近点对(分治))
http://acm.hdu.edu.cn/showproblem.php?pid=5721 n个点,去掉一个点的情况下,最近距离平方之和. 平面最近点对模版题,先求出最近距离,然后找到是哪两个点,然 ...
- AJAX 在中文社区的“集体信仰动摇”?!
最近好像在中文社区又多了关于AJAX的讨论,很多都是说AJAX仅仅是过渡技术,又或者说AJAX现在有些滥用了,反正就好像一场狂热下来大家又成了怀疑论者了. Microsoft Expression 看 ...
- hdu 5311 Hidden String(find,substr)
Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a str ...
- 维基链WICC | 项目进展周报第57期(1.6~1.12)
技术 & 产品周报 公链建设 交易手续费阶梯收费.100% 实现了回滚数据获取的RPC接口.100% 追加抵押支持未支付的利息转化成债务.100% 优化了中位数价格计算,提升性能.100% 完 ...
- hdu 5312 Sequence(数学推导——三角形数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others) ...
- hdu 5312 打表加特判
Problem Description Today, Soda has learned a sequence whose n -th (n≥1) item is 3n(n−1)+1 . Now ...
- Summary of the monthly meeting in January and February
March 8th Women's Day, Jinmaoyuan also has a festival You rarely have a lover with the same paragrap ...
最新文章
- 机器学习(14)逻辑回归(理论)
- 简易git操作 -- 让你的格子绿起来
- 使用 PowerShell 创建 Linux 虚拟机
- python浪漫代码-python七夕浪漫表白源码
- .NET中的正则表达式 (三)RegexCompilationInfo 类
- python random用法_python random 的用法
- lisp医院化验系统_浅谈医院化验室信息系统(LIS)的建设
- bluetooth射频已关闭请打开bluetooth射频_【05/25 - 公告】微软已推送 Build 18362.145 (KB4497935) 版本...
- c语言程序设计个人作业,C语言程序设计-作业集.doc
- 大工计算机基础在线作业答案,大工1209《计算机应用基础》在线作业123.doc
- python取非_Python:取非矩形区域的GLCM
- MyCAT与MySQL导入、导出文件
- 计算机界面没磁盘驱动器,驱动器中没有磁盘的原因和解决办法
- 转录组RNA-seq分析前沿进展综述
- Uncle-黄,恍恍惚惚的面经 -- 1(初级Java)
- PIC单片机c语言休眠,PIC16F72 休眠程序
- excel文件下载下来损坏 js_js文件操作之——导出Excel (js-xlsx)
- 如何画好软件架构图?为什么说我们需要软件架构图?
- perforce(p4) 使用
- 动画讲解—— CSS
热门文章
- 上海盐城生物php招聘_上海祥源生物科技招聘国际商务专员,中英文熟练
- java app支付_Java 微信支付之APP支付服务端 (一)
- 无法支持python程序的编辑运行和调试_解决pycharm的Python console不能调试当前程序的问题...
- OpenCV-Python实战(21)——OpenCV人脸检测项目在Web端的部署
- primefaces教程_Primefaces,带有JPA的Spring 4(Hibernate 4 / EclipseLink)示例教程
- Android CheckBox
- c语言中关键字static_了解C ++中Static关键字的变体
- C语言和C++的区别是什么?到底学哪种好
- Docker容器实现原理及容器隔离性踩坑介绍
- Docker 运行Tensorboard 和 jupyter的正确方法