BZOJ 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘
| 
 
 【题目分析】  【代码】 #include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <stack>
#include <cmath>
using namespace std;
struct P{
    int x,y;
}p[5001];
P s[5001];
int top=0;
double ans=0;
inline long long dis(P a,P b)
{return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}
inline P operator-(const P &a,const P &b)
{return (P){a.x-b.x,a.y-b.y};}
inline long long operator*(const P &a,const P &b)
{return a.x*b.y-a.y*b.x;}
inline bool operator<(const P &a,const P &b)
{
    long long x=(a-p[1])*(b-p[1]);
    if (x==0) return dis(p[1],a)<dis(p[1],b);
    else return x<0;
}
int main()
{
    int n;
    scanf("%d",&n);
    for (int i=1;i<=n;++i) scanf("%d%d",&p[i].x,&p[i].y);
    int t=1;
    for (int i=1;i<=n;++i) if (p[i].y<p[t].y||(p[i].y==p[t].y&&p[i].x<p[t].x)) t=i;//扫描一遍,找到起始点 
    swap(p[1],p[t]);
    sort(p+2,p+n+1);
    s[++top]=p[1];s[++top]=p[2];
    for (int i=3;i<=n;++i)
    {
        while (top>=2&&(s[top]-s[top-1])*(p[i]-s[top-1])>=0) top--;
        s[++top]=p[i];
    }
    s[top+1]=p[1];
    for (int i=1;i<=top;++i) ans+=sqrt(dis(s[i],s[i+1]));
    printf("%.2lfn",ans);
}(编辑:源码网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 

