有蟲!
該比賽已結束,您無法在比賽模式下遞交該題目。您可以點選“在題庫中開啟”以普通模式檢視和遞交本題。
Problem Description
Author: louishuang
有一個人剛學會了太空運算子想耍帥寫凸包面積結果寫爛了
幫幫他debug吧!
修改 個字元且 AC: 100 分。
修改 個字元且 AC: 50 分。
超過 30 字元或未通過測資:0 分。
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
struct Point {
long long x, y;
bool operator<=>(const Point&) const = default;
};
long long cross(Point o, Point a, Point b) {
return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
}
vector<Point> convex_hull(vector<Point>& pts) {
int n = pts.size(), k = 0;
if (n <= 2) return pts;
vector<Point> hull(2 * n);
sort(pts.begin(), pts.end());
for (int i = 0; i < n; ++i) {
while (k >= 2 && cross(hull[k - 2], hull[k - 1], pts[i]) <= 0) k--;
hull[k++] = pts[i];
}
for (int i = n - 2, t = k + 1; i >= 0; i--) {
while (k >= t && cross(hull[k - 2], hull[k - 1], pts[i]) <= 0) k--;
hull[k++] = pts[i];
}
hull.resize(k - 1);
return hull;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
if (!(cin >> n)) return 0;
vector<Point> pts(n);
for (int i = 0; i < n; i++) cin >> pts[i].x >> pts[i].y;
vector<Point> hull = convex_hull(pts);
long long area = 0;
for (int i = 0; i < hull.size(); i++) {
area += hull[i].x * hull[(i + 1) % hull.size()].y - hull[(i + 1) % hull.size()].x * hull[i].y;
}
cout << abs(area) / 2 << (abs(area) % 2 ? ".5" : ".0") << "\n";
return 0;
}
4
0 0
0 2
2 0
2 2
4.0