EOJ 1189 wall 计算几何

来源:互联网 发布:项尖数据恢复注册码 编辑:程序博客网 时间:2024/06/05 14:16

思路就是先求凸包,然后可以证明出距离为L的城墙周长正好是凸包周长加上2*pi*L(画图可以看出)

#include "set"#include "map"#include "queue"#include "stack"#include "cmath"#include "cstdio"#include "cstdlib"#include "iostream"#include "algorithm"#define EPS 1e-10#define MAX_N 100000#define Pi acos(-1.0)#define INF 0x3f3f3f3f#pragma warning(disable:4996)#define up(i, low, high) for (int i = low; i < high; i++)#define down(i, high, low) for (int i = high; i >= low; i--)using namespace std;double add (double a, double b){if (abs (a + b) < EPS * (abs (a) + abs (b))) return 0;return a + b;}//二维向量结构体struct P{double x, y;P () {}P (double x, double y) : x (x), y (y) {};P operator + (P p){return P (add (x, p.x), add (y, p.y));}P operator - (P p){return P (add (x, -p.x), add (y, -p.y));}P operator * (double d){return P (x * d, y * d);}//内积double dot (P p){return add (x * p.x, y * p.y);}//外积double det (P p){return add (x * p.y, -y * p.x);}};P a[MAX_N];bool cmp_x (P a, P b){return a.x < b.x || (a.x == b.x && a.y < b.y);}//计算凸包vector<P> Graham (P* ps, int n){sort (ps, ps + n, cmp_x);//凸包的顶点数int k = 0;//构造中的凸包vector<P> qs (n * 2);//构造凸包的下侧up (i, 0, n){//注意要小于等于0while (k > 1 && (qs[k - 1] - qs[k - 2]).det (ps[i] - qs[k - 1]) <= 0) k--;qs[k++] = ps[i];}//构造凸包的上侧int t = k;down (i, n - 2, 0){while (k > t && (qs[k - 1] - qs[k - 2]).det (ps[i] - qs[k - 1]) <= 0) k--;qs[k++] = ps[i];}//0被扫描了两次qs.resize (k - 1);return qs;}//距离的平方double dis (P p, P q){return (p - q).dot (p - q);}int main (){//freopen ("D:\\Test_in.txt", "r", stdin);//freopen ("D:\\Test_out.txt", "w", stdout);int n, L;while (~scanf ("%d%d", &n, &L)){vector<int> p;up (i, 0, n) scanf ("%lf%lf", &a[i].x, &a[i].y);vector<P> Convex_Hull = Graham (a, n);double ans = 0;up (i, 0, Convex_Hull.size () - 1){ans += sqrt (dis (Convex_Hull[i], Convex_Hull[i + 1]));}//printf ("%.0f\n", ceil (ans));ans += sqrt (dis (Convex_Hull[Convex_Hull.size () - 1], Convex_Hull[0]));ans += 2 * Pi * L;printf ("%.0f\n", ans);}}


0 0
原创粉丝点击