POJ 1113 Wall 二维凸包
来源:互联网 发布:去掉数组中重复的元素 编辑:程序博客网 时间:2024/05/16 04:41
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 100200 400300 400300 300400 300400 400500 400500 200350 200200 200
Sample Output
1628
Hint
Source
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
城堡围墙长度最小值 = 城堡顶点坐标构成的散点集的凸包总边长 + 半径为L的圆周长
证明如下:假如顺时针给出四个点A、B、C、D。组成了凸四边形ABCD。我们不妨过A点作AE垂直于AB,同时过A点再作AF垂直于AD,过B点作BG、BH分别垂直于AB、BC。连结EG,垂线段的长度为L,过A点以AE为半径作一段弧连到AF,同理,使GH成为一段弧。此时EG=AB(边),AB段城墙的最小值为EF+弧EF+弧GH=AB+弧EF+弧GH。对所有点进行同样的操作后,可知城墙的最小值=四边形的周长+相应顶点的弧长(半径都为L)之和。
下面证明这些顶点弧长组成一个圆。依然以前面的四边形为例。A、B、C、D四顶点各成周角,总和为360*4=1440度,四边形内角和为360度,每个顶点作两条垂线,总角度为4*2*90=720度,所以总圆周角为1440-360-720=360度,刚好组成圆。
所以四边形ABCD的围墙最短= 四边形的周长+圆周长。
推广到任意多边形,用同样的方法,城墙最短=凸包的周长 + 以L为半径的圆的周长。
首先,我们得出城墙最短=凸包的周长 + 相应顶点的弧长(半径都为L)之和。
再证明 相应顶点的弧长(半径都为L)之和=以L为半径的圆的周长。
事实上,设凸包顶点为n,n个顶点组成n个周角,角度为360*n=2*180*n,凸包的内角和为180*(n-2),作了2*n条垂线,和为2*n*90=180*n,所以总圆周角为2*180*n-180*(n-2)-180*n=360,组成圆。
凸包模板我用的是Andrew算法,它是Graham扫描算法的变种,前者的效率高,数值稳定性好
#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;#define PB push_back#define MP make_pair#define CLR(vis) memset(vis,0,sizeof(vis))#define MST(vis,pos) memset(vis,pos,sizeof(vis))#define MAX3(a,b,c) max(a,max(b,c))#define MAX4(a,b,c,d) max(max(a,b),max(c,d))#define MIN3(a,b,c) min(a,min(b,c))#define MIN4(a,b,c,d) min(min(a,b),min(c,d))#define PI acos(-1.0)#define INF 0x7FFFFFFF#define LINF 1000000000000000000LL#define eps 1e-8typedef long long ll;typedef unsigned long long ull;typedef double PointType;const int maxn=1000+100;struct point{ PointType x,y; point(double x=0,double y=0):x(x),y(y) {}};typedef point Vector ;Vector operator + (Vector A , Vector B){ return Vector(A.x + B.x , A.y + B.y);}Vector operator - (point A , point B){ return Vector(A.x - B.x , A.y - B.y);}Vector operator * (Vector A , double p){ return Vector(A.x * p, A.y * p);}Vector operator / (Vector A , double p){ return Vector(A.x / p, A.y / p);}bool operator < (const point& a, const point& b){ return a.x<b.x||(a.x==b.x && a.y<b.y);}int dcmp(double x){ if(fabs(x) < eps) return 0;else return x<0 ?-1:1;}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;}point data[maxn],ans[maxn];int cmp(point a,point b){ if(a.x!=b.x) return a.x<b.x; else return a.y<b.y;}int Convex_hull(point* p,int n,point* ch){ sort(p,p+n,cmp); int m=0; for(int i=0;i<n;i++) { while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--; ch[m++]=p[i]; } int k=m; for(int i=n-2;i>=0;i--) { while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--; ch[m++]=p[i]; } if(n>1) m--; return m;}int main(){ int n; double r; while(scanf("%d%lf",&n,&r)!=EOF) { double sum=0; for(int i=0;i<n;i++) scanf("%lf%lf",&data[i].x,&data[i].y); int res=Convex_hull(data,n,ans); for(int i=1;i<res;i++) sum+=Length(ans[i]-ans[i-1]); sum+=Length(ans[0]-ans[res-1]); sum+=(2*PI*r); printf("%.0lf\n",sum); } return 0;}
- POJ 1113 Wall 二维凸包
- POJ 1113 Wall 凸包
- POJ 1113 Wall (凸包)
- poj 1113 Wall--凸包
- poj 1113 Wall 凸包
- poj 1113 Wall-----凸包
- [poj 1113]Wall[凸包]
- poj 1113 Wall 凸包
- POJ 1113 Wall (凸包)
- POJ 1113 Wall 凸包
- poj 1113 Wall (凸包)
- POJ 1113 Wall (凸包)
- Wall(凸包POJ 1113)
- 【POJ 1113】 Wall (凸包)
- poj 1113 Wall 凸包
- POJ 1113 Wall (凸包)
- Poj 1113 Wall [凸包]
- POJ 1113 WALL(凸包-计算几何)
- 母函数之Big Event in HDU hdoj 1171
- bnu36905 Nested Segments 离散化+线段树
- Android开发周围wifi热点和属性的获取
- 一些有用的数论知识。(莫比乌斯反演)
- 陈老师的多校联合2009年武汉站D题 ||hdu 3033
- POJ 1113 Wall 二维凸包
- hdu 2092 求整数解是否存在
- HDOJ题目2120 Ice_cream's world I(并查集)
- PHP语法自动检查的Vim插件
- UVA524- Prime Ring Problem
- android编程:解决异常“android.view.InflateException: Binary XML file line # : Error inflating class”
- 剑指Offer29数组中出现次数超过一半的数字
- VC6 python 扩展
- 2014多校3(1002)hdu4888(最大流(dinic))