【CF744D】Hongcow Draws a Circle

题意:给你平面上n个红点和m个蓝点,求一个最大的圆,满足圆内不存在蓝点,且至少包含一个红点。

$n,m\le 10^3$

题解:我们先不考虑半径为inf的情况。显然所求的圆一定是要与某个蓝点相切的。我们可以先枚举这个蓝点,然后二分答案。当半径已知、一个点固定时,圆的可能位置只能是绕着一个点旋转得到的结果,其余的所有点都对应着极角上的一段区间,我们可以将这些区间排序,采用扫描线,看一下是否存在一段区间包含红点且不包含蓝点即可。

但是如果你仔细分析的话你会发现这样的二分是不满足单调性的。不过如果我们一开始不光枚举蓝点,还枚举所有红点,一起进行二分,这样就满足单调性了。

直接做的复杂度是$O(n\log ^2 n)$,会TLE,看了标程加了一些神优化才过~具体见代码。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cmath>
  6. #define pi acos(-1.0)
  7. using namespace std;
  8. typedef long double db;
  9. const db eps=1e-12;
  10. const int maxn=1010;
  11. struct point
  12. {
  13. db x,y;
  14. point() {}
  15. point(db a,db b) {x=a,y=b;}
  16. point operator + (const point &a) const {return point(x+a.x,y+a.y);}
  17. point operator - (const point &a) const {return point(x-a.x,y-a.y);}
  18. db operator * (const point &a) const {return x*a.y-y*a.x;}
  19. point operator * (const db &a) const {return point(x*a,y*a);}
  20. }p[maxn<<1];
  21. struct line
  22. {
  23. point p,v;
  24. line() {}
  25. line(point a,point b) {p=a,v=b;}
  26. };
  27. struct node
  28. {
  29. db x;
  30. int k;
  31. node() {}
  32. node(double a,int b) {x=a,k=b;}
  33. }q[maxn<<3];
  34. int n,m,tot;
  35. inline db dis(point a,point b)
  36. {
  37. return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
  38. }
  39. db getrange(point a,point b,db R)
  40. {
  41. db d=dis(a,b)/2;
  42. return acos(d/R);
  43. }
  44. bool cmp(const node &a,const node &b) {return a.x<b.x;}
  45. inline bool solve(int x,db R)
  46. {
  47. int i;
  48. tot=0;
  49. if(x<=n) q[++tot]=node(-pi,1),q[++tot]=node(pi,-1);
  50. else
  51. {
  52. for(i=1;i<=n;i++)
  53. {
  54. if(dis(p[i],p[x])>R+R-eps) continue;
  55. db a=getrange(p[x],p[i],R),b=atan2(p[i].y-p[x].y,p[i].x-p[x].x);
  56. db c=b-a,d=b+a;
  57. if(c<-pi) c+=2*pi;
  58. if(d>pi) d-=2*pi;
  59. if(c<d) q[++tot]=node(c,1),q[++tot]=node(d,-1);
  60. else q[++tot]=node(-pi,1),q[++tot]=node(d,-1),q[++tot]=node(c,1),q[++tot]=node(pi,-1);
  61. }
  62. }
  63. for(i=n+1;i<=n+m;i++)
  64. {
  65. if(dis(p[i],p[x])>R+R-eps) continue;
  66. db a=getrange(p[x],p[i],R),b=atan2(p[i].y-p[x].y,p[i].x-p[x].x);
  67. db c=b-a,d=b+a;
  68. if(c<-pi) c+=2*pi;
  69. if(d>pi) d-=2*pi;
  70. if(c<d) q[++tot]=node(c,-10000),q[++tot]=node(d,10000);
  71. else q[++tot]=node(-pi,-10000),q[++tot]=node(d,10000),q[++tot]=node(c,-10000),q[++tot]=node(pi,10000);
  72. }
  73. sort(q+1,q+tot+1,cmp);
  74. int tmp=0;
  75. for(i=1;i<=tot;i++)
  76. {
  77. if(tmp>0&&i!=1&&q[i].x>q[i-1].x+eps) return 1;
  78. tmp+=q[i].k;
  79. }
  80. return 0;
  81. }
  82. inline bool check(db mid)
  83. {
  84. for(int i=1;i<=n+m;i++) if(solve(i,mid)) return 1;
  85. return 0;
  86. }
  87. inline int rd()
  88. {
  89. int ret=0,f=1; char gc=getchar();
  90. while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
  91. while(gc>='0'&&gc<='9') ret=ret*10+(gc^'0'),gc=getchar();
  92. return ret*f;
  93. }
  94. int main()
  95. {
  96. n=rd(),m=rd();
  97. if(m==1)
  98. {
  99. puts("-1");
  100. return 0;
  101. }
  102. int i;
  103. for(i=1;i<=n;i++) p[i].x=rd(),p[i].y=rd();
  104. random_shuffle(p+1,p+n+1);
  105. for(i=1;i<=m;i++) p[i+n].x=rd(),p[i+n].y=rd();
  106. random_shuffle(p+n+1,p+m+1);
  107. db l=0,r,mid;
  108. for(i=1;i<=n+m;i++) if(solve(i,l)) //神优化
  109. {
  110. r=1e9;
  111. while(r-l>1e-5)
  112. {
  113. mid=(l+r)/2;
  114. if(solve(i,mid)) l=mid;
  115. else r=mid;
  116. }
  117. }
  118. if(l>1e9-1) puts("-1");
  119. else printf("%.18Lf",l);
  120. return 0;
  121. }

【CF744D】Hongcow Draws a Circle 二分+几何的更多相关文章

  1. Incircle and Circumcircle(二分+几何)浙大月赛zoj3806(详解版)图

    Incircle and Circumcircle Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A triangle is o ...

  2. hdu 4033 二分几何

    参考:http://blog.csdn.net/libin56842/article/details/26618129 题意:给一个正多边形内点到其他顶点的距离(逆时针给出),求正多边形的边长 二分多 ...

  3. 二分法 (UVA10668 Expanding Rods)(二分+几何)

    转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1301845324 大致题意: 一根两端固定在两面墙上的杆 受热弯曲后变弯曲.求前后两个状态 ...

  4. 【CF887E】Little Brother 二分+几何

    [CF887E]Little Brother 题意:给你n个圆和一条线段,保证圆和圆.圆和线段所在直线不相交,不相切,不包含.求一个过线段两端点的圆,满足不和任何圆相交(可以相切.包含).问圆的最小半 ...

  5. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A B C D 暴力 水 二分 几何

    A. Vicious Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. 【BZOJ3007】拯救小云公主 二分+几何+对偶图

    [BZOJ3007]拯救小云公主 Description     英雄又即将踏上拯救公主的道路……     这次的拯救目标是——爱和正义的小云公主.     英雄来到boss的洞穴门口,他一下子就懵了 ...

  7. POJ 1905 题解(二分+几何)

    题面 传送门 分析 如图:已知AB=L,弧AB=L(1+nC)" role="presentation" style="position: relative;& ...

  8. POJ2242 The Circumference of the Circle(几何)

    题目链接. 题目大意: 给定三个点,即一个任意三角形,求外接圆的周长. 分析: 外接圆的半径可以通过公式求得(2*r = a/sinA = b/sinB = c/sinC),然后直接求周长. 注意: ...

  9. ZJOI2018游记Round1

    广告 ZJOI2018Round2游记 All Falls Down 非常感谢学弟学妹们捧场游记虽然这是一篇假游记 ZJOI Round1今天正式落下帷幕.在这过去的三天里遇到了很多朋友,见识了很多有 ...

随机推荐

  1. AngularJS.directive系列:嵌套directive的通讯及scope研究

    一.directive中的scope directive无疑是AngularJS中比较复杂难懂的部分,而directive中个scope更是其中最复杂的部分了,尤其是在嵌套directive中互相通讯 ...

  2. git远端删除被提交后又被加到.gitignore的文件

    远端删除文件而不影响本地文件 git rm [-r] --cached file_or_dir_name 利用.gitignore来自动删除所有匹配文件 我试过网上推荐的写法 git rm --cac ...

  3. gcc 高版本兼容低版本 技巧 :指定 -specs={自定义specs文件} 参数。可以搞定oracle安装问题

    如: #!/bin/sh /usr/bin/gcc-7 -specs=/usr/lib/gcc/x86_64-linux-gnu/jin.spec $* 该技巧很实用.这么久才发现,唉,不是专业搞某个 ...

  4. ava中有三种移位运算符

    转自:http://www.cnblogs.com/hongten/p/hongten_java_yiweiyunsuangfu.html <<      :     左移运算符,num ...

  5. Apache性能优化总结

    1.介绍 首先要了解Apache采用的MPM(Multi -Processing Modules,多道处理模块),MPM是Apache的核心,它的作用是管理网络连接.调度请求.Apache2.0中MP ...

  6. LeetCode_Maximum Subarray | Maximum Product Subarray

    Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...

  7. SpringMVC -- 梗概--源码--贰--静态资源的访问问题

    配置:<mvc:default-servlet-handler/> 1>静态资源:除了Servlet.Controller之外的资源,如:js,css,png,html等 2> ...

  8. eclipse中设置文件的编码格式为utf-8

    1.可以在 eclipse 中配置 workspace 项下 text file encoding 属性的值来决定此工作区间下所有的 eclipse 项目的文档的编码属性. Window-->P ...

  9. 【代码审计】CLTPHP_v5.5.3 前台任意文件上传漏洞分析

      0x00 环境准备 CLTPHP官网:http://www.cltphp.com 网站源码版本:CLTPHP内容管理系统5.5.3版本 程序源码下载:https://gitee.com/chich ...

  10. (转)作为一个新人,怎样学习嵌入式Linux?(韦东山)

    被问过太多次,特写这篇文章来回答一下.   在学习嵌入式Linux之前,肯定要有C语言基础.汇编基础有没有无所谓(就那么几条汇编指令,用到了一看就会).C语言要学到什么程度呢?越熟当然越好,不熟的话也 ...