题目链接
题意: 给出一些二维的点,求将其全部包围起来所需的线段长度。
先求出凸包,再遍历每条线段求长度。
(只有两个点时,答案就是两点间的距离)
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
using namespace std;
const int N = 5e2 + 5;
struct Pt{
int x, y;
Pt() {}
Pt(int x, int y) : x(x), y(y) {}
friend Pt operator - (const Pt &a, const Pt &b) {
return Pt(a.x - b.x, a.y - b.y);
}
};
Pt t[105], res[105];
inline int det(const Pt &a, const Pt &b) {
return a.x * b.y - a.y * b.x;
}
inline bool cmp(const Pt &a, const Pt &b) {
int flag = det(b- t[0], a - t[0]);
return flag < 0 || (flag == 0 && a.x < b.x);
}
int convex_hull(Pt *a, Pt *res, int n) {
int k = 0;
for(int i = 1; i < n; i ++)
if(a[k].y > a[i].y || (a[k].y == a[i].y && a[k].x > a[i].x))
k = i;
if(k != 0)
swap(a[0], a[k]);
sort(a + 1, a + n, cmp);
res[0] = a[0], res[1] = a[1];
int m = 1;
for(int i = 2; i < n; i ++) {
while(m && det(res[m] - res[m - 1], a[i] - res[m -1]) <= 0)
m --;
res[++ m] = a[i];
}
return ++ m;
}
inline double norm (const Pt &p) {
return sqrt(p.x * p.x + p.y * p.y);
}
int main() {
ios::sync_with_stdio(false);
int n;
while(cin >> n, n) {
for(int i = 0; i < n; i ++)
cin >> t[i].x >> t[i].y;
int len = convex_hull(t, res, n);
double ans = 0;
if(len == 2)
ans = norm(res[0] - res[1]);
else
for(int i = 0; i < len; i ++)
ans += norm(res[i] - res[(i + 1) % len]);
cout << fixed << setprecision(2) << ans << endl;
}
return 0;
}