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 ...
随机推荐
- 【BZOJ1478】Sgu282 Isomorphism Pólya定理神题
[BZOJ1478]Sgu282 Isomorphism 题意:用$m$种颜色去染一张$n$个点的完全图,如果一个图可以通过节点重新标号变成另外一个图,则称这两个图是相同的.问不同的染色方案数.答案对 ...
- Android之读取 AndroidManifest.xml 中的数据:版本号、应用名称、自定义K-V数据(meta-data)
AndroidManifest.xml中的定义如下: <manifest xmlns:android="http://schemas.android.com/apk/res/andro ...
- Android studio was unable to create a local connection in order...
以管理员身份运行cmd 输入netsh winsock reset 重启电脑
- springMVC前后台交互
后台返回json对象: package com.sawshaw.controller; import org.springframework.stereotype.Controller; import ...
- 关于*** WARNING L15: MULTIPLE CALL TO SEGMENT
编写51程序的时候,有时候会在主函数和中断函数里面调用同一个函数,如果正的出现这种情况,编译器会提出 这种警告: *** WARNING L15: MULTIPLE CALL TO SEGMENT(重 ...
- CodeForces 551C - GukiZ hates Boxes - [二分+贪心]
题目链接:http://codeforces.com/problemset/problem/551/C time limit per test 2 seconds memory limit per t ...
- SS iproute2,nslookup,dig
从某种意义上说,iproute工具集几乎可以替代掉net-tools工具集,具体的替代方案是这样的:用途 net-tool(被淘汰) iproute2地址和链路配置 ifconfig ip ...
- js 通过html()及text()方法获取并设置p标签的显示值
html()方法 此方法类似于JavaScript中的innerHTML属性,可以用来读取或者设置某个元素中的HTML内容.要获取某个元素的内容,可以这样: 复制代码 代码如下: var p_html ...
- Jmeter(五)_函数
JMeter提供了很多函数,如果能够熟练使用,可以为脚本带来很多方便. JMeter函数是一种特殊值,可用于除测试计划外的任何组件. 函数调用的格式如下所示:${__functionName(var1 ...
- 原生js模仿下拉刷新功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...