其他分享
首页 > 其他分享> > 2019 ICPC Malaysia National H题

2019 ICPC Malaysia National H题

作者:互联网

题意:给定n个点,求出这些点构成的凸包,然后逆时针输出,另外还有q次询问,每次询问一个点是否在凸包里。

题解:二维凸包裸题,直接利用叉积判断点是否在凸包内即可,时间复杂度n2,不知为何这题给了15s,然后我代码只跑了15ms

#include <bits/stdc++.h>
using namespace std;
double eps=1e-15;
double pi=acos(-1);
struct Point{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (Vector A,double B){return Vector(A.x*B,A.y*B);}
Vector operator / (Vector A,double B){return Vector(A.x/B,A.y/B);}
int dcmp(double x){
    if(fabs(x)<eps)return 0;
    else return x<0?-1:1;
}
bool operator < (const Point &a,const Point &b){
    return dcmp(a.x-b.x)<0||(dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)<0);
}
bool operator == (const Point &a,const Point &b){
    return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double Cross(Vector A,Vector B){
    return A.x*B.y-A.y*B.x;
}
double Dot(Vector A,Vector B){
    return A.x*B.x+A.y*B.y;
}
Vector Rotate(Vector A,double rad){
    return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
int tubao(Point *p,int n,Point *ch){//求凸包,返回凸包数组的长度
    sort(p,p+n);
    int m=0;
    for(int i=0;i<n;i++){
        while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
        ch[m++]=p[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--){
        while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
        ch[m++]=p[i];
    }
    if(n>1)m--;
    return m;
}
void readp(Point &A){
    scanf("%lf%lf",&A.x,&A.y);
}
bool onsegment(Point p,Point a1,Point a2){
    if(p==a1||p==a2)return false;
    return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
}
bool segmentcross(Point a1,Point a2,Point b1,Point b2){
    if(a1==b1||a1==b2||a2==b1||a2==b2)return true;
    double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
           c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
int intubao(Point *ch,int n,Point p){//判断p点是否在凸包内
    Vector A,B;
    int flag=0;
    for(int i=0;i<n;i++){
        A=ch[(i+1)%n]-ch[i];
        B=p-ch[i];
        /*if(onsegment(p,ch[i],ch[(i+1)%n])){//这题说了点在凸包上视为在凸包外
            flag=-1;
            break;
        }*/
        if(Cross(A,B)>0){
            flag++;
        }
    }
    if(flag==-1||flag==n)return 1;
    return 0;
}
int T,n,q,m;
Point p1[10005],ch1[10005];
struct node{
    double x,y;
}g[1005];
int main(){
    scanf("%d",&T);
    int kase=0;
    while(T--){
        scanf("%d%d",&n,&q);
        for(int i=0;i<n;i++){
            readp(p1[i]);
        }
        int m1=tubao(p1,n,ch1);
        for(int i=1;i<=q;i++){
            scanf("%lf%lf",&g[i].x,&g[i].y);
        }
        printf("Case %d\n",++kase);
        for(int i=0;i<m1;i++){
            printf("%d %d\n",(int)ch1[i].x,(int)ch1[i].y);
        }
        printf("%d %d\n",(int)ch1[0].x,(int)ch1[0].y);
        for(int i=1;i<=q;i++){
            Point t;
            t.x=g[i].x;
            t.y=g[i].y;
            printf("%d %d ",(int)t.x,(int)t.y);
            if(intubao(ch1,m1,t))printf("is unsafe!\n");
            else printf("is safe!\n");
        }
        printf("\n");
    }
}

 

标签:ch,return,Point,int,double,National,Malaysia,Vector,2019
来源: https://www.cnblogs.com/ccsu-kid/p/11073903.html