hdu1392(一道凸包水题)

Surround the Trees

题意: 把树围起来。
思路:简单凸包。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
const int  mod = 1e9+7;
#define ll long long
#define llu unsigned long long 
using namespace std;
map<string,int>mp;
const int maxn = 100550+10;
const double eps = 0.0000001;
int v[maxn],a[maxn];

struct Point {
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};
int n; 
Point tree[150],s[150];

double cross(Point a,Point b,Point c) {
    return ((b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x));
}
double dis(Point a,Point b) {
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

}

bool cmp(Point a,Point b) {
    if(a.x != b.x) return a.x<b.x;
    else return a.y<b.y;
}

bool cmp1(Point a,Point b) {
    double m = cross(tree[0],a,b);
    if(fabs(m-0) < eps) {
        if(dis(tree[0],b) > dis(tree[0],a)) return true;
        else return false;
    }
    else {
        if(m>0) return true;
        else return false;
    }
}


int main(){
    while(scanf("%d",&n)!=EOF){
        if(n==0) break;
        for(int i=0;i<n;i++) {
            scanf("%lf%lf",&tree[i].x,&tree[i].y);      
        }
        if(n==1) {
            printf("0.00\n"); continue;
        }
        if(n==2) {
            printf("%.2lf\n",dis(tree[0],tree[1])); continue;
        }
        sort(tree,tree+n,cmp);
        sort(tree+1,tree+n,cmp1);
        s[0].x = tree[0].x;
        s[0].y = tree[0].y;
        s[1].x = tree[1].x;
        s[1].y = tree[1].y;
        int top = 1;
        for(int i=2;i<n;i++) {
            while(top>=1 && cross(s[top-1],s[top],tree[i]) < 0) top--;
            s[++top] = tree[i];
        }
    /*  cout<<top<<endl;
        for(int i=0;i<=top;i++) {
            cout<<s[i].x<<"  "<<s[i].y<<endl;
        }cout<<endl;*/
        double sum = 0;
        for(int i=0;i<top;i++) {
    //      cout<<dis(s[i],s[i+1])<<endl;
            sum += dis(s[i],s[i+1]);

        }
        sum += dis(s[0],s[top]);
        printf("%.2lf\n",sum);
    }   
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值