首页 > 代码库 > BZOJ1670 [Usaco2006 Oct]Building the Moat护城河的挖掘

BZOJ1670 [Usaco2006 Oct]Building the Moat护城河的挖掘

裸的凸包。。。(和旋转卡壳有什么关系吗。。。蒟蒻求教T T)

话说忘了怎么写了。。。(我以前都是先做上凸壳再做下凸壳的说)

于是看了下hzwer的写法,用了向量的点积,方便多了,于是果断学习(Orz)了!

竟然比原作者要快T T

 

 1 /************************************************************** 2     Problem: 1670 3     User: rausen 4     Language: C++ 5     Result: Accepted 6     Time:16 ms 7     Memory:900 kb 8 ****************************************************************/ 9  10 #include <cstdio>11 #include <cmath>12 #include <algorithm>13  14 #define points P15 using namespace std;16 typedef long long ll;17 const int N = 5005;18 int n, top;19 double ans;20 struct points{21     int x, y;22 }p[N], s[N];23  24 inline ll operator * (const P a, const P b){25     return a.x * b.y - a.y * b.x;26 }27  28 inline P operator - (const P a, const P b){29     P tmp;30     tmp.x = a.x - b.x, tmp.y = a.y - b.y;31     return tmp;32 }33  34 inline ll Sqr(const ll x){35     return x * x;36 }37  38 inline ll dis(const P a, const P b){39     return Sqr(a.x - b.x) + Sqr(a.y - b.y);40 }41  42 inline bool operator < (const P a, const P b){43     ll tmp = (a - p[1]) * (b - p[1]);44     return tmp == 0 ? dis(p[1], a) < dis(p[1], b) : tmp > 0;45 }46  47 inline int read(){48     int x = 0, sgn = 1;49     char ch = getchar();50     while (ch < 0 || ch > 9){51         if (ch == -) sgn = -1;52         ch = getchar();53     }54     while (ch >= 0 && ch <= 9){55         x = x * 10 + ch - 0;56         ch = getchar();57     }58     return sgn * x;59 }60  61 int work(){62     int k = 1;63     for (int i = 2; i <= n; ++i)64         if (p[i].y < p[k].y || (p[i].y == p[k].y && p[i].x < p[k].x)) k = i;65     swap(p[1], p[k]);66     sort(p + 2, p + n + 1);67     top = 2, s[1] = p[1], s[2] = p[2];68     for (int i = 3; i <= n; ++i){69         while ((s[top] - s[top - 1]) * (p[i] - s[top]) <= 0) --top;70         s[++top] = p[i];71     }72     s[top + 1] =  p[1];73     for (int i = 1; i <= top; ++i)74         ans += sqrt(dis(s[i], s[i + 1]));75 }76  77 int main(){78     n = read();79     for (int i = 1; i <= n; ++i)80         p[i].x = read(), p[i].y = read();81     work();82     printf("%.2lf\n", ans);83 }
View Code

 

BZOJ1670 [Usaco2006 Oct]Building the Moat护城河的挖掘