Wall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28157   Accepted: 9401

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.

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

结果四舍五入就可以了

Source

题意:
            求n个点的凸包的周长和圆的周长的和;
思路:
          了解点的排序,左转判定;
          判断向量p1=(x1,y1)到p2=(x2,y2)是否做左转,只需要判断x1*y2-x2*y1的正负,如果结果为正,则从p1到p2做左转。

1.将各点排序(请参看基础篇),为保证形成圈,把P0在次放在点表的尾部;

2.准备堆栈:建立堆栈S,栈指针设为t,将0、1、2三个点压入堆栈S;

3.对于下一个点i

                只要S[t-1]、S[t]、i不做左转

         就反复退栈;

      将i压入堆栈S

4.堆栈中的点即为所求凸包;

代码:

#include "iostream"   //poj 1113
#include "cstdio"
#include "cmath"
#include "cstring"
#include "iomanip"
#include "algorithm"
using namespace std; const double eps = 1e-8;
const double PI = acos(-1.0); int cmp(double x){ //判断一个实数的正负
if(fabs(x) < eps) return 0;
if(x > 0) return 1;
return -1;
} inline double sqr(double x){ //计算一个数的平方
return x*x;
} struct point{
double x,y;
point(){};
point(double a,double b) : x(a),y(b) {}
friend point operator - (const point &a,const point &b){
return point(a.x - b.x,a.y - b.y);
}
double norm(){
return sqrt(sqr(x) + sqr(y));
}
}; double det(const point &a,const point &b){ //计算两个向量的叉积
return a.x*b.y - a.y*b.x;
} double dist(const point &a,const point &b){ //计算两个点的距离
return (a-b).norm();
} #define N 1005 point start;
point stacks[3*N]; bool cmp_sort(point a,point b) //对点进行极角排序(以左下角的点为参考点)
{
int temp = cmp(det(a-start,b-start));//(叉积判断)
if(temp==1) return true;
else if(temp==-1) return false;
temp = cmp(dist(b,start)-dist(a,start)); //sort排序后只有true和false,牢记!!!
if(temp==1) return true;
return false;
} void Melkman(point *a,int &n) //求n个点的凸包
{
int k; //记录左下点的下标
int i,j;
point temp;
double minf = 100005;
for(i=0; i<n; ++i)
{
if(cmp(a[i].x-minf) == -1)
{
minf = a[i].x;
k = i;
}
else if(cmp(a[i].x-minf) == 0)
{
if(cmp(a[k].y-a[i].y) == 1)
k = i;
}
}
{ //交换点
temp = a[0];
a[0] = a[k];
a[k] = temp;
}
start = a[0];
sort(a+1,a+n,cmp_sort); //排序!
for(i=0; i<N; ++i)
stacks[i].x = stacks[i].y = 0;
stacks[0] = a[0];
stacks[1] = a[1];
int top=1;
i = 2;
while(i<n)
{
if(top==0 || cmp(det(stacks[top-1]-stacks[top],stacks[top]-a[i]))>=0)
{
top++;
stacks[top] = a[i];
i++;
}
else
top--;
}
for(n=0; n<=top; ++n)
a[n] = stacks[n];
} int main()
{
int i,j;
int n,r;
point a[N];
while(scanf("%d %d",&n,&r)!=-1)
{
for(i=0; i<n; ++i)
scanf("%lf %lf",&a[i].x,&a[i].y);
Melkman(a,n);
double ans = 2*PI*r;
a[n] = a[0]; //将n个点连成一个圈
for(i=0; i<n; ++i)
ans += dist(a[i],a[i+1]);
printf("%.0lf\n",ans);
}
return 0;
}

最新文章

  1. set 集合容器实现元素的插入与中序排序
  2. 未封装的js放大镜特效
  3. 数据库整合数据报表SQL实战
  4. ASP.NET分页
  5. Java Web编程的主要组件技术——Struts核心组件
  6. hibernate中session
  7. aspx页面记住密码
  8. Node.js中Async详解:流程控制
  9. Effective Java 第三版——43.方法引用优于lambda表达式
  10. 负载,性能测试工具-Gatling
  11. Java开发笔记(五十五)关键字static的用法
  12. Django项目目录介绍
  13. cf666 C. Codeword 组合数学
  14. Yii Cache 缓存的使用
  15. Git从远程仓库里拉取一条本地不存在的分支方法
  16. bash 设置
  17. [转]Excel.dll 导出Excel控制
  18. MySql报2006error错误的解决方法(数据过大)
  19. 页面链接跳转历史URL不记录的兼容处理
  20. (三)hibernate单表操作

热门文章

  1. Python基础:开篇
  2. 【C#】1.2 控制台应用程序学习要点
  3. windbg sos加载相关
  4. Urlencode and Urldecode 命令行
  5. StackOverflow Update: 560M Pageviews A Month, 25 Servers, And It&#39;s All About Performance
  6. IOS网络编程之请求内容
  7. SharePoint 2013 设置自定义布局页
  8. The URL &quot;filename&quot; is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web
  9. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q60-Q62)
  10. android 判断屏幕是否亮着