【计算几何初步-凸包-Graham扫描法-极角序】【HDU1348】 WALL
WallTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build a beautiful brick wall with a perfect shape and nice tall
towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall. Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements. ![]() The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.
Input
The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King's castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to
the castle. Next N lines describe coordinates of castle's vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.
Output
Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King's requirements. You must present the integer number of feet to the King, because the floating numbers
are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates. This problem contains multiple test cases! The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks. The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input
Sample Output
Source
Recommend
|
题目本身不多说了
ANS=凸包周长+2*R*L 很容易证明
Graham算法的话 代码注释也有点解释
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define pi 3.1415926
#define exp 10e-6
using namespace std;
int N,L;
struct point{
double x,y;
};
point A[1050];
point stk[1050];
int s=0;
int k;
int start;
double ans; double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double crossdet(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
double cross(point a,point b,point c)
{
return crossdet(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
}
double dotdet(double x1,double y1,double x2,double y2)
{
return x1*x2+y1*y2;
}
double dot(point a,point b,point c)
{
return dotdet(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
}
int sgn(double x)
{
if(fabs(x)<exp) return 0;
if(x<0) return -1;
else return 1;
}int cmp(point a,point b) // 当叉积为0时通过点积比较的cmp
{
double temp=cross(A[1],a,b);
double temp1;
if(sgn(temp)==0)
{
temp1=dot(a,A[1],b);
return sgn(temp1)<0; // 点靠起点近的排前面
}
else return sgn(temp)>0;
}
/*
int cmp(point a,point b) //当叉积为0时通过距离比较的cmp
{
double temp=cross(A[1],a,b);
double temp1;
if(sgn(temp)==0)
{
return dist(a,A[1])<dist(b,A[1]);
}
else return sgn(temp)>0;
}
/*
int cmp(const void *i,const void *j) //qsort的cmp
{
point *a=(point *)i,*b=(point *)j;
double re=cross(A[1],*a,*b);
if(re==0)
return dist(*a,A[1])>dist(*b,A[1]);
return re<0;
}
*/
void input()
{
memset(stk,0,sizeof(stk));
memset(A,0,sizeof(A));
s=0;ans=0;
cin>>N>>L;
for(int i=1;i<=N;i++)
{
scanf("%lf%lf",&A[i].x,&A[i].y);
}
}
void findmin(int &k) //寻找最小的y,同时最小的话选x小的
{
k=1;
for(int i=2;i<=N;i++)
{
if(sgn(A[i].y-A[k].y)<0)
k=i;
else if(sgn(A[i].y-A[k].y)==0&&(A[i].x-A[k].x)<0)
k=i;
}
}
void solve()
{
findmin(start);
swap(A[1],A[start]);//小细节注意
// qsort(A+2,N-1,sizeof(A[1]),cmp);
sort(A+2,A+N+1,cmp);
for(int i=1;i<=2;i++)
{
stk[++s]=A[i]; //一开始二个肯定在的点入栈
}
for(int i=3;i<=N;i++)
{
while(sgn(cross(stk[s-1],stk[s],A[i]))<=0&&s>=2) //1.防止下面的2个点退栈 2.若stk[i-1]A[i]不在stk[i-1]A[s]的逆时针方向 退栈 寻找更好的凸包
s--;
stk[++s]=A[i]; //入栈 最终栈里面至少有3个点 也显然可知若即使只有3个点 则3个点都在凸包上
}
for(int i=2;i<=s;i++)
{
ans+=dist(stk[i],stk[i-1]);
}
ans+=dist(stk[1],stk[s]);
}
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
int main()
{
int T;
//init();
cin>>T;
int ttt=0;
while(T--)
{
if(ttt++) printf("\n");
input();
solve();
if(N==1) ans=0;
else if(N==2) ans=dist(A[1],A[2]);
ans=ans+2*pi*L; //由题目显然克制 凸包周长加那个圆的面积
printf("%.0lf\n",ans);
}
}
【计算几何初步-凸包-Graham扫描法-极角序】【HDU1348】 WALL的更多相关文章
- [hdu contest 2019-07-29] Azshara's deep sea 计算几何 动态规划 区间dp 凸包 graham扫描法
今天hdu的比赛的第一题,凸包+区间dp. 给出n个点m个圆,n<400,m<100,要求找出凸包然后给凸包上的点连线,连线的两个点不能(在凸包上)相邻,连线不能与圆相交或相切,连线不能相 ...
- poj 1696:Space Ant(计算几何,凸包变种,极角排序)
Space Ant Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2876 Accepted: 1839 Descrip ...
- 凸包--Graham扫描法
一直听大佬们说:凸包.凸包.凸包 一直不会..... 然后.... 今天考试,考了一道计算几何的简单题.... 这,,,还是学一下吧.. 然后考试现场学习一下凸包算法. 先理解一下凸包是啥东西. 看看 ...
- 【计算几何初步-凸包-Jarvis步进法。】【HDU1392】Surround the Trees
[科普]什么是BestCoder?如何参加? Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- 凸包算法(Graham扫描法)详解
先说下基础知识,不然不好理解后面的东西 两向量的X乘p1(x1,y1),p2(x2,y2) p1Xp2如果小于零则说明 p1在p2的逆时针方向 如果大于零则说明 p1在p2的顺时针方向 struct ...
- hrbustoj 1318:蛋疼的蚂蚁(计算几何,凸包变种,叉积应用)
蛋疼的蚂蚁 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 39(22 users) Total Accepted: 26 ...
- 计算几何 : 凸包学习笔记 --- Graham 扫描法
凸包 (只针对二维平面内的凸包) 一.定义 简单的说,在一个二维平面内有n个点的集合S,现在要你选择一个点集C,C中的点构成一个凸多边形G,使得S集合的所有点要么在G内,要么在G上,并且保证这个凸多边 ...
- 凸包模板——Graham扫描法
凸包模板--Graham扫描法 First 标签: 数学方法--计算几何 题目:洛谷P2742[模板]二维凸包/[USACO5.1]圈奶牛Fencing the Cows yyb的讲解:https:/ ...
- Graham 扫描法找凸包(convexHull)
凸包定义 通俗的话来解释凸包:给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边型,它能包含点集中所有的点  Graham扫描法 由最底的一点 \(p_1\) 开始(如果有多个这样的点, ...
随机推荐
- [转]轻量级 Java Web 框架架构设计
工作闲暇之余,我想设计并开发一款轻量级 Java Web 框架,看看能否取代目前最为流行的而又越来越重的 Spring.Hibernate 等框架.请原谅在下的大胆行为与不自量力,本人不是为了重造轮子 ...
- unity4.x for mac破解(含Unity全版本号破解)
声明,破解方式及工具,均来源于国外互联网.仅供交流学习使用! 国外一个大仙做的破解.这位大侠实在是牛,全版本号跟进,win和mac的破解包都有.win下有类似于注冊机的Patch,mac下有crack ...
- STL中map,set的基本用法示例
本文主要是使用了STL中德map和set两个容器,使用了它们本身的一些功能函数(包括迭代器),介绍了它们的基本使用方式,是一个使用熟悉的过程. map的基本使用: #include "std ...
- Python网络爬虫(6)--爬取淘宝模特图片
经过前面的一些基础学习,我们大致知道了如何爬取并解析一个网页中的信息,这里我们来做一个更有意思的事情,爬取MM图片并保存.网址为https://mm.taobao.com/json/request_t ...
- Android学习笔记--JNI的使用方法
1.JNI是什么 JNI是Java Native Interface的缩写,它提供若干的API实现Java与其他语言之间的通信.而Android Framework由基于Java语言的的Java层与基 ...
- Cookie同域,跨域单点登录(转)
Cookie 同域单点登录 最近在做一个单点登录的系统整合项目,之前我们使用控件实现单点登录(以后可以介绍一下).但现在为了满足客户需求,在不使用控件情况下实现单点登录,先来介绍一下单点登录. 单点登 ...
- CSS彻底研究(1)
Github pages 博文 基本选择器 标记选择器h1 {...} 类别.class_name{...},两个class同时作用,如class = 'one two',冲突取前者 ID选择器 #i ...
- java中的io系统详解(转)
Java 流在处理上分为字符流和字节流.字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符.字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组. Java 内用 U ...
- keycode(来自互联网)
- Linq 标准查询操作符三
本文介绍了LINQ标准查询操作符.没有这些操作符,LINQ就不会存在.本文为理解这些操作符的功能提供了很好的基础.了解它们将会很有帮助,因为LINQ的各种Provider都是基于这些操作符来完成各自丰 ...
