• 凸包


    /* 
     * 求凸包,Graham算法 
     * 点的编号0~n-1 
     * 返回凸包结果Stack[0~top-1]为凸包的编号 
     */ 
    const int MAXN = 1010; 
    Point list[MAXN]; 
    int Stack[MAXN],top; 
    //相对于list[0]的极角排序 
    bool _cmp(Point p1,Point p2) 
    { 
    	double tmp = (p1-list[0])^(p2-list[0]); 
      	if(sgn(tmp) > 0)return true; 
      	else if(sgn(tmp) == 0 && sgn(dist(p1,list[0]) - dist(p2,list[0])) <= 0) 
        	return true; 
      	else return false; 
    } 
    void Graham(int n) 
    { 
      	Point p0; 
      	int k = 0; 
      	p0 = list[0]; 
      	//找最下边的一个点 
      	for(int i = 1;i < n;i++) 
      	{ 
        	if( (p0.y > list[i].y) || (p0.y == list[i].y && p0.x > list[i].x) ) 
        	{ 
          	p0 = list[i]; 
          	k = i; 
        	} 
      	} 
      	swap(list[k],list[0]); 
      	sort(list+1,list+n,_cmp); 
      	if(n == 1) 
      	{
    	  top = 1; 
          Stack[0] = 0; 
          return; 
      	} 
      	if(n == 2) 
      	{ 
          top = 2; 
          Stack[0] = 0; 
          Stack[1] = 1; 
          return ; 
        } 
      	Stack[0] = 0; 
      	Stack[1] = 1; 
     	top = 2; 
      	for(int i = 2;i < n;i++) 
      	{ 
        	while(top>1 && sgn((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]]))<=0) 
          	top--; 
    	  	Stack[top++] = i; 
      	} 
    } 
    
    
    3、平面最近点对(HDU 1007) 
    #include <stdio.h> 
    #include <string.h> 
    #include <algorithm> 
    #include <iostream> 
    #include <math.h> 
    using namespace std; 
    const double eps = 1e-6; 
    const int MAXN = 100010; 
    const double INF = 1e20; 
    struct Point 
    { 
        double x,y; 
    }; 
    double dist(Point a,Point b) 
    { 
        return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); 
    } 
    Point p[MAXN]; 
    Point tmpt[MAXN]; 
    bool cmpxy(Point a,Point b) 
    { 
        if(a.x != b.x)return a.x < b.x; 
        else return a.y < b.y; 
    } 
    bool cmpy(Point a,Point b) 
    { 
        return a.y < b.y; 
    } 
    double Closest_Pair(int left,int right) 
    { 
        double d = INF; 
        if(left == right)return d; 
        if(left + 1 == right) 
            return dist(p[left],p[right]); 
        int mid = (left+right)/2; 
        double d1 = Closest_Pair(left,mid); 
        double d2 = Closest_Pair(mid+1,right); 
        d = min(d1,d2); 
        int k = 0; 
        for(int i = left;i <= right;i++) 
        { 
            if(fabs(p[mid].x - p[i].x) <= d) 
                tmpt[k++] = p[i]; 
        } 
        sort(tmpt,tmpt+k,cmpy); 
        for(int i = 0;i <k;i++) 
        { 
            for(int j = i+1;j < k && tmpt[j].y - tmpt[i].y < d;j++) 
            { 
                d = min(d,dist(tmpt[i],tmpt[j])); 
            } 
        } 
        return d; 
    } 
    int main() 
    { 
        int n; 
        while(scanf("%d",&n)==1 && n) 
        { 
            for(int i = 0;i < n;i++) 
                scanf("%lf%lf",&p[i].x,&p[i].y); 
            sort(p,p+n,cmpxy); 
            printf("%.2lf
    ",Closest_Pair(0,n-1)/2); 
        } 
        return 0; 
    } 
    

    题解:LightOJ 1313 - Protect the Mines(凸包)

  • 相关阅读:
    Ajax随笔
    hdu 1395 2^x mod n = 1 暴力过~~最好学下欧拉定理~~~
    云计算设计模式(二)——断路器模式
    STL algorithm算法minmax,minmax_element(36)
    格式化时间
    django 带參数的 url
    Linux下 目录 压缩 解压缩 打包
    事实上就是为了玩
    Datazen配置
    HotSpot关联规则算法(2)-- 挖掘连续型和离散型数据
  • 原文地址:https://www.cnblogs.com/xzxl/p/7247618.html
Copyright © 2020-2023  润新知