题解:P13182 [GCJ 2017 Finals] Omnicircumnavigation

· · 题解

Another computational geometry problem finds that Tourist K has omnicircumnavigation.

☝️🤓

Omni, from Latin, meaning all or every.

Circum, meaning around.

And navigation, from French, act of moving on water in ships or other vessels.

A navigation route that circles the Earth in any orientation.

But what does this mean?

以下称 Omnicircumnavigation 为“优美的”。

首先我们只要关注路径上的顶点集合。路径是优美的当且仅当不存在一个大圆(球面上最大的圆)将点集全部划分到一侧(不能正好在大圆上)。

特判如果有两个点互为对锺点。删去在球面上重复的点。

如果是不优美的,则存在一个上述的大圆。我们扰动这个大圆直到其只碰到其中一个点,设为点 P。那么转动地球,将 P 移到北极点,剩下的点一定在同一个半球(严格)内。

于是枚举这个 P,设其向量为 p,将其他所有点沿着 p 方向投影到同一个平面上,这些点的向量极角排序后看是否有两个相邻向量之间角度大于 \frac\pi 2(间隔大于 180^{\circ},在同一个半球)。

当然判断是否在同一个半球的极角排序是不用真的排序的,只要随便找一个向量,遍历求出左侧和右侧角度最大的向量,判断这两个向量之间的夹角方向即可。

这样就做完了,实现时其实不用投影和旋转,巧用点积和叉积。实现方面的细节请见代码。

强调一下拍到平面的两个向量方向恰好平行或相反的情况,我的处理方式是分别和 p 作叉积,看这两个结果之间点积的符号即可,需要开 __int128。

不涉及任何浮点运算,O(n^2) 完成此题。

Thanks so much for watching. Take care of yourself, and be well.

#include<bits/stdc++.h>
using namespace std;
#define rep(Ii,Jj,Kk) for(int Ii=(Jj),Ii##_=(Kk);Ii<=Ii##_;Ii++)
#define per(Ii,Jj,Kk) for(int Ii=(Jj),Ii##_=(Kk);Ii>=Ii##_;Ii--)
typedef long long ll;
#define siz(Aa) ((int)(Aa).size())
#define all(Aa) (Aa).begin(),(Aa).end()
#define int ll
struct node{
    int x,y,z;
    friend int operator*(node u,node v){// inner product
        return u.x*v.x+u.y*v.y+u.z*v.z;
    }
    friend node operator^(node u,node v){// outer product
        return {
        /* x */ u.y*v.z-u.z*v.y,
        /* y */ u.z*v.x-u.x*v.z,
        /* z */ u.x*v.y-u.y*v.x
        };
    }
    bool iszero(){
        return x==0 && y==0 && z==0;
    }
    friend bool operator==(node u,node v){
        return u.x==v.x && u.y==v.y && u.z==v.z;
    }
    friend istream& operator>>(istream&is,node&u){return is>>u.x>>u.y>>u.z;}
    friend ostream& operator<<(ostream&os,const node&u){return os<<u.x<<" "<<u.y<<" "<<u.z;}
    // void print(){
        // db len=sqrt(x*x+y*y+z*z);
        // cout<<"("<<x/len<<","<<y/len<<","<<z/len<<")\n";
    // }
};
int mixed_product(node u,node v,node w){
    return (u^v)*w;
}
__int128 big_inner(node u,node v){
    return (__int128)u.x*v.x+(__int128)u.y*v.y+(__int128)u.z*v.z;
}
int n;
node pers,tar;
int pers_mixed_product(node u,node v){
    return mixed_product(v,u,pers);
}
vector<node> v,w,f;
void work(){
    cin>>n;
    v.resize(n);
    rep(i,0,n-1) cin>>v[i];
    w.clear();
    // 判断掉对锺点,删除重复点
    rep(i,0,n-1){
        bool flag=true;
        rep(j,i+1,n-1) if((v[i]^v[j]).iszero()){
            if(v[i]*v[j]>0){// 同一个点
                flag=false;
            }else{// 对锺点
                cout<<"YES";
                return ;
            }
        }
        if(flag){
            w.push_back(v[i]);
        }
    }
    /*
    枚举一个点,以这个点的向量为轴,将所有点压缩到一个平面上
    若所有点在一个小于半圆的扇区内,则 NO
    */
    rep(i,0,siz(w)-1){
        pers=w[i];
        tar=w[!i];
        node L=tar,R=tar;
        bool flag=false;
        rep(j,0,siz(w)-1) if(j!=i){
            int delta=pers_mixed_product(tar,w[j]);
            if(delta>0){// L
                if(pers_mixed_product(L,w[j])>0){
                    L=w[j];
                }
            }else if(delta<0){// R
                if(pers_mixed_product(w[j],R)>0){
                    R=w[j];
                }
            }else{
                node a=tar^pers,b=w[j]^pers;
                if(big_inner(a,b)<0){// 投影到平面上对锺
                    cout<<"YES";
                    return ;
                }
            }
        }
        if(L==tar && R==tar){
            cout<<"NO";
            return ;
        }
        if(pers_mixed_product(L,R)<0){
            cout<<"NO";
            return ;
        }
    }
    cout<<"YES";
}
signed main(){ios::sync_with_stdio(false),cin.tie(nullptr);
    int T;cin>>T;
    rep(i,1,T){
        cout<<"Case #"<<i<<": ";
        work();
        cout<<"\n";
    }
return 0;}