http://acm.hdu.edu.cn/showproblem.php?pid=1392

题目大意:

  二维平面给定n个点,用一条最短的绳子将所有的点都围在里面,求绳子的长度。

解题思路:

  凸包的模板。凸包有很多的算法。这里用Adrew。

  注意这几组测试数据

  1

  1 1

  3

  0 0

  1 0

  2 0

  输出数据

  0.00

  2.00

 #include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std; #define MAXN 100+10 struct Point{
double x, y; Point(double x = , double y = ): x(x), y(y){} void scan(){
scanf("%lf %lf", &x, &y);
}
}; typedef Point Vector; Vector operator - (Vector A, Vector B){
return Vector(A.x - B.x, A.y - B.y);
} double dot(Vector A, Vector B){//点乘
return A.x * B.x + A.y * B.y;
}
double length(Vector A){//向量模
return sqrt(dot(A, A));
}
double cross(Vector A, Vector B){//叉乘
return A.x * B.y - A.y * B.x;
} int n;
Point p[MAXN], stack[MAXN]; bool input(){
scanf("%d", &n);
if(n == ) return false;
for(int i = ; i < n; ++i){
p[i].scan();
}
return true;
} bool cmp(Point A, Point B){
return A.x < B.x || (A.x == B.x && A.y < B.y);
} int convex_hull(){//Adrew算法 求凸包
sort(p, p + n, cmp); int m = ;
for(int i = ; i < n; ++i){
while(m > && cross(stack[m - ] - stack[m - ], p[i] - stack[m - ]) <= ) --m;
stack[m ++] = p[i];
}
int k = m;
for(int i = n - ; i >= ; --i){
while(m > k && cross(stack[m - ] - stack[m - ], p[i] - stack[m - ]) <= ) --m;
stack[m ++] = p[i];
}
if(n > ) --m;
return m;
} void solve(){
n = convex_hull();
if(n == ){//当只有一个点时,绳子长度为0
puts("0.00");
return ;
}
if(n == ){//当只有两个点时,绳子长度为两点之间的长度
printf("%.2lf\n", length(stack[] - stack[]));
return ;
}
double ans = ;//绳子的累加长度
stack[n] = stack[];//为了算第n点到第1点的距离
for(int i = ; i < n; ++i){
ans += length(stack[i] - stack[i + ]);
}
printf("%.2lf\n", ans);
} int main(){
while(input()){
solve();
}
return ;
}

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(凸包入门)

    Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...

  3. HDU 1392 Surround the Trees(凸包)题解

    题意:给一堆二维的点,问你最少用多少距离能把这些点都围起来 思路: 凸包: 我们先找到所有点中最左下角的点p1,这个点绝对在凸包上.接下来对剩余点按照相对p1的角度升序排序,角度一样按距离升序排序.因 ...

  4. HDU 1392 Surround the Trees 构造凸包

    又是一道模板题 #include <iostream> #include <cstring> #include <cstdlib> #include <cst ...

  5. HDU 1392 Surround the Trees(凸包)

    题面 懒得粘贴了... 大致题意:坐标系内有若干个点,问把这些点都圈起来的最小凸包周长. 题解 直接求出凸包,统计一遍答案即可 #include<iostream> #include< ...

  6. 计算几何(凸包模板):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 ...

  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 (凸包周长)

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

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

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

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

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

随机推荐

  1. kafka进阶

    1. kafka整体结构图 Kafka名词解释和工作方式 Producer :消息生产者,就是向kafka broker发消息的客户端. Consumer :消息消费者,向kafka broker取消 ...

  2. sencha touch Ext.app.Application

    Ext.app.Application一般用于app.js中 用来初始化整个应用 可以预先加载controllers(控制器),models(模型),stores(数据源),views(视图) 例如: ...

  3. 实战BRTSvc一款我见过的最嚣张的挖矿软件

    第一步:发现告警 Suricata发现特征字符串jsonrpc,这个是匹配挖矿木马的一个重要特征.于是开始分析告警信息: 告警中可以提取出的有效信息如下: 目标IP:149.28.199.108 目标 ...

  4. VC消息传递(对话框间传递参数)

    以下用一个自创的对话框类(MyMessageDlg)向视图类(MessageTestView)发送自定义消息为例,说明这两种不同方法的自定义消息的 消息传递的方法一:使用ON_MESSAGE使用ON_ ...

  5. 【CF896E】Welcome home, Chtholly 暴力+分块+链表

    [CF896E]Welcome home, Chtholly 题意:一个长度为n的序列ai,让你支持两种操作: 1.l r x:将[l,r]中ai>x的ai都减去x.2.l r x:询问[l,r ...

  6. wpgcms---流程控制

    在模板里面Twig标签语法的时候,很多时候会用到流程控制. if 判断: {% if true %} {% endif %} // 示例 {% if item.href %} href="{ ...

  7. nginx配置虚拟主机之不同端口和不同IP地址

    配置nginx虚拟主机不同端口和不同ip地址,和上编nginx基于域名配置虚拟主机博文类似,请先参考. zxl.com域名不同端口,配置文件内容如下: 1 2 3 4 5 6 7 8 9 10 11 ...

  8. BAT等大厂已开源的70个实用工具盘点(附下载地址)

    前面的一篇文章<微软.谷歌.亚马逊.Facebook等硅谷大厂91个开源软件盘点(附下载地址)>列举了国外8个互联网公司(包括微软.Google.亚马逊.IBM.Facebook.Twit ...

  9. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

  10. PyQT5初学(一)

    PyQt5 是Digia的一套Qt5与python绑定的应用框架,同时支持2.x和3.x.本教程使用的是3.x.Qt库由Riverbank Computing开发,是最强大的GUI库之一 ,官方网站: ...