您的位置:首页 > 其它

HDU 1348 Wall (水平序Graham算法)

2016-02-07 13:58 316 查看

Wall

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4774 Accepted Submission(s): 1370



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

[align=left]Sample Input[/align]

1

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


[align=left]Sample Output[/align]

1628


Graham算法

总长就是凸包的长度加上PI * 2 * L,至于怎么证明。。。

对了,输出的格式也要特别注意

下面是代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <set>
#define PI (atan(1.0) * 4)
using namespace std;
struct point {
int x, y;
};
bool cmp(point a, point b) {
if(a.x != b.x) return a.x < b.x;
else return a.y < b.y;
}
double Graham(point * ver, int N) {
stack<point> S;
while(!S.empty()) {
S.pop();
}
S.push(ver[0]);
S.push(ver[1]);
int i;
int flag;
point t1, t2;
for(i = 2; i < N; i++) {
flag = 0;
t1 = S.top();
S.pop();
t2 = S.top();
S.push(t1);
if((t1.x - t2.x) * (ver[i].y - t1.y) - (ver[i].x - t1.x) * (t1.y - t2.y) <= 0) {
S.pop();
flag = 1;
}
if(S.size() < 2 || flag == 0) {
S.push(ver[i]);
}
else {
i--;
}
}
double len = 0;
t1 = S.top();
S.pop();
while(!S.empty()) {
t2 = S.top();
S.pop();
len += sqrt((t1.x - t2.x) * (t1.x - t2.x) * 1.0 + (t1.y - t2.y) * (t1.y - t2.y) * 1.0);
t1 = t2;
}
return len;
}
int main() {
int T;
point vertices[1010];
point rvertices[1010];
while(~scanf("%d", &T)) {
int N, L;
while(T--) {
scanf("%d %d", &N, &L);
int i;
for(i = 0; i < N; i++) {
scanf("%d %d", &vertices[i].x, &vertices[i].y);
}
sort(vertices, vertices + N, cmp);
int j;
for(i = 0, j = N - 1; i < N; i++, j--) {
rvertices[i] = vertices[j];
}
double len1 = Graham(vertices, N);
double len2 = Graham(rvertices, N);
printf("%.0lf\n", len1 + len2 + PI * (2 * L));
if(T) printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: