zoukankan      html  css  js  c++  java
  • HDU 1392 Surround the Trees

    题解:计算凸包……

    #include <cstdio>  
    #include <cstring>  
    #include <algorithm>  
    #include <cmath>
    using namespace std;  
    struct node{int x,y;}vex[1005],stackf[1005]; 
    bool cmp1(node a,node b){  
        if(a.y==b.y)return a.x<b.x;  
        else return a.y<b.y;  
    }  
    int cross(node a,node b,node c){return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);} 
    double dis(node a,node b){return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));} 
    bool cmp2(node a,node b){  
        int m=cross(vex[0],a,b);  
        if(m==0)return dis(vex[0],a)-dis(vex[0],b)<=0?1:0;  
        else return m>0?1:0;  
    }  
    int main(){  
        int t;  
        while(scanf("%d",&t),t!=0){  
            for(int i=0;i<t;i++)scanf("%d%d",&vex[i].x,&vex[i].y);  
            if(t==1)printf("%.2f
    ",0.00);  
            else if(t==2)printf("%.2f
    ",dis(vex[0],vex[1]));  
            else{  
                sort(vex,vex+t,cmp1);  
                sort(vex+1,vex+t,cmp2);  
                memset(stackf,0,sizeof(stackf));  
                stackf[0]=vex[0]; stackf[1]=vex[1];  
                int top=1;  
                for(int i=2;i<t;i++){  
                    while(i>=1&&cross(stackf[top-1],stackf[top],vex[i])<0)top--;      
                    stackf[++top]=vex[i];   
            }  
            double s=0;  
            for(int i=1;i<=top;i++)s+=dis(stackf[i-1],stackf[i]);  
            s+=dis(stackf[top],vex[0]);  
            printf("%.2f
    ",s);  
            }  
        }  
        return 0;
    } 
    
  • 相关阅读:
    Node Js
    动态遍历接收的参数
    负载均衡和springcloud.Ribbon
    Spring Cloud
    springboot整合hibernate
    python模块知识总结
    什么是netty--通俗易懂
    IDEA 的操作与使用
    spring 整合guava
    log4j向指定文件输出日志
  • 原文地址:https://www.cnblogs.com/forever97/p/3650021.html
Copyright © 2011-2022 走看看