Wall

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 40363 Accepted: 13754

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

结果四舍五入就可以了

  • 不是直接求凸包,围住城堡的所需的最小距离,这个是凸包的长度,但是建造的围墙和城堡之间还有一个距离L,所以所求周长比凸包长度要多几段圆弧,所有圆弧的角度和为\(360°\),所以再加上一个半径为L的圆周长即为所求.
  • 步进法

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
const int MAX = 5005;
const double PI = acos(-1.0);
typedef struct point {
double x;
double y;
point() { }
point(double a, double b) {
x = a;
y = b;
}
point operator -(const point &b)const { //返回减去后的新点
return point(x - b.x, y - b.y);
}
double operator *(const point &b)const { //点乘
return x*b.x + y*b.y;
}
}point;
double dist(point p1, point p2) { //返回平面上两点距离
return sqrt((p1 - p2)*(p1 - p2));
}
int n, c, w, ans[MAX], num, sd[MAX], ta, stk[MAX], tp1, tp2;
point x[MAX];
bool cmp(point a, point b) { return a.y < b.y || a.y == b.y && a.x < b.x; } //先按y,再按x从小排序
bool cmulti(point p1, point p2, point p3) { //判断p1p0和p2p0的关系,<0,p1p0在p2p0的逆时针方向
return ((p3.x - p1.x)*(p2.y - p1.y) - (p2.x - p1.x)*(p3.y - p1.y))<0;
}
void Jarvis() {
ta = num = 0;
sd[ta++] = 0, sd[ta++] = 1;
sort(x, x + n, cmp);
for (int i = 2; i < n; i++) {
while (ta > 1 && !cmulti(x[sd[ta - 1]], x[sd[ta - 2]], x[i]))//不是外侧点则回溯,且不取内部点
ta--;
sd[ta++] = i;
}
for (int j = 0; j < ta; j++) {
ans[num++] = sd[j];
}
ta = 0;
sd[ta++] = n - 1;
sd[ta++] = n - 2;
for (int i = n - 3; i >= 0; i--) {
while (ta > 1 && !cmulti(x[sd[ta - 1]], x[sd[ta - 2]], x[i]))
ta--;
sd[ta++] = i;
}
for (int j = 0; j < ta; j++) {
ans[num++] = sd[j];
}
}
int main() {
scanf("%d%d", &n, &c);
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &x[i].x, &x[i].y);
}
Jarvis();
double res1 = 0;
for (int i = 0; i < num - 1; i++) {
res1 += dist(x[ans[i]], x[ans[i + 1]]);
}
res1 += 2 * PI * c;
printf("%.0f\n", res1);
return 0;
}
  • 用Gamham-scan重写了一下vo(*  ̄ ▽  ̄ *)ov

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
const int MAX = 5005;
const double PI = acos(-1.0);
typedef struct point {
double x;
double y;
point() { }
point(double a, double b) {
x = a;
y = b;
}
point operator -(const point &b)const { //返回减去后的新点
return point(x - b.x, y - b.y);
}
double operator *(const point &b)const { //点乘
return x*b.x + y*b.y;
}
}point;
double dist(point p1, point p2) { //返回平面上两点距离
return sqrt((p1 - p2)*(p1 - p2));
}
int n, res[MAX]; //ans为凸包点集坐标,n为点的个数,sd为临时坐标。
int top = 1;
point p[MAX]; //x存放凸包点集
bool cmp(point a, point b) {
if (a.y == b.y) return a.x < b.x;
return a.y < b.y;
}
bool multi(point p1, point p2, point p0) { //判断p1p0和p2p0的关系,<0,p1p0在p2p0的逆时针方向,>0,p1p0在p2p0的顺时针方向
return (p1.x - p0.x)*(p2.y - p0.y) >= (p2.x - p0.x)*(p1.y - p0.y);
}
void Graham(int n) {
int i, len; //top模拟栈顶
sort(p, p + n, cmp);
//少于3个点也就没有办法形成凸包
if (n == 0)return; res[0] = 0;
if (n == 1)return; res[1] = 1;
if (n == 2)return; res[2] = 2;
for (i = 2; i < n; i++) {
while (top&&multi(p[i], p[res[top]], p[res[top - 1]])) //如果当前这个点和栈顶两个点构成折线右拐了,就回溯到上一个点
top--; //弹出栈顶
res[++top] = i; //否则将这个点入栈
}
len = top;
res[++top] = n - 2;
for (int i = n - 3; i >= 0; i--) {
while (top!=len&&multi(p[i], p[res[top]], p[res[top - 1]]))
top--;
res[++top] = i;
}
}
int main() {
int c;
scanf("%d%d", &n, &c);
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &p[i].x, &p[i].y);
}
Graham(n);
double res1 = 0;
for (int i = 0; i < top; i++) {
res1 += dist(p[res[i]], p[res[i + 1]]);
}
res1 += 2 * PI * c;
printf("%.0f\n", res1);
return 0;
}

POJ 1113--Wall(计算凸包)的更多相关文章

  1. 题解报告:poj 1113 Wall(凸包)

    Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...

  2. POJ 1113 Wall【凸包周长】

    题目: http://poj.org/problem?id=1113 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  3. POJ 1113 Wall (凸包)

    Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...

  4. POJ 1113 Wall(凸包)

    [题目链接] http://poj.org/problem?id=1113 [题目大意] 给出一个城堡,要求求出距城堡距离大于L的地方建围墙将城堡围起来求所要围墙的长度 [题解] 画图易得答案为凸包的 ...

  5. POJ 1113 Wall 求凸包

    http://poj.org/problem?id=1113 不多说...凸包网上解法很多,这个是用graham的极角排序,也就是算导上的那个解法 其实其他方法随便乱搞都行...我只是测一下模板... ...

  6. POJ 1113 Wall 求凸包的两种方法

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31199   Accepted: 10521 Descriptio ...

  7. 2018.07.04 POJ 1113 Wall(凸包)

    Wall Time Limit: 1000MS Memory Limit: 10000K Description Once upon a time there was a greedy King wh ...

  8. POJ 1113 Wall(计算几何の凸包)

    Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...

  9. poj 1113 Wall 凸包的应用

    题目链接:poj 1113   单调链凸包小结 题解:本题用到的依然是凸包来求,最短的周长,只是多加了一个圆的长度而已,套用模板,就能搞定: AC代码: #include<iostream> ...

  10. poj 1113:Wall(计算几何,求凸包周长)

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28462   Accepted: 9498 Description ...

随机推荐

  1. 天气小雨, 心情多云, 练习标准的键盘ABC打法

    今天看到饿了么转型生活做千亿美元公司 突然想到一些就写下来 当时外卖一份8元 10元的年代那个开心啊 很久以前宁可跑个远, 都不愿意叫外卖 叫了大概1年的外卖了, 之前还感到便宜多样, 现在感觉到的是 ...

  2. vbScript: 编号成生不夠位數前面加零

    '编号成生Geovin Du '不夠位數前面加零 塗聚文 Function getStringlen(str,lenint) so="" itop=0 slen=Len(str) ...

  3. es6新语法的使用

    1.声明变量: let 声明变量 作用域代码块作用域{} 尽在模块 先使用后声明 会报错 { let a= 12; alert(a) } let 不允许重复声明同一个变量 const 声明是一个常量, ...

  4. yanxin8文章归档

    文章归档 - 2015年四月 (共21篇文章) 26日: 14443协议的CRC_A和CRC_B (0条评论) 25日: 百度钱包-1分钱5元话费 (0条评论) 22日: 驾照考试总结 (0条评论) ...

  5. ASP.NET 中对大文件上传的简单处理

    在 ASP.NET 开发的过程中,文件上传往往使用自带的 FileUpload 控件,可是用过的人都知道,这个控件的局限性十分大,最大的问题就在于上传大文件时让开发者尤为的头疼,而且,上传时无法方便的 ...

  6. Linux 一直提示 login incorrect

  7. HTML基础内容(持续更新...)

    1.<!DOCTYPE html>声明有助于浏览器中正确显示网页 HTML5<!DOCTYPE html>HTML 4.01<!DOCTYPE HTML PUBLIC & ...

  8. Sharepoint学习笔记—修改SharePoint的Timeouts (Execution Timeout)

    有时在Sharepoin中有些执行任务可能会超过Sharepoint环境默认的Timout限制,这种情况下系统会报"Request Timed out"错误.对此我们可以在两个层次 ...

  9. python 后台服务

    centos 6x #!/bin/sh # chkconfig: 123456 90 10 # TTS Server for Speech Synthesis # workdir=/etc/speec ...

  10. String, StringBuffer and StringBuilder

    一 String 概述: String 被声明为 final,因此它不可被继承. 在 Java 8 中,String 内部使用 char 数组存储数据. public final class Stri ...