凸包入门(Graham扫描法)(A - Wall POJ - 1113)
题目链接:https://cn.vjudge.net/contest/276359#problem/A
题目大意:有一个国王,要在自己的城堡周围建立围墙,要求围墙能把城堡全部围起来,并且围墙距离城堡的距离至少为l,然后问你最小的消耗量。
具体思路: 将围起来城堡的围墙全部往外移,求出这些点构成的凸包,然后再加上半径为l的圆的周长,这就是最终答案。
AC代码:
#include<iostream>
#include<stack>
#include<iomanip>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iomanip>
#include<cmath>
#include<algorithm>
using namespace std;
# define ll long long
const int maxn = 1e3+;
const double pi= acos(-1.0);
struct node
{
double x,y;
} p[maxn],P[maxn];
int n,tot;
double ans,l;
double X(node A,node B,node C)//求叉积的过程
{
return (B.x-A.x)*(C.y-A.y)-(C.x-A.x)*(B.y-A.y);
}
double len(node A,node B)
{
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
bool cmp(node A,node B)
{
double pp=X(p[],A,B);
if(pp>)
return true;
if(pp<)
return false;
return len(p[],A)<len(p[],B);
}
int main()
{
while(~scanf("%d %lf",&n,&l))
{
ans=2.0*pi*l;
for(int i=; i<n; i++)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
}
if(n==)
{
printf("%.0lf\n",ans);
}
else if(n==)
{
printf("%.0lf\n",ans+=len(p[],p[]));
}
else
{
for(int i=; i<n; i++)
{
if(p[i].y<p[].y)
{
swap(p[i],p[]);
}
else if(p[i].y==p[].y&&p[i].x<p[].x)
{
swap(p[i],p[]);
}
}//先把第一个点找出来
sort(p+,p+n,cmp);
P[]=p[];
P[]=p[];//寻找凸包的起点和终点
tot=;
for(int i=; i<n; i++)
{
while(tot>&&X(P[tot-],P[tot],p[i])<=)//斜率相同的时候,取较远的。
tot--;
tot++;
P[tot]=p[i];
}
for(int i=; i<tot; i++)
{
ans+=len(P[i],P[i+]);
}
ans+=len(P[],P[tot]);
printf("%.0lf\n",ans);
}
}
return ;
}
凸包入门(Graham扫描法)(A - Wall POJ - 1113)的更多相关文章
- 凸包模板——Graham扫描法
凸包模板--Graham扫描法 First 标签: 数学方法--计算几何 题目:洛谷P2742[模板]二维凸包/[USACO5.1]圈奶牛Fencing the Cows yyb的讲解:https:/ ...
- Wall - POJ 1113(求凸包)
题目大意:给N个点,然后要修建一个围墙把所有的点都包裹起来,但是要求围墙距离所有的点的最小距离是L,求出来围墙的长度. 分析:如果没有最小距离这个条件那么很容易看出来是一个凸包,然后在加上一个最小距离 ...
- poj1113Wall 求凸包周长 Graham扫描法
#include<iostream> #include<algorithm> #include<cmath> using namespace std; typede ...
- 计算几何--求凸包模板--Graham算法--poj 1113
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28157 Accepted: 9401 Description ...
- POJ 1113 Wall 凸包 裸
LINK 题意:给出一个简单几何,问与其边距离长为L的几何图形的周长. 思路:求一个几何图形的最小外接几何,就是求凸包,距离为L相当于再多增加上一个圆的周长(因为只有四个角).看了黑书使用graham ...
- poj 1113 Wall 凸包的应用
题目链接:poj 1113 单调链凸包小结 题解:本题用到的依然是凸包来求,最短的周长,只是多加了一个圆的长度而已,套用模板,就能搞定: AC代码: #include<iostream> ...
- poj 1113 凸包周长
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33888 Accepted: 11544 Descriptio ...
- 【BZOJ-1670】Building the Moat护城河的挖掘 Graham扫描法 + 凸包
1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 464 Solv ...
- 计算几何 : 凸包学习笔记 --- Graham 扫描法
凸包 (只针对二维平面内的凸包) 一.定义 简单的说,在一个二维平面内有n个点的集合S,现在要你选择一个点集C,C中的点构成一个凸多边形G,使得S集合的所有点要么在G内,要么在G上,并且保证这个凸多边 ...
随机推荐
- PHP面试经常被提到的问题
1. Include 与 require的区别,require和require_once的效率哪个高? PHP在遇到include时就解释一次,如果页面中出现10次include,php就解释10次, ...
- input accept 属性
*.3gpp audio/3gpp, video/3gpp 3GPP Audio/Video *.ac3 audio/ac3 AC3 Audio *.asf allpication/vnd.ms-as ...
- bzoj 1036: [ZJOI2008]树的统计Count (树链剖分+线段树 点权)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 21194 Solved: 8589[Submit ...
- BZOJ 1036 树的统计 | 树链剖分模板题
又做了一遍--去掉读入优化只有八十行~ #include <cstdio> #include <cstring> #include <algorithm> usin ...
- 51nod 1376 最长上升子序列的数量 | DP | vector怒刷存在感!
51nod 1376 最长上升子序列的数量 题解 我们设lis[i]为以位置i结尾的最长上升子序列长度,dp[i]为以位置i结尾的最长上升子序列数量. 显然,dp[i]要从前面的一些位置(设为位置j) ...
- 【转】__ATTRIBUTE__ 你知多少
__ATTRIBUTE__ 你知多少? GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(V ...
- [JSOI2009] 球队收益 (费用流)
终于来发题解啦! pdf版题解 #include<iostream> #include<cstring> #include<cstdio> #include< ...
- Python实现类似JavaScript 的Json对象
Python实现类似JavaScript 的Json对象 用过js的都知道 js中json也是一个对象,所以可以直接通过class.attr 取值,当attr不存在时也不会报错,那么Python可不可 ...
- for,while,do while
long i; ;i<;i++) printf( printf("%ld\n",i); ) printf("b\n"); i=; do { printf( ...
- vim文件头部注释配置
http://note.youdao.com/noteshare?id=26dff538fabf3e8a0c4e85815256d5bb