2013
11-09

# Wall

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.

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.

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.

9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200

1628

import java.io.BufferedReader;
import java.io.IOException;

public class Main {

class Point {
int x;
int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}
}

public static void main(String[] args) throws IOException {
Main main = new Main();
System.in));
String[] s;
int n = Integer.parseInt(s[0]);
int l = Integer.parseInt(s[1]);
int x, y;
Point[] p = new Point[n];
for (int i = 0; i < n; i++) {
x = Integer.parseInt(s[0]);
y = Integer.parseInt(s[1]);
p[i] = main.new Point(x, y);
}
Point[] ch = new Point[n];
int len = 0;
if (n >= 3) {
len = Graham_scan(p, ch, n);
}
double sum = 0;
for (int i = 0; i < len - 1; i++) {
sum += distance(ch[i], ch[i + 1]);
}
if (len > 1) {
sum += distance(ch[len - 1], ch[0]);
}
sum += 2 * l * Math.PI;
System.out.println(Math.round(sum));

}

public static double multiply(Point p1, Point p2, Point p0) {
return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y));
}

public static double distance(Point p1, Point p2) {
return (Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y)
* (p1.y - p2.y)));
}

public static int Graham_scan(Point[] PointSet, Point[] ch, int n) {
int i, j, k = 0, top = 2;
Point tmp;
for (i = 1; i < n; i++)
if ((PointSet[i].y < PointSet[k].y)
|| ((PointSet[i].y == PointSet[k].y) && (PointSet[i].x < PointSet[k].x)))
k = i;
tmp = PointSet[0];
PointSet[0] = PointSet[k];
PointSet[k] = tmp;
for (i = 1; i < n - 1; i++) {
k = i;
for (j = i + 1; j < n; j++)
if ((multiply(PointSet[j], PointSet[k], PointSet[0]) > 0)
|| ((multiply(PointSet[j], PointSet[k], PointSet[0]) == 0) && (distance(
PointSet[0], PointSet[j]) < distance(
PointSet[0], PointSet[k]))))
k = j;
tmp = PointSet[i];
PointSet[i] = PointSet[k];
PointSet[k] = tmp;
}
ch[0] = PointSet[0];
ch[1] = PointSet[1];
ch[2] = PointSet[2];
for (i = 3; i < n; i++) {
while (top > 0 && multiply(PointSet[i], ch[top], ch[top - 1]) >= 0)
top--;
ch[++top] = PointSet[i];
}
}