bzoj 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘——凸包
生活随笔
收集整理的這篇文章主要介紹了
bzoj 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘——凸包
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670
用叉積判斷。注意兩端的平行于 y 軸的。
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define ll long long #define db double using namespace std; const int N=5005,INF=1e7; int n,sta[N],top;db ans; struct Node{int x,y;Node(int x=0,int y=0):x(x),y(y) {}bool operator< (const Node &b)const{return x<b.x||(x==b.x&&y<b.y);}Node operator- (const Node &b)const{return Node(b.x-x,b.y-y);} }a[N]; ll Sqr(int x){return (ll)x*x;} ll Cross(Node a,Node b){return (ll)a.x*b.y-(ll)a.y*b.x;} db dist(Node a,Node b){return sqrt(Sqr(a.x-b.x)+Sqr(a.y-b.y));} int main() {scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);sort(a+1,a+n+1);for(int i=1;i<=n;i++){while(top>1&&Cross(a[i]-a[sta[top]],a[i]-a[sta[top-1]])>=0)top--;sta[++top]=i;}for(int i=2;i<=top;i++)ans+=dist(a[sta[i-1]],a[sta[i]]);for(int i=1;i<=n;i++)a[i].y=INF-a[i].y+1;sort(a+1,a+n+1);top=0;for(int i=1;i<=n;i++){while(top>1&&Cross(a[i]-a[sta[top]],a[i]-a[sta[top-1]])>=0)top--;sta[++top]=i;}for(int i=2;i<top;i++)ans+=dist(a[sta[i-1]],a[sta[i]]);if(top>1&&a[sta[top]].x!=a[sta[top-1]].x)ans+=dist(a[sta[top-1]],a[sta[top]]);if(a[1].x==a[2].x)ans+=abs(a[1].y-a[2].y);printf("%.2f\n",ans);return 0; }?
轉載于:https://www.cnblogs.com/Narh/p/10143271.html
總結
以上是生活随笔為你收集整理的bzoj 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘——凸包的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Taro 多端项目实践笔记
- 下一篇: SECD machine