BNUOJ 51638 Air Hockey(三分搜索、二分搜索)

题意:

$平面上给定2个球的初始位置,运动向量,以及半径$
$求2个球是否相撞,若撞,输出碰撞的时间,否则输出最近距离$

分析:

$先三分求两圆圆心的最近距离,如果两圆圆心最近距离不大于两圆半径之和,则可以碰撞$
$再二分求出碰撞时间即可,否则直接输出两圆圆心最近距离减去两圆半径之和$

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// Created by TaoSama on 2016-04-23
// Copyright (c) 2016 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const double EPS = 1e-8;
int sgn(double x) {
return x < -EPS ? -1 : x > EPS;
}
struct Point {
double x, y;
void read() {
scanf("%lf%lf", &x, &y);
}
Point operator+(const Point& p) {
return {x + p.x, y + p.y};
}
Point operator-(const Point& p) {
return {x - p.x, y - p.y};
}
Point operator*(const double k) {
return {k * x, k * y};
}
double length() {
return hypot(x, y);
}
} x, vx, y, vy;
int rx, ry;
double get(double m) {
Point p = x + vx * m, q = y + vy * m;
return (p - q).length();
}
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
while(t--) {
x.read(); scanf("%d", &rx); vx.read();
y.read(); scanf("%d", &ry); vy.read();
double l = 0, r = 1e9;
for(int i = 1; i <= 100; ++i) {
double ll = (2 * l + r) / 3;
double rr = (l + 2 * r) / 3;
if(get(ll) < get(rr)) r = rr;
else l = ll;
}
double dis = get(l) - rx - ry, t = l;
if(sgn(dis) <= 0) { //hit
double l = 0, r = t;
for(int i = 1; i <= 100; ++i) {
double m = (l + r) / 2;
if(get(m) < rx + ry) r = m;
else l = m;
}
printf("%.12f\n", l);
} else printf("%.12f\n", dis);
}
return 0;
}


1. 除非注明,本博文即为原创,转载请注明链接地址
2. 本博文只代表博主当时的观点或结论,请不要恶意攻击
3. 如果本文帮到了您,不妨点一下 下面分享到 按钮,让更多的人看到