hdu 2108 Shape of HDU 凸包

http://acm.hdu.edu.cn/showproblem.php?pid=2108

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

struct point { double x,y; };
bool mult(point sp,point ep,point op){
    return (sp.x-op.x)*(ep.y-op.y)>=(ep.x-op.x)*(sp.y-op.y);
}

bool operator < (const point &l,const point &r){
    return l.y<r.y || (l.y==r.y && l.x < r.x);
}

int graham(point pnt[],int n,point res[]){ //pnt是图中的所有的点,res是通过判断后在凸边行边上的点,而且这些点都是按逆时针存储的,n是所有点的个数
    int i,len,k = 0,top = 1;
    sort(pnt,pnt+n);
    if(n == 0) return 0; res[0]=pnt[0];
    if(n == 1) return 1; res[1]=pnt[1];
    if(n == 2) return 2; res[2]=pnt[2];
    for(i=2;i<n;i++) {
        while(top && mult(pnt[i],res[top],res[top-1]))
            top--;
        res[++top] = pnt[i];
    }
    len = top; res[++top] = pnt[n-2];
    for(i=n-3;i>=0;i--){
        while(top!=len && mult(pnt[i],res[top],res[top-1]))
            top--;
        res[++top]=pnt[i];
    }
    return top; // 返回凸包中点的个数
}

point res[50001],pnt[50001];

int main() {
    int n;
    while(~scanf("%d",&n) && n) {
        for(int i=0;i<n;i++) scanf("%lf%lf",&pnt[i].x,&pnt[i].y);
        if(graham(pnt,n,res) == n) puts("convex");
        else puts("concave");   
    }
    return 0;    
}

  

原文地址:https://www.cnblogs.com/tobec/p/3341877.html