zoukankan      html  css  js  c++  java
  • 护城河的挖掘「USACO 2006」

    题意

    凸包模板,给定平面上点集,求包含所有点的凸包周长最小值。


    思路

    使用(Graham)扫描法解决。

    考虑将最左下的点设为原点(事实上任意点均可作为原点),然后其余各点根据斜率排序。

    对于每一个节点,我们考虑加入它是否会与已有的边点构成内凹,如果会,那么放弃已有边点。

    显然我们可以通过维护一个单调栈完成该过程。

    代码

    #include <bits/stdc++.h>
    
    using namespace std;
    
    namespace StandardIO {
    
    	template<typename T>inline void read (T &x) {
    		x=0;T f=1;char c=getchar();
    		for (; c<'0'||c>'9'; c=getchar()) if (c=='-') f=-1;
    		for (; c>='0'&&c<='9'; c=getchar()) x=x*10+c-'0';
    		x*=f;
    	}
    
    	template<typename T>inline void write (T x) {
    		if (x<0) putchar('-'),x*=-1;
    		if (x>=10) write(x/10);
    		putchar(x%10+'0');
    	}
    
    }
    
    using namespace StandardIO;
    
    namespace Project {
    	
    	const int N=5005;
    	const double eps=1e-9;
    	
    	int n,top;
    	double ans;
    	struct node {
    		int x,y;
    		double rad;
    		node () {}
    		node (int _x,int _y) : x(_x),y(_y) {}
    		friend node operator + (const node &x,const node &y) {
    			return node(x.x+y.x,x.y+y.y);
    		}
    		friend node operator - (const node &x,const node &y) {
    			return node(x.x-y.x,x.y-y.y);
    		}
    		friend int operator * (const node &x,const node &y) {
    			return x.x*y.y-x.y*y.x;
    		}
    		friend bool operator < (const node &x,const node &y) {
    			return (x.x==y.x)?(x.y<y.y):(x.x<y.x);
    		}
    	} a[N],S[N];
    	
    	inline double dist (const node &x,const node &y) {
    		return sqrt((y.x-x.x)*(y.x-x.x)+(y.y-x.y)*(y.y-x.y));
    	}
    	inline bool cmp (const node &x,const node &y) {
    		double k=(x-a[1])*(y-a[1]);
    		if (fabs(k)<=eps) return dist(x,a[1])<dist(y,a[1]);
    		return k>eps;
    	}
    	inline double rad (node x,node y) {
    		return atan2(y.y-x.y,y.x-x.x);
    	}
    
    	inline void MAIN () {
    		read(n);
    		for (register int i=1; i<=n; ++i) {
    			read(a[i].x),read(a[i].y);
    		}
    		sort(a+1,a+n+1);
    		sort(a+2,a+n+1,cmp);
    		S[++top]=a[1],S[++top]=a[2];
    		for (register int i=3; i<=n; ++i) {
    			while ((S[top]-S[top-1])*(a[i]-S[top-1])<=0) --top;
    			S[++top]=a[i];
    		}
    		S[top+1]=a[1];
    		for (register int i=1; i<=top; ++i) {
    			ans+=dist(S[i],S[i+1]);
    		}
    		printf("%.2lf",ans);
    	}
    	
    }
    
    int main () {
    //	freopen(".in","r",stdin);
    //	freopen(".out","w",stdout);
    	Project::MAIN();
    }
    
    
  • 相关阅读:
    工信部计算机系统集成资质(高级)项目经理
    cout cerr clog
    Cstyle 字符串小示例
    泛型<编程>:基于策略的basic_string实现
    string中c_str()、data()、copy(p,n)函数的用法
    C++引用与const引用比较
    深入了解scanf/getchar/gets/cin等函数(转载)
    使用ifstream和getline读取文件内容[c++]
    const参数,const返回值与const函数 .
    C++ limits头文件的用法(numeric_limits)
  • 原文地址:https://www.cnblogs.com/ilverene/p/11372811.html
Copyright © 2011-2022 走看看