题解:P10859 [HBCPC2024] Nana Likes Polygons
一道结论比较显然的题。
首先,我们有一个结论:选三角形必定最优。如何证明呢?首先,三角形本身就是凸多边形,其次,若有四条边以上的凸多边形,我们可以选择其中的三个点连成一个三角形,这时面积更小。因此,我们的结论是正确的。
那么,我们可以枚举三个点的坐标,用叉积计算一下三角形面积即可,时间复杂度
参考代码如下:
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define debug(x) cerr << (#x) << '\n'
#define pii pair <int, int>
#define pb emplace_back
#define chkmin(x, y) x > y ? x = y : 0
#define chkmax(x, y) x < y ? x = y : 0
#define L(i, a, b) for (int i = (a); i <= (b); i++)
#define R(i, a, b) for (int i = (a); i >= (b); i--)
#define x first
#define y second
using namespace std;
const int P = 998244353;
vector <pii> p;
signed main() {
int t;
cin >> t;
while (t--) {
p.clear();
int n;
cin >> n;
L(i, 1, n) {
int f, s;
cin >> f >> s;
p.pb(f, s);
}
// sort(p.begin(), p.end());
double s = 1e18;
L(i, 0, n - 1) {
L(j, i + 1, n - 1) {
L(k, j + 1, n - 1) {
double ans = p[j].x * p[k].y - p[j].y * p[k].x;
ans -= (p[i].x * p[k].y - p[i].y * p[k].x);
ans += (p[i].x * p[j].y - p[i].y * p[j].x);
ans = fabs(ans);
if (ans != 0) chkmin(s, ans / 2.0);
}
}
}
if (n < 3 || s == 1e18) cout << -1 << '\n';
else cout << s << '\n';
}
}