zoukankan      html  css  js  c++  java
  • bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘【凸包】

    凸包模板

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int N=5005;
    int n,w,top;
    struct dian
    {
    	double x,y;
    	dian(double X=0,double Y=0)
    	{
    		x=X,y=Y;
    	}
    	dian operator + (const dian &a) const
    	{
    		return dian(x+a.x,y+a.y);
    	}
    	dian operator - (const dian &a) const
    	{
    		return dian(x-a.x,y-a.y);
    	}
    }a[N],s[N],d;
    double cj(dian a,dian b)
    {
    	return a.x*b.y-a.y*b.x;
    }
    bool cmp(const dian &x,const dian &y)
    {
    	double ar=cj(x-d,y-d);
    	return ar>0||(ar==0&&x.x<y.x);
    }
    double dis(dian a,dian b)
    {
    	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    int read()
    {
    	int r=0,f=1;
    	char p=getchar();
    	while(p>'9'||p<'0')
    	{
    		if(p=='-')
    			f=-1;
    		p=getchar();
    	}
    	while(p>='0'&&p<='9')
    	{
    		r=r*10+p-48;
    		p=getchar();
    	}
    	return r*f;
    }
    int main()
    {
    	n=read();
    	d.x=1e9,d.y=1e9;
    	for(int i=1;i<=n;i++)
    	{
    		a[i].x=read(),a[i].y=read();
    		if(a[i].x<d.x||(a[i].x==d.x&&a[i].y<d.y))
    			w=i,d=a[i];
    	}
    	swap(a[w],a[n]);
    	n--;
    	sort(a+1,a+1+n,cmp);
    	s[++top]=d;s[++top]=a[1];
    	for(int i=2;i<=n;i++)
    	{
    		while(top>1&&cj(s[top]-a[i],s[top-1]-a[i])>=0)
    			top--;
    		s[++top]=a[i];
    	}
    	double ans=0;
    	s[top+1]=s[1];
    	for(int i=1;i<=top;i++)
    		ans+=dis(s[i],s[i+1]);
    	printf("%.2lf
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    c# 坑人的发邮件组件
    生成拼音
    FileDb
    WMI tester
    c# 纯代码调用 webservice
    c# 中 利用 CookieContainer 对 Cookie 进行序列化和反序列化校验
    在经过身份验证的服务中不支持跨域 javascript 回调
    c# 使用 namedpipe 通信
    c++ 创建线程以及参数传递
    c#函数地址传入c++
  • 原文地址:https://www.cnblogs.com/lokiii/p/8989034.html
Copyright © 2011-2022 走看看