题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1392

题意:给出一些点的坐标,求最小的凸多边形把所有点包围时此多边形的周长。

解法:凸包ConvexHull 模板题

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<cstdlib>
  6. #include<algorithm>
  7. #define inf 0x7fffffff
  8. #define PI 3.141592654
  9. #define exp 1e-10
  10. using namespace std;
  11. struct Point
  12. {
  13. double x,y;
  14. Point (double x=,double y=):x(x),y(y){}
  15. friend bool operator < (Point A,Point B)
  16. {
  17. if (A.x != B.x) return A.x < B.x;
  18. return A.y<B.y;
  19. }
  20. }an[];
  21. typedef Point Vector;
  22. Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x , A.y+B.y); }
  23. Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x , A.y-B.y); }
  24.  
  25. int dcmp(double x)
  26. {
  27. if (fabs(x)<exp) return ;
  28. else return x< ? - : ;
  29. }
  30. double cross(Vector A,Vector B)
  31. {
  32. return A.x*B.y-B.x*A.y;
  33. }
  34. int ConvexHull(Point *p,int n,Point *ch)
  35. {
  36. sort(p,p+n);
  37. int m=;
  38. for (int i= ;i<n ;i++)
  39. {
  40. while (m> && dcmp(cross(ch[m-]-ch[m-] , p[i]-ch[m-]))<=) m--;
  41. ch[m++]=p[i];
  42. }
  43. int k=m;
  44. for (int i=n- ;i>= ;i--)
  45. {
  46. while (m>k && dcmp(cross(ch[m-]-ch[m-] , p[i]-ch[m-]))<=) m--;
  47. ch[m++]=p[i];
  48. }
  49. ch[m++]=ch[];
  50. if (n>) m--;
  51. return m;
  52. }
  53. double dis(Point a,Point b)
  54. {
  55. return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
  56. }
  57. int main()
  58. {
  59. int n;
  60. while (cin>>n,n)
  61. {
  62. for (int i= ;i<n ;i++)
  63. scanf("%lf%lf",&an[i].x,&an[i].y);
  64. if (n==) {printf("0.00\n");continue;}
  65. else if (n==) {printf("%.2f\n",dis(an[],an[]));continue;}
  66. Point bn[];
  67. int m=ConvexHull(an,n,bn);
  68. double sum=;
  69. for (int i= ;i<m ;i++)
  70. {
  71. sum += dis(bn[i],bn[i+]);
  72. }
  73. printf("%.2f\n",sum);
  74. }
  75. return ;
  76. }

hdu 1392 Surround the Trees的更多相关文章

  1. HDU 1392 Surround the Trees(凸包入门)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. HDU - 1392 Surround the Trees (凸包)

    Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...

  3. HDU 1392 Surround the Trees (凸包周长)

    题目链接:HDU 1392 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope ...

  4. hdu 1392 Surround the Trees 凸包裸题

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. hdu 1392 Surround the Trees 凸包模板

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. hdu 1392:Surround the Trees(计算几何,求凸包周长)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. hdu 1392 Surround the Trees (凸包)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU 1392 Surround the Trees(凸包*计算几何)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...

  9. 计算几何(凸包模板):HDU 1392 Surround the Trees

    There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So a ...

随机推荐

  1. RecyclerView中显示不同的item

    测试代码: activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an ...

  2. 如何创建ajax对象?

    1.IE低版本 2.非IE和高版本 <script> function createAjax(){ var request=false; //window对象中有XMLHttpReques ...

  3. 代码重启SQL命令

    1.net stop mssqlserver --停止mssqlserver 2.net start mssqlserver  --开始mssqlserver

  4. jquery.unobtrusive-ajax.js的扩展,做到片段式加载

    //ajax支持库 /*! ** Unobtrusive Ajax support library for jQuery ** Copyright (C) Microsoft Corporation. ...

  5. salt-ssh安装及简单使用

    需要 salt-master 0.17以上版本支持 1.安装 相关依赖包可查看requirements.txt Jinja2 M2Crypto msgpack-python pycrypto PyYA ...

  6. ListView用法及加载数据时的闪烁问题和加载数据过慢问题

    ListView介绍及添加数据时的闪烁问题 1.     ListView类 1.1 ListView常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示 ...

  7. ES6 入门系列 - let 和 const 命令

    let命令 基本用法 ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. { let a = ; ; } a // ReferenceEr ...

  8. zip解压缩

    package com.green.project.compress; import java.io.File;import java.io.FileInputStream;import java.i ...

  9. Oracle 11g Windows 迁移至 Linux

    OS: windows server 2008 R2 enterprise DB: 11.2.0.1.0 数据库配置: ORACLE_BASE=D:\app\Administrator ORACLE_ ...

  10. EXCEL处理数据小技巧

    1.EXCEL 中如何获得4*8这个数字中,*号前面的数字 输入  =LEFT(AC2,FIND("*",AC2)-1) 获得*右边的数字 输入 =RIGHT(AC2,FIND(& ...