0%

2019 ACM-ICPC 南京 现场赛 K. Triangle (二分)

题意

给定一个三角形和一个点 $p$,如果该点不在三角形边上直接输出 $-1$,否则在三角形上找一点 $q$,使得线段 $pq$ 平分三角形面积。

思路

看完题想都没想直接二分了。

就是比赛时复制粘贴的时候改错了,贡献了三发罚时。

板子来自 kuangbin 的模板。

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <bits/stdc++.h>
using namespace std;
typedef double db;
const db eps = 1e-8;
const db inf = 1e20;
const db pi = acos(-1.0);

int sgn(db x){
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}

struct Point{
db x, y;
Point(){}
Point(db _x, db _y){
x = _x;
y = _y;
}
void input(){
scanf("%lf%lf", &x, &y);
}
bool operator == (Point b)const{
return sgn(x-b.x) == 0 && sgn(y-b.y) == 0;
}
Point operator -(const Point &b)const{
return Point(x-b.x, y-b.y);
}
//叉积
db operator ^(const Point &b)const{
return x*b.y - y*b.x;
}
//点积
db operator *(const Point &b)const{
return x*b.x + y*b.y;
}
//返回两点的距离
db dis(Point p){
return hypot(x-p.x, y-p.y);
}
Point operator +(const Point &b)const{
return Point(x+b.x, y+b.y);
}
Point operator *(const db &k)const{
return Point(x*k, y*k);
}
Point operator /(const db &k)const{
return Point(x/k, y/k);
}
};

struct Line{
Point s,e;
Line(){}
Line(Point _s,Point _e){
s = _s;
e = _e;
}
// 点在线段上的判断
bool pointonseg(Point p){
return sgn((p-s)^(e-s)) == 0 && sgn((p-s) * (p-e)) <= 0;
}

// 求两直线的交点
Point crosspoint(Line v){
double a1 = (v.e-v.s)^(s-v.s);
double a2 = (v.e-v.s)^(e-v.s);
return Point((s.x*a2-e.x*a1)/(a2-a1),(s.y*a2-e.y*a1)/(a2-a1));
}
};

// 求点a和点b的中点
Point get_mid(Point a, Point b) {
return (a + b) * 0.5;
}

int main() {
int T;
scanf("%d", &T);
while(T--) {
Point a, b, c, p;
Line ab, bc, ac;
a.input(), b.input(), c.input(), p.input();
ab = Line(a, b); bc = Line(b, c); ac = Line(a, c);
db area = fabs((b - a) ^ (c - a) * 0.5); // 三角形面积
if(ab.pointonseg(p)) { // 点p在线段ab上
if(a.dis(p) < b.dis(p)) { // 点p靠近点a,则另一点一定在线段bc上
Point l = c, r = b;
Point mid = get_mid(l, r);
int times = 1000;
while(times--) {
mid = get_mid(l, r);
db s = fabs((mid - p) ^ (b - p));
if(sgn(s - area) > 0) {
l = mid;
} else if(sgn(s - area) == 0) {
break;
} else {
r = mid;
}
}
printf("%.10lf %.10lf\n", mid.x, mid.y);
} else { // 点p靠近点b,则另一点一定在线段ac上
Point l = c, r = a;
Point mid = get_mid(l, r);
int times = 1000;
while(times--) {
mid = get_mid(l, r);
db s = fabs((mid - p) ^ (a - p));
if(sgn(s - area) > 0) {
l = mid;
} else if(sgn(s - area) == 0) {
break;
} else {
r = mid;
}
}
printf("%.10lf %.10lf\n", mid.x, mid.y);
}
} else if(bc.pointonseg(p)) { // 另外两种情况复制粘贴就行了
if(b.dis(p) < c.dis(p)) {
Point l = a, r = c;
Point mid = get_mid(l, r);
int times = 1000;
while(times--) {
mid = get_mid(l, r);
db s = fabs((mid - p) ^ (c - p));
if(sgn(s - area) > 0) {
l = mid;
} else if(sgn(s - area) == 0) {
break;
} else {
r = mid;
}
}
printf("%.10lf %.10lf\n", mid.x, mid.y);
} else {
Point l = a, r = b;
Point mid = get_mid(l, r);
int times = 1000;
while(times--) {
mid = get_mid(l, r);
db s = fabs((mid - p) ^ (b - p));
if(sgn(s - area) > 0) {
l = mid;
} else if(sgn(s - area) == 0) {
break;
} else {
r = mid;
}
}
printf("%.10lf %.10lf\n", mid.x, mid.y);
}
} else if(ac.pointonseg(p)) {
if(a.dis(p) < c.dis(p)) {
Point l = b, r = c;
Point mid = get_mid(l, r);
int times = 1000;
while(times--) {
mid = get_mid(l, r);
db s = fabs((mid - p) ^ (c - p));
if(sgn(s - area) > 0) {
l = mid;
} else if(sgn(s - area) == 0) {
break;
} else {
r = mid;
}
}
printf("%.10lf %.10lf\n", mid.x, mid.y);
} else {
Point l = b, r = a;
Point mid = get_mid(l, r);
int times = 1000;
while(times--) {
mid = get_mid(l, r);
db s = fabs((mid - p) ^ (a - p));
if(sgn(s - area) > 0) {
l = mid;
} else if(sgn(s - area) == 0) {
break;
} else {
r = mid;
}
}
printf("%.10lf %.10lf\n", mid.x, mid.y);
}
} else {
printf("-1\n");
}
}

return 0;
}

赛后发现直接推也可以,也不用写这么长的代码了 (虽然大部分是复制粘贴)。感觉在演队友

欢迎关注我的其它发布渠道