思路:
这题考查凸包、相似图形。九年级下册,我们学到了图形的面积比等于相似比的平方比。由于题目给定的操作并不存在删点或加点,所以两次观察到的点所构成的凸包一定相似。设第一次观察到的点所形成的凸包的面积为 ,第二次观察到的点形成的凸包的面积为 ,于是 。此题结束。注意:不开 long long 会见祖宗,除了两个样例其他全部 WA。
AC 代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n, idx, tot;
double a1, a2;
struct node {
int x, y;
} a[N], b[N], st[N], pr[N];
bool cmp(node l, node r) {
if (l.x != r.x) {
return l.x < r.x;
}
return l.y < r.y;
}
double cross(node a, node b) {
return a.x * b.y - a.y * b.x;
}
double side(node p, node p1, node a) {
node x = {p1.x - p.x, p1.y - p.y};
node y = {a.x - p.x, a.y - p.y};
return cross(x, y);
}
signed main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].x >> a[i].y;
}
sort(a + 1, a + 1 + n, cmp);
st[1] = a[1], st[2] = a[2];
idx = 2;
for (int i = 3; i <= n; i++) {
while (idx > 1 && side(st[idx - 1], st[idx], a[i]) <= 0) {
idx--;
}
st[++idx] = a[i];
}
for (int i = 1; i < idx; i++) {
pr[++tot] = st[i];
}
idx = 0;
st[++idx] = a[n], st[++idx] = a[n - 1];
for (int i = n - 2; i >= 1; i--) {
while (idx > 1 && side(st[idx - 1], st[idx], a[i]) <= 0) {
idx--;
}
st[++idx] = a[i];
}
for (int i = 1; i <= idx; i++) {
pr[++tot] = st[i];
}
pr[++tot] = a[1];
for (int i = 1; i < tot; i++) {
a1 += cross(pr[i], pr[i + 1]);
}
a1 /= 2;
for (int i = 1; i <= n; i++) {
cin >> b[i].x >> b[i].y;
}
sort(b + 1, b + 1 + n, cmp);
tot = 0, idx = 2;
st[1] = b[1], st[2] = b[2];
for (int i = 3; i <= n; i++) {
while (idx > 1 && side(st[idx - 1], st[idx], b[i]) <= 0) {
idx--;
}
st[++idx] = b[i];
}
for (int i = 1; i < idx; i++) {
pr[++tot] = st[i];
}
idx = 0;
st[++idx] = b[n], st[++idx] = b[n - 1];
for (int i = n - 2; i >= 1; i--) {
while (idx > 1 && side(st[idx - 1], st[idx], b[i]) <= 0) {
idx--;
}
st[++idx] = b[i];
}
for (int i = 1; i <= idx; i++) {
pr[++tot] = st[i];
}
pr[++tot] = b[1];
for (int i = 1; i < tot; i++) {
a2 += cross(pr[i], pr[i + 1]);
}
a2 /= 2;
cout << fixed << setprecision(8) << sqrt(abs(a2) / abs(a1));
return 0;
}