zoukankan      html  css  js  c++  java
  • [poj3348]Cows

    题目大意:求凸包面积。

    解题关键:模板题,叉积求面积。

    这里的cmp函数需要调试一下,虽然也对,与普通的思考方式不同。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    using namespace std;
    typedef long long ll;
    struct point{
        double x,y;
        point(){}
        point(double _x,double _y){x=_x;y=_y;}
        point operator-(const point &b)const{return point(x-b.x,y-b.y);}
        double operator^(const point &b)const{return x*b.y-y*b.x;}
        double operator*(const point &b)const{return x*b.x+y*b.y;}
    }A[10010],result[10010];
    int dist(point a,point b){
        return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    }
    int cp(point p1,point p2,point p3){
        return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.x);
    }
    bool cmp(point a,point b){
        int ans=cp(A[0],a,b);
        if(ans==0) return dist(A[0],a)-dist(A[0],b)<=0;
        else return ans>0;
    }
    int n;
    int main(){
        while(scanf("%d",&n)!=EOF){
            int pos=0;
            for(int i=0;i<n;++i){
                scanf("%lf%lf",&A[i].x,&A[i].y);
                if(A[pos].y>=A[i].y){
                    if(A[pos].y==A[i].y){
                        if(A[pos].x>A[i].x)pos=i;
                    }
                    else pos=i;
                }
            }
            if(n<3){
                printf("0
    ");
                continue;
            }
            int top=1;
            swap(A[0],A[pos]);
            sort(A+1,A+n,cmp);
            result[0]=A[0];result[1]=A[1];
            for(int i=2;i<n;++i){
                while(cp(result[top-1],result[top],A[i])<0)top--;
                result[++top]=A[i];
            }
            
            double s=0;
            for(int i=1;i<top;++i){
                point t1=result[i]-result[0],t2=result[i+1]-result[0];
                double area=fabs(t1^t2)*0.5;
                s+=area;
            }
            printf("%d
    ",(int)(s/50.0));
        }
        return 0;
    }
  • 相关阅读:
    BZOJ1106[POI2007]立方体大作战tet
    BZOJ4407 于神之怒加强版
    BZOJ1103: [POI2007]大都市meg
    BZOJ3170: [Tjoi2013]松鼠聚会
    Luogu 2912 [USACO08OCT]牧场散步Pasture Walking
    BZOJ1251 序列终结者- splay
    BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队
    BZOJ 1005[HNOI2008]明明的烦恼
    二叉树
    [CODEVS1130]数字反转
  • 原文地址:https://www.cnblogs.com/elpsycongroo/p/8744725.html
Copyright © 2011-2022 走看看