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 ...
随机推荐
- cmake openssl ios
1 下载源代码 git clone https://github.com/pol51/OpenSSL-CMake.git cd OpenSSL-CMake mkdir build && ...
- FastDFS client for .net
fastDfs Code: https://code.google.com/p/fastdfs/ FastDfs_Client_DotNet:https://code.google.com/p/fas ...
- Elasticsearch 索引、更新、删除文档
一.Elasticsearch 索引(新建)一个文档的命令: curl XPUT ' http://localhost:9200/test_es_order_index/test_es_order_t ...
- Java基础之理解封装,继承,多态三大特性
目录 封装 继承 多态 封装 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 代码理解 publ ...
- php中调用这个功能可以在web页面中显示hello world这个经典单词
php程序写的时间长了,自然对他所提供的功能了如指掌,他所提供的一大堆功能,真是觉得很好用,但有时候会发现php也缺少一些功能,自己总是会产生为php添加一些自定义的功能的想法.久而久之,终于今天憋不 ...
- Linq 多连接及 left join 实例 记录
var retList = from d in mbExList.Cast<MaterialBaseEx>().ToList() join c in umcList.Cast<Cla ...
- PAT甲1115 Counting Nodes in a BST【dfs】
1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...
- poj1269 intersecting lines【计算几何】
We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a p ...
- zero-shor learning 数据集
OSR数据集下载地址: http://people.csail.mit.edu/torralba/code/spatialenvelope/ Relative Attributes Marr Priz ...
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...