QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#733118 | #9520. Concave Hull | cfaaa | WA | 0ms | 3760kb | C++14 | 2.0kb | 2024-11-10 17:17:08 | 2024-11-10 17:17:08 |
Judging History
answer
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
struct Point {
long long x, y;
bool operator<(const Point& other) const {
return (x == other.x) ? y < other.y : x < other.x;
}
};
// 叉积计算
long long cross(const Point& o, const Point& a, const Point& b) {
return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
}
// 计算多边形面积的两倍
long long area2(const vector<Point>& poly) {
long long res = 0;
int n = poly.size();
for (int i = 0; i < n; ++i) {
int j = (i + 1) % n;
res += poly[i].x * poly[j].y - poly[i].y * poly[j].x;
}
return abs(res);
}
// 凸包算法(Andrew算法)
vector<Point> convexHull(vector<Point>& points) {
sort(points.begin(), points.end());
vector<Point> hull;
// 下半部分
for (const Point& p : points) {
while (hull.size() >= 2 && cross(hull[hull.size() - 2], hull[hull.size() - 1], p) <= 0)
hull.pop_back();
hull.push_back(p);
}
// 上半部分
int lower = hull.size() + 1;
for (int i = points.size() - 2; i >= 0; --i) {
while (hull.size() >= lower && cross(hull[hull.size() - 2], hull[hull.size() - 1], points[i]) <= 0)
hull.pop_back();
hull.push_back(points[i]);
}
hull.pop_back(); // Remove the duplicate last point
return hull;
}
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<Point> points(n);
for (int i = 0; i < n; ++i) {
cin >> points[i].x >> points[i].y;
}
vector<Point> hull = convexHull(points);
// 如果凸包的点数小于3,无法构成有效的凹多边形
if (hull.size() < 3) {
cout << 1 << endl;
continue;
}
// 计算凸包面积的两倍
long long area = area2(hull);
cout << area << endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3760kb
input:
2 6 -2 0 1 -2 5 2 0 4 1 2 3 1 4 0 0 1 0 0 1 1 1
output:
44 2
result:
wrong answer 1st lines differ - expected: '40', found: '44'