pku1113-Wall 凸包(安德鲁算法版)

Wall

题目链接.
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 42823 Accepted: 14602
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

结果四舍五入就可以了

题意:求凸包周长再加上一个圆周的周长

代码:

#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <set>
#include <vector>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <climits>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
#define inf 0x3f3f3f3f
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MAXN = 1e4 + 7;
const ll MAXM = 1e3 + 7;
const ll MOD = 1e9 + 7;
const double eps = 1e-6;
const double pi = acos(-1.0);
struct node
{
    double x, y;
} a[MAXN];
node sta[MAXN];
int cnt = 0;
bool cmp(node x, node y)
{
    if (x.x == y.x)
        return x.y < y.y;
    return x.x < y.x;
}
double ccw(node a, node b, node c) //利用叉积判断方向(c点是否在ab向量的逆时针方向)
{
     return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x)<0;
    /* 小于0说明顺时针方向 */
}
void convex(int n, node a[])
{
    sort(a, a + n, cmp);
    cnt = 0;
    for (int i = 0; i < n; i++) //计算上半个凸包
    {
        /*如果点数有两个或以上且即将加入凸包的点位于凸包倒数第二点和倒数第一个点所构成的向量的逆时针位置,则删除倒数第一个点*/
        while (cnt > 1 && ccw(sta[cnt - 2], sta[cnt - 1], a[i]))
            --cnt;
        sta[cnt++] = a[i];
    }
    int k = cnt;
    for (int i = n - 2; i >= 0; --i) //计算下半个凸包
    {
        while (cnt > k && ccw(sta[cnt - 2], sta[cnt - 1], a[i]))
            --cnt;
        sta[cnt++] = a[i];
    }
    if (n > 1) //对于只有一个点的包再单独判断
        --cnt;
}
double dis(node x, node y)
{
    return sqrt((y.y - x.y) * (y.y - x.y) + (y.x - x.x) * (y.x - x.x));
}
int main()
{
    int N, L;
    while (~scanf("%d%d", &N, &L))
    {
        for (int i = 0; i < N; i++)
            scanf("%lf%lf", &a[i].x, &a[i].y);
        convex(N, a);
        double ans = 2 * pi * L;
        for (int i = 1; i < cnt; i++)
            ans += dis(sta[i], sta[i - 1]);
        ans += dis(sta[0], sta[cnt - 1]);
        printf("%d\n", (int)(ans+0.5));
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/graytido/p/11121603.html