codeforces #8D Two Friends (二分答案+计算几何)

╰半夏微凉° 2022-06-17 08:51 259阅读 0赞

题目链接;

点击打开题目链接

题意:

有两个人Alan和Bob,他们现在都在A点,现在Bob想去B点,Alan想先到C点再去B点。

要求Alan走过的长度不能超过最短路长度+t1,Bob走过的长度不能超过最短路长度+t2,求两人在一起最多走多久(分开后再汇合不算一起走)?

题解:

设Alan最多走L1,Bob最多走L2,注意还要加上t1和t2这两个差值。

首先如果Bob能陪伴Alan全程(即L2≥Distance(A,C)+Distance(C,B)),那么答案显然为min(L1,L2) 。此时他们一定是在Alan到达C之前分开的

否则两人分离时Bob一定还没有经过C点 ,这时显然不比一起回家更优。

容易发现答案是单调的,我们不妨二分答案x,即Alan 和Bob走距离为x的相同路线后分开。

不妨设分离点为P,当前二分到mid,那么:

Distance(P,A)≤mid

Distance(P,B)≤L2−mid

Distance(P,C)≤L1−Distance(B,C)−mid

即:

设分离点为P,那么点P必须满足一下三个条件:

P必须在以A为圆心半径为x的圆内,因为他们走的公共距离为x

P必须在以B为圆心半径为L2−x的圆内,为了让Bob在分开之后能及时返回B点

P必选在以C为圆心半径为L1−x−BC的圆内,因为Alan在到达C之后还要径直走回B点。

所以如果三个圆相交,那么一定存在这样的点P。

所以容易发现每个不等式中P的范围都是一个圆 。

因此我们只需要判断三个圆是否有公共部分即可 。

判断三个圆是否相交:

三个圆两两相交是必要条件但不是充分条件。

因为可能会有这种情况:

这里写图片描述

在两两相交的前提下,如果有一个小圆内含在一个大圆内的话,那么这三个圆也是相交的。

否则,如果三个圆有公共部分,两两圆必然有1∼2个交点。

如图:

这里写图片描述

考虑这三个圆的相交区域,它必然是由若干个圆弧组成的。

所以这块区域的关键点也一定是某两个圆的交点,枚举两两圆的共三组交点,如果有一个交点满足都在三个圆的圆内或圆上那么这三个圆就是相交的

我的是二分了200次。
AC代码:

  1. //#pragma comment(linker, "/STACK:102400000,102400000")
  2. //#include <bits/stdc++.h>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <vector>
  9. #include <map>
  10. #include <cmath>
  11. #include <queue>
  12. #include <set>
  13. #include <bitset>
  14. #include <iomanip>
  15. #include <list>
  16. #include <complex>
  17. #include <stack>
  18. #include <utility>
  19. using namespace std;
  20. typedef long long ll;
  21. typedef unsigned long long ull;
  22. typedef pair<int,int> pii;
  23. typedef vector<int> vi;
  24. const double eps = 1e-8;
  25. const int INF = 1e9+7;
  26. const ll inf =(1LL<<62) ;
  27. const int MOD = 1e9 + 7;
  28. const ll mod = (1LL<<32);
  29. const int N =1e6+6;
  30. const int M=100010;
  31. const int maxn=1001;
  32. #define mst(a) memset(a, 0, sizeof(a))
  33. #define M_P(x,y) make_pair(x,y)
  34. #define pi acos(-1.0)
  35. #define in freopen("in.txt","r",stdin)
  36. #define rep(i,j,k) for (int i = j; i <= k; i++)
  37. #define per(i,j,k) for (int i = j; i >= k; i--)
  38. #define lson x << 1, l, mid
  39. #define rson x << 1 | 1, mid + 1, r
  40. const int lowbit(int x) { return x&-x; }
  41. int read(){ int v = 0, f = 1;char c =getchar();
  42. while( c < 48 || 57 < c ){
  43. if(c=='-') f = -1;c = getchar();}
  44. while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();
  45. return v*f;}
  46. #define point complex<double>
  47. double t1, t2;
  48. point cinema, shop, house;
  49. void readpoint(point &p)
  50. {
  51. double x, y;
  52. scanf("%lf %lf", &x, &y);
  53. p = point(x, y);
  54. }
  55. bool inter(point a, double r_a, point b, double r_b, point c, double r_c) //以c为主圆求a b焦点判相交
  56. {
  57. if (abs(c - a) <= r_a && abs(c - b) <= r_b) return true;
  58. b -= a;
  59. c -= a; //以a为原点
  60. point r = point(b.real() / abs(b), b.imag() / abs(b)); //将x轴正方向置为b
  61. b /= r;
  62. c /= r;
  63. double d = (r_a * r_a - r_b * r_b + abs(b) * abs(b)) / (2 * abs(b));
  64. double h = sqrt(max(r_a * r_a - d * d, 0.0));
  65. if (abs(h * h + (d - abs(b)) * (d - abs(b))) - r_b * r_b > eps) return false;
  66. if (abs(point(d, h) - c) <= r_c || abs(point(d, -h) - c) <= r_c) return true;
  67. return false;
  68. }
  69. bool check(point a, double r_a, point b, double r_b, point c, double r_c) //判断三圆是否相交
  70. {
  71. if (r_a <= eps || r_b <= eps || r_c <= eps) return false; //有空集
  72. r_a = max(r_a, 0.0);
  73. r_b = max(r_b, 0.0);
  74. r_c = max(r_c, 0.0);
  75. if (inter(a, r_a, c, r_c, b, r_b)) return true;
  76. if (inter(b, r_b, a, r_a, c, r_c)) return true;
  77. if (inter(c, r_c, b, r_b, a, r_a)) return true;
  78. return false;
  79. }
  80. int main()
  81. {
  82. scanf("%lf %lf", &t1, &t2);
  83. readpoint(cinema); //cinema
  84. readpoint(house); //house
  85. readpoint(shop); //shop
  86. if (abs(shop - cinema) + abs(house - shop) <= abs(cinema - house) + t2)//Alan <= Bob + t2
  87. {
  88. printf("%lf\n", min( abs(cinema - house) + t2, abs(shop - cinema) + abs(house - shop) + t1));
  89. }
  90. else
  91. {
  92. double l, r, mid;
  93. l = 0;
  94. r = min( abs(cinema - house) + t2, abs(shop - cinema) + abs(house - shop) + t1);
  95. for(int i=1;i<=200;i++)
  96. {
  97. mid = (r + l) / 2;
  98. if (check(cinema, mid, shop, abs(shop - cinema) + t1 - mid, house, abs(house - cinema) + t2 - mid)){
  99. l = mid;
  100. }
  101. else {
  102. r = mid;
  103. }
  104. }
  105. printf("%.4lf\n", l);
  106. }
  107. return 0;
  108. }

发表评论

表情:
评论列表 (有 0 条评论,259人围观)

还没有评论,来说两句吧...

相关阅读

    相关 codeforces 159 D(几何二分)

    [传送门][Link 1] 题意:给你n个点,问与x轴相切,并且包含这n个点的圆的最小半径是多少。 思路:真是做的的怀疑人生。思路是首先判断点是否在一边。 如果在一边一定