UVALive 6859
2016-08-18 18:34
344 查看
凸包模版。
要求将所有点严格包含在内,并且只能走格子的边和对角线。
两种思路:第一种比较好理解,就是将每个点周围四个点,画在图上,去重后套模版,复杂度较高O(4n);其实,将每一个点严格包含在内就是将它旁边的点连接起来,这样同一方向的边与凸包所求出的边是相等的只不过严格包含后需要在结果加上转四个方向时的总长度4 * sqrt(2)就会得到结果。
代码:
要求将所有点严格包含在内,并且只能走格子的边和对角线。
两种思路:第一种比较好理解,就是将每个点周围四个点,画在图上,去重后套模版,复杂度较高O(4n);其实,将每一个点严格包含在内就是将它旁边的点连接起来,这样同一方向的边与凸包所求出的边是相等的只不过严格包含后需要在结果加上转四个方向时的总长度4 * sqrt(2)就会得到结果。
代码:
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int n; struct Point { long long x, y; Point(long long x = 0, long long y = 0) : x(x), y(y) {} bool operator < (const Point &t) const { return x < t.x || x == t.x && y < t.y; } bool operator == (const Point &t) const { return x == t.x && y == t.y; } Point operator - (const Point &t) { return Point(x - t.x, y - t.y); } }p[100005 * 5], ch[100005 * 5]; long long Cross(const Point &t1, const Point &t2) { return t1.x * t2.y - t2.x * t1.y; } int ConvexHull() { int m = 0; for(int i = 0; i < n; ++i) { while(m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) --m; ch[m++] = p[i]; } int k = m; for(int i = n - 2; i >= 0; --i) { while(m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) --m; ch[m++] = p[i]; } if(n > 1) --m; return m; } int main() { while(~scanf("%d", &n)) { for(int i = 0; i < n; ++i) { scanf("%lld%lld", &p[i].x, &p[i].y); } sort(p, p + n); // n = unique(p, p + 5 * n) - p; int m = ConvexHull(); // double ans = 0; long long ans1 = 0; long long ans2 = 0; ch[m] = ch[0]; for(int i = 0; i < m; ++i) { long long d1 = abs(ch[i + 1].x - ch[i].x); long long d2 = abs(ch[i + 1].y - ch[i].y); ans1 += max(d1, d2) - min(d1, d2); ans2 += min(d1, d2); } printf("%.6f\n", ans1 + (ans2 + 4) * sqrt(2)); } }
相关文章推荐
- UVALive 6859 Points
- = =回归。。【UVAlive】6859凸包
- UVALive 6859 Points (凸包)
- UVALive 6859
- UVALive6859 Points +凸包
- uvalive 6859
- UVAlive 6859 Points(凸包)
- UVALive 6859 Points (凸包)
- UVaLive 6859 Points (几何,凸包)
- UVALive 6859 (凸包)
- UVALive 6859 Points (凸包)
- uvalive6859 凸包
- UVALive 6859--凸包
- UVALive - 3902 Network
- UVALive 3683 A Scheduling Problem(树形DP)
- UVaLive/LA 6802 Turtle Graphics(水题)
- UVALive 3029 City Game (扫描维护)
- UVALive 3695 Distant Galaxy (部分枚举,扫描维护)
- uvalive 4253(暴力)
- NEERC 2009 ( UValive 4589) Asteroids