2 2 2 -0.5 0 0.5 0 3 3 0 1 1 0 -1 0
Case #1: 0.3183 Case #2: 0.5123
#include <cstdio> #include <cstdlib> #include <cmath> #define N 200 #define inf 1e-6 #define PI 3.141592653 typedef struct { double x; double y; }point; point points[N]; point chs[N]; int sp; //求凸包周长的模板 double dis(point a, point b) { return sqrt((a.x - b.x) * (a.x - b.x) * 1.0 + (a.y - b.y) * (a.y - b.y)); } double multi(point p0, point p1, point p2) { return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y); } int cmp(const void *p, const void *q) { point a = *(point *)p; point b = *(point *)q; double k = multi(points[0], a, b); if(k < -inf) return 1; else if(fabs(k) < inf && (dis(a, points[0]) - dis(b, points[0])) > inf) return 1; else return -1; } void convex_hull(int n) { int k, d; double miny = points[0].y; int index = 0; for(int i = 1; i < n; i++) { if(points[i].y < miny) { miny = points[i].y; index = i; } else if(points[i].y == miny && points[i].x < points[index].x) index = i; } point temp; temp = points[index]; points[index] = points[0]; points[0] = temp; qsort(points+1, n-1, sizeof(points[0]), cmp); chs[0] = points[n-1]; chs[1] = points[0]; sp = 1; k = 1; while(k <= n-1) { double d = multi(chs[sp], chs[sp-1], points[k]); if(d <= 0) { sp++; chs[sp] = points[k]; k++; } else sp--; } } int main() { double sum, d; int T, n; scanf("%d",&T); for(int Ca = 1; Ca <= T; Ca++) { sum = 0; scanf("%d %lf", &n, &d); if(n == 0 || n == 1) { printf("Case #%d: 0.0000\n", Ca); continue; } for(int i = 0; i < n; i++) scanf("%lf%lf", &points[i].x, &points[i].y); if(n == 2) { double len = dis(points[0],points[1]); printf("Case #%d: %.4f\n", Ca, (2 * len) / (PI * d)); continue; } convex_hull(n); for(int i = 1; i <= sp; i++) sum += dis(chs[i-1], chs[i]); sum += dis(chs[0], chs[sp]); //算出凸包周长 printf("Case #%d: %.4f\n", Ca, sum / (PI * d)); } }