hdu 1392 Surround the Trees

来源:互联网 发布:手机淘宝分期付款流程 编辑:程序博客网 时间:2024/06/01 17:01
注意 n == 1 n==2 的情况#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int maxn = 200;struct node{    double x, y;    double rad;}p[maxn], tp[maxn];bool cmp1(node a, node b){    if(a.y == b.y) return a.x < b.x;    return a.y < b.y;}bool cmp2(node a, node b){    if(a.rad == b.rad) return a.x < b.x;    return a.rad < b.rad;}double rad_point(node a, node b){    return atan2((b.y-a.y), (b.x-a.x));}bool tp_point(node a, node b, node c){    double m = (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);    if(m < 0) return true;    return false;}double dist_point(node a, node b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int main(){    int n;    while(scanf("%d", &n) != EOF && n)    {        if(n==1)        {            printf("0.00\n");            continue;        }        for(int i = 0; i < n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);        if(n==2)        {            printf("%.2lf\n", dist_point(p[0], p[1]));            continue;        }        sort(p, p+n, cmp1);        for(int i = 0; i < n; i++) p[i].rad = rad_point(p[0], p[i]);        sort(p+1, p+n, cmp2);        tp[0] = p[0];        tp[1] = p[1];        tp[2] = p[2];        int top = 2;        for(int i = 3; i < n; i++)        {            while(top >= 1 && tp_point(tp[top-1], tp[top], p[i])) top--;            tp[++top] = p[i];        }        top++;        double dist = 0.0;        for(int i = 0; i < top; i++) dist += dist_point(tp[i%top], tp[(i+1)%top]);        printf("%.2lf\n", dist);    }    return 0;}

原创粉丝点击