题解报告:poj 1113 Wall(凸包)
Description
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
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
Sample Input
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
Sample Output
1628
Hint
#include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=;
const double PI=acos(-1.0);
struct node{int x,y;};
node vex[maxn];
node stackk[maxn];
bool cmp1(node a,node b){
if(a.y==b.y)return a.x<b.x;
else return a.y<b.y;
}
bool cmp2(node a,node b){
double A=atan2(a.y-stackk[].y,a.x-stackk[].x);
double B=atan2(b.y-stackk[].y,b.x-stackk[].x);
if(A!=B)return A<B;
else return a.x<b.x;
}
int cross(node p0,node p1,node p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dis(node a,node b){
return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
}
int main(){
int n,l;
while(~scanf("%d%d",&n,&l)){
for(int i=;i<n;++i)//输入t个点
scanf("%d%d",&vex[i].x,&vex[i].y);
memset(stackk,,sizeof(stackk));
sort(vex,vex+n,cmp1);
stackk[]=vex[];
sort(vex+,vex+n,cmp2);
stackk[]=vex[];
int top=;
for(int i=;i<n;++i){
while(top>&&cross(stackk[top-],stackk[top],vex[i])<=)top--;
stackk[++top]=vex[i];
}
double s=;
for(int i=;i<=top;++i)
s+=dis(stackk[i-],stackk[i]);
s+=dis(stackk[top],vex[]);
s+=*PI*l;//加上圆的周长
printf("%d\n",(int)(s+0.5));//四舍五入
}
return ;
}
AC代码二(32ms):Andrew算法:时间复杂度为O(nlogn),但比Graham-scan算法还快!
#include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=;
const double PI=acos(-1.0);
struct node{int x,y;}vex[maxn],stackk[maxn];
bool cmp(node a,node b){//坐标排序
return ((a.y<b.y)||(a.y==b.y&&a.x<b.x));
}
int cross(node p0,node p1,node p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dis(node a,node b){
return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
}
int main(){
int n,l;
while(~scanf("%d%d",&n,&l)){
for(int i=;i<n;++i)
scanf("%d%d",&vex[i].x,&vex[i].y);
memset(stackk,,sizeof(stackk));
sort(vex,vex+n,cmp);
int top=-;
for(int i=;i<n;++i){//构造凸包下侧
while(top>&&cross(stackk[top-],stackk[top],vex[i])<=)top--;
stackk[++top]=vex[i];
}
for(int i=n-,k=top;i>=;--i){//构造凸包上侧
while(top>k&&cross(stackk[top-],stackk[top],vex[i])<=)top--;
stackk[++top]=vex[i];
}
double s=;
for(int i=;i<=top;++i)//计算凸包周长
s+=dis(stackk[i-],stackk[i]);
s+=*PI*l;
printf("%d\n",(int)(s+0.5));
}
return ;
}
题解报告:poj 1113 Wall(凸包)的更多相关文章
- poj 1113 Wall 凸包的应用
题目链接:poj 1113 单调链凸包小结 题解:本题用到的依然是凸包来求,最短的周长,只是多加了一个圆的长度而已,套用模板,就能搞定: AC代码: #include<iostream> ...
- POJ 1113 Wall 凸包 裸
LINK 题意:给出一个简单几何,问与其边距离长为L的几何图形的周长. 思路:求一个几何图形的最小外接几何,就是求凸包,距离为L相当于再多增加上一个圆的周长(因为只有四个角).看了黑书使用graham ...
- POJ 1113 Wall 凸包求周长
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26286 Accepted: 8760 Description ...
- POJ 1113 - Wall 凸包
此题为凸包问题模板题,题目中所给点均为整点,考虑到数据范围问题求norm()时先转换成double了,把norm()那句改成<vector>压栈即可求得凸包. 初次提交被坑得很惨,在GDB ...
- poj 1113 wall(凸包裸题)(记住求线段距离的时候是点积,点积是cos)
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43274 Accepted: 14716 Descriptio ...
- POJ 1113 Wall(凸包)
[题目链接] http://poj.org/problem?id=1113 [题目大意] 给出一个城堡,要求求出距城堡距离大于L的地方建围墙将城堡围起来求所要围墙的长度 [题解] 画图易得答案为凸包的 ...
- POJ 1113 Wall【凸包周长】
题目: http://poj.org/problem?id=1113 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- poj 1113:Wall(计算几何,求凸包周长)
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28462 Accepted: 9498 Description ...
- POJ 1113 Wall 求凸包
http://poj.org/problem?id=1113 不多说...凸包网上解法很多,这个是用graham的极角排序,也就是算导上的那个解法 其实其他方法随便乱搞都行...我只是测一下模板... ...
随机推荐
- wxpython中控件对键盘输入无响应的可能原因
问题描述: 开发环境:Win7 32bit + Python2.7.6 + WxPython 3.0.1-b20140707 开发某初级CAD软件中,需要实现点击TreeCtrl控件的相应选择,实现G ...
- Linux fork函数具体图解-同一时候分析一道腾讯笔试题
原创blog.转载请注明出处 头文件: #include<unistd.h> #include<sys/types.h> 函数原型: pid_t fork( void); (p ...
- 使用libcurl的包装库cpr发起http请求
cpr GitHub地址https://github.com/whoshuu/cpr 简单示例:cpr_http_request.cpp #include <iostream> #incl ...
- IDEA中Git的应用场景
工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程git仓库上获取项目源码 场景三:小 ...
- html 常用转译空格字符
本人有时候做表格强迫症,字段有的是3个字有的是4个字,也有两个字的,所有不对齐感觉看着难受, 因此需要用空格来让表头文字对齐,找到了下面几个常用的转译字符. 1. &160#;不断行的空白( ...
- ios状态栏的一些操作
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; //显示 [UIApplication sharedA ...
- Notepad++文本编辑器 快捷键
Ctrl+C 复制Ctrl+X 剪切Ctrl+V 粘贴Ctrl+Z 撤消Ctrl+Y 恢复Ctrl+A 全选Ctrl+F 键查找对话框启动Ctrl+H 查找/替换对话框Ctrl+D 复制并粘贴当行 C ...
- Java 通过 HTTP 下载文件
1. [代码]Download.java package core.spider; import java.io.*;import java.net.*;import java.util.*; / ...
- silverlight 使用IValueConverter 转换
在绑定数据中 有时候我们需要转换相关数据类型 silverlight提供了一个System.Windows.Data.IValueConverter接口它提供两个方法 Convert和ConvertB ...
- .NETFramework:Encoding
ylbtech-.NETFramework:Encoding 1.返回顶部 1. #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, Pub ...