HDU 1392 Surround the Trees(几何 凸包模板)
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(几何 凸包模板)的更多相关文章
- HDU 1392 Surround the Trees(凸包入门)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 题解报告: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 ...
- HDU 1392 Surround the Trees(凸包)题解
题意:给一堆二维的点,问你最少用多少距离能把这些点都围起来 思路: 凸包: 我们先找到所有点中最左下角的点p1,这个点绝对在凸包上.接下来对剩余点按照相对p1的角度升序排序,角度一样按距离升序排序.因 ...
- HDU 1392 Surround the Trees 构造凸包
又是一道模板题 #include <iostream> #include <cstring> #include <cstdlib> #include <cst ...
- HDU 1392 Surround the Trees(凸包)
题面 懒得粘贴了... 大致题意:坐标系内有若干个点,问把这些点都圈起来的最小凸包周长. 题解 直接求出凸包,统计一遍答案即可 #include<iostream> #include< ...
- 计算几何(凸包模板):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 ...
- hdu 1392 Surround the Trees 凸包模板
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 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 ...
- HDU - 1392 Surround the Trees (凸包)
Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...
- hdu 1392:Surround the Trees(计算几何,求凸包周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 3-1 vue-resource基础介绍
1.静态引用 <script src="https://cdn.bootcss.com/vue-resource/1.3.4/vue-resource.js">< ...
- pycharm 里面引用pymysql
import pymysql db =pymysql.connect(host ='192.168.1.131',port=3306,user='jack',password ='jack',db = ...
- matlab中 数据保留有效位数
可以使用round函数 ,这函数原本功能是四舍五入 比如: >> A = 0.0326465;>> B = round(A*1000)/1000 B = 0.0330
- python nose测试框架全面介绍四
四.内部插件介绍 1.Attrib 标记,用于筛选用例 在很多时候,用例可以分不同的等级来运行,在nose中很增加了这个功能,使用attrib将用例进行划分 有两种方式: ef test_big_do ...
- unity3d的优化场景技术LOD+IOC
一.unity3d的优化场景技术 LOD+IOC 遮挡剔除(occlusion culling)其实就是在摄像机范围内的物体才被渲染出来,没有在视野范围内的,统统关掉渲染,这样能让性能大大提高. I ...
- Unity3D笔记二十 多媒体与网络
1 游戏音频 1.游戏音乐:如游戏背景音乐 2.游戏音效:如开枪或打怪物时“砰砰”的游戏音效 Unity 3D游戏引擎共支持4种音乐格式的文件,具体如下. aiff:适用于较短的音乐文件,可用作游戏音 ...
- 【笔记】javascript权威指南-第二章-词法结构
词法结构 //本书是指:javascript权威指南 //以下内容摘记时间为:2013.7.28 字符集 UTF-8和UTF-16的区别?Unicode和UTF是什么关系?Unicode转义 ...
- Bitbucket - 用git 用法
核心流程: 从远端中心repo那里Git clone 到本地,再在本地开发(add, commit), 通常会利用branch管理,如果觉得code 没问题了,就push到远端的中心repo上.这里中 ...
- redis -clock_gettime问题
/home/wm/redis-/deps/jemalloc/src/nstime.c:: undefined reference to `clock_gettime' 这个错误 解决思路如下 .查找实 ...
- DLRS(近三年深度学习应用于推荐系统论文汇总)
Recommender Systems with Deep Learning Improving Scalability of Personalized Recommendation Systems ...