[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\n");
            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\n",(int)(s/50.0));
    }
    return 0;
}

 

posted @ 2018-04-08 14:17  Elpsywk  阅读(149)  评论(0编辑  收藏  举报