hdu 6617

我人傻了,我两份半平面交的板子都错了??
然后粘了网友的板子过了????
我他妈????
这现场赛万一出个半平面交我都不知道粘哪份板子了
其实题目本身还挺简单的,
维护一下小凸包每条边切的点,
然后二分半平面交就行了。
代码几乎照着网友的抄的。。。。。。好像一共也就几行代码

#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef double db;
const int maxn = 4e5+5;
const db eps=1e-13;
const db pi=acos(-1);
int sign(db k){
    if (k>eps) return 1; else if (k<-eps) return -1; return 0;
}
int cmp(db k1,db k2){return sign(k1-k2);}
int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;}// k3 在 [k1,k2] 内
struct point{
    db x,y;
    point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};}
    point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};}
    point operator * (db k1) const{return (point){x*k1,y*k1};}
    point operator / (db k1) const{return (point){x/k1,y/k1};}
    int operator == (const point &k1) const{return cmp(x,k1.x)==0&&cmp(y,k1.y)==0;}
    // 逆时针旋转
    point turn(db k1){return (point){x*cos(k1)-y*sin(k1),x*sin(k1)+y*cos(k1)};}
    point turn90(){return (point){-y,x};}
    bool operator < (const point k1) const{
        int a=cmp(x,k1.x);
        if (a==-1) return 1; else if (a==1) return 0; else return cmp(y,k1.y)==-1;
    }
    db abs(){return sqrt(x*x+y*y);}
    db abs2(){return x*x+y*y;}
    db dis(point k1){return ((*this)-k1).abs();}
    point unit(){db w=abs(); return (point){x/w,y/w};}
    void scan(){double k1,k2; scanf("%lf%lf",&k1,&k2); x=k1; y=k2;}
    void print(){printf("%.11lf %.11lf\n",x,y);}
    db getw(){return atan2(y,x);}
    point getdel(){if (sign(x)==-1||(sign(x)==0&&sign(y)==-1)) return (*this)*(-1); else return (*this);}
    int getP() const{return sign(y)==1||(sign(y)==0&&sign(x)==-1);}
};
int inmid(point k1,point k2,point k3){return inmid(k1.x,k2.x,k3.x)&&inmid(k1.y,k2.y,k3.y);}
db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;}
db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;}
db rad(point k1,point k2){return atan2(cross(k1,k2),dot(k1,k2));}
// -pi -> pi
int compareangle (point k1,point k2){//极角排序+
    return k1.getP()<k2.getP()||(k1.getP()==k2.getP()&&sign(cross(k1,k2))>0);
}
point proj(point k1,point k2,point q){ // q 到直线 k1,k2 的投影
    point k=k2-k1;return k1+k*(dot(q-k1,k)/k.abs2());
}
point reflect(point k1,point k2,point q){return proj(k1,k2,q)*2-q;}
int clockwise(point k1,point k2,point k3){// k1 k2 k3 逆时针 1 顺时针 -1 否则 0
    return sign(cross(k2-k1,k3-k1));
}
int checkLL(point k1,point k2,point k3,point k4){// 求直线 (L) 线段 (S)k1,k2 和 k3,k4 的交点
    return cmp(cross(k3-k1,k4-k1),cross(k3-k2,k4-k2))!=0;
}
point getLL(point k1,point k2,point k3,point k4){
    db w1=cross(k1-k3,k4-k3),w2=cross(k4-k3,k2-k3); return (k1*w2+k2*w1)/(w1+w2);
}
int intersect(db l1,db r1,db l2,db r2){
    if (l1>r1) swap(l1,r1); if (l2>r2) swap(l2,r2); return cmp(r1,l2)!=-1&&cmp(r2,l1)!=-1;
}
int checkSS(point k1,point k2,point k3,point k4){
    return intersect(k1.x,k2.x,k3.x,k4.x)&&intersect(k1.y,k2.y,k3.y,k4.y)&&
           sign(cross(k3-k1,k4-k1))*sign(cross(k3-k2,k4-k2))<=0&&
           sign(cross(k1-k3,k2-k3))*sign(cross(k1-k4,k2-k4))<=0;
}
db disSP(point k1,point k2,point q){
    point k3=proj(k1,k2,q);
    if (inmid(k1,k2,k3)) return q.dis(k3); else return min(q.dis(k1),q.dis(k2));
}
db disSS(point k1,point k2,point k3,point k4){
    if (checkSS(k1,k2,k3,k4)) return 0;
    else return min(min(disSP(k1,k2,k3),disSP(k1,k2,k4)),min(disSP(k3,k4,k1),disSP(k3,k4,k2)));
}
int onS(point k1,point k2,point q){return inmid(k1,k2,q)&&sign(cross(k1-q,k2-k1))==0;}
struct circle{
    point o; db r;
    void scan(){o.scan(); scanf("%lf",&r);}
    int inside(point k){return cmp(r,o.dis(k));}
};
struct line{
    // p[0]->p[1]
    point p[2];
    line(point k1,point k2){p[0]=k1; p[1]=k2;}
    line(){}
    point& operator [] (int k){return p[k];}
    int include(point k){return sign(cross(p[1]-p[0],k-p[0]))>0;}
    point dir(){return p[1]-p[0];}
    line push(){ // 向外 ( 左手边 ) 平移 eps
        const db eps = 1e-6;
        point delta=(p[1]-p[0]).turn90().unit()*eps;
        return {p[0]-delta,p[1]-delta};
    }
};
point getLL(line k1,line k2){return getLL(k1[0],k1[1],k2[0],k2[1]);}
int parallel(line k1,line k2){return sign(cross(k1.dir(),k2.dir()))==0;}
int sameDir(line k1,line k2){return parallel(k1,k2)&&sign(dot(k1.dir(),k2.dir()))==1;}
int operator < (line k1,line k2){
    if (sameDir(k1,k2)) return k2.include(k1[0]);
    return compareangle(k1.dir(),k2.dir());
}
int checkpos(line k1,line k2,line k3){return k3.include(getLL(k1,k2));}
vector<line> getHL(vector<line> &L){ // 求半平面交 , 半平面是逆时针方向 , 输出按照逆时针
    sort(L.begin(),L.end()); deque<line> q;
    for (int i=0;i<(int)L.size();i++){
        if (i&&sameDir(L[i],L[i-1])) continue;
        while (q.size()>1&&!checkpos(q[q.size()-2],q[q.size()-1],L[i])) q.pop_back();
        while (q.size()>1&&!checkpos(q[1],q[0],L[i])) q.pop_front();
        q.push_back(L[i]);
    }
    while (q.size()>2&&!checkpos(q[q.size()-2],q[q.size()-1],q[0])) q.pop_back();
    while (q.size()>2&&!checkpos(q[1],q[0],q[q.size()-1])) q.pop_front();
    vector<line>ans; for (int i=0;i<q.size();i++) ans.push_back(q[i]);
    return ans;
}
vector<point> ConvexHull(vector<point>A,int flag=1){ // flag=0 不严格 flag=1 严格
    int n=A.size(); vector<point>ans(n*2);
    sort(A.begin(),A.end()); int now=-1;
    for (int i=0;i<A.size();i++){
        while (now>0&&sign(cross(ans[now]-ans[now-1],A[i]-ans[now-1]))<flag) now--;
        ans[++now]=A[i];
    } int pre=now;
    for (int i=n-2;i>=0;i--){
        while (now>pre&&sign(cross(ans[now]-ans[now-1],A[i]-ans[now-1]))<flag) now--;
        ans[++now]=A[i];
    } ans.resize(now); return ans;
}
bool cclock(vector<point> &p){
    int n = p.size();
    for(int i=1;i<n-1;i++){
        if(sign(cross(p[i]-p[i-1],p[i+1]-p[i-1]))>0)    return 0;
        else if(sign(cross(p[i]-p[i-1],p[i+1]-p[i-1]))<0){
            reverse(p.begin(),p.end());
            return 1;
        }
    }
    return 1;
}
//117-151 更可靠的半平面交板子 from matthew99 upd 9-29
bool on_left(line l,point p){
    return sign(cross(l.dir(),p-l[0]))>0;
}
inline bool half_plane(line *plane, const int &n_plane, point *poly, int &n_poly){
    static int pos[maxn + 5];
    static double ang[maxn + 5];
    for(int i=0;i<n_plane;i++) ang[i] = atan2(plane[i].dir().y, plane[i].dir().x), pos[i] = i;
    sort(pos, pos + n_plane, [&](int x, int y) { return ang[x] < ang[y]; });
    int head, rear;
    static point p[maxn + 5];
    static line q[maxn + 5];
    head = rear = 0;
    q[rear++] = plane[pos[0]];
    for(int i=0;i<n_plane;i++){
        while (head < rear - 1 && !on_left(plane[pos[i]], p[rear - 2])) --rear;
        while (head < rear - 1 && !on_left(plane[pos[i]], p[head])) ++head;
        q[rear++] = plane[pos[i]];
        if (abs(cross(q[rear - 1].dir() , q[rear - 2].dir())) < eps){
            --rear;
            if (on_left(q[rear - 1], plane[pos[i]][0])) q[rear - 1] = plane[pos[i]];
        }
        if (head + 1 < rear) p[rear - 2] = getLL(q[rear - 2], q[rear - 1]);
    }
    while (head < rear - 1 && !on_left(q[head], p[rear - 2])) --rear;
    if (abs(cross(q[head].dir() , q[rear - 1].dir())) < eps){
        if (on_left(q[rear - 1], q[head][0])) --rear;
        else ++head;
    }
    if (head + 1 >= rear) return 0;
    p[rear - 1] = getLL(q[rear - 1], q[head]);
    n_poly = 0;
    for(int i=head;i<rear;i++) poly[n_poly++] = p[i];
    return 1;
}

int relation(point p,line l){    //点和向量关系   1:左侧   2:右侧   3:在线上
    int c=sign(cross(p-l[0],l[1]-l[0]));
    if(c<0)    return 1;
    else if(c>0)    return 2;
    else    return 3;
}
line que[maxn];
int half_plane_intersection(line *L,int n){    //以逆时针方向 半平面交求多边形的核  ch表示凸包的顶点  返回顶点数 -1则表示不存在
    int head=0,tail=1;
    que[0]=L[0],que[1]=L[1];
    for(int i=2;i<n;i++){
        while(tail>head&&relation(getLL(que[tail],que[tail-1]),L[i])==2)   tail--;
        while(tail>head&&relation(getLL(que[head],que[head+1]),L[i])==2)   head++;
        que[++tail]=L[i];
    }
    while(tail>head&&relation(getLL(que[tail],que[tail-1]),que[head])==2)  tail--;
    while(tail>head&&relation(getLL(que[head],que[head+1]),que[tail])==2)  head++;
    for(int i=head;i<=tail;i++){
        int j=(i==tail? head: i+1);
        if(sign(cross(que[i][1]-que[i][0],que[j][1]-que[j][0]))<=0)
            return 0;
    }
    return 1;
}

int T,n,m,id[400005];
vector<point> p,q;
line l[400005];int cnt=0;point s[400005];int y;
bool check(db x){
    cnt=0;
    for(int i=0;i<n;i++){
        l[cnt++]=line(p[i]*x-q[id[i]],p[(i+1)%n]*x-q[id[i]]);
    }
    return half_plane_intersection(l,cnt);
}
void log(vector<point>&v){for(auto x:v)x.print();}
int main(){
    scanf("%d",&T);
    while (T--){
        scanf("%d",&n);p.resize(n);
        for(int i=0;i<n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
        cclock(p);n=p.size();
        scanf("%d",&m);q.resize(m);
        for(int i=0;i<m;i++)scanf("%lf%lf",&q[i].x,&q[i].y);
        if(m==1){
            printf("%.6f\n",0.0);
            continue;
        }
//      log(q);
        q=ConvexHull(q);m=q.size();
//      log(q);
//      log(q);
        db ans = 1e18;
        for(int cas=1;cas<=2;cas++){
            for(int i=0;i<n;i++)p[i]=p[i]*-1;
//           p=ConvexHull(p);
//          log(p);
            int j=0;
            for(int i=0;i<n;i++){
                while (sign(cross(p[(i+1)%n]-p[i],q[(j+1)%m]-q[j]))<0||sign(cross(p[(i+1)%n]-p[i],q[(j-1+m)%m]-q[j]))<0){
                    j=(j+1)%m;
                }
                id[i]=j;//这条边无限外移切到的点
                // cout<<i<<' '<<j<<endl;
            }
            db l=0,r=1e10;
            for(int i=0;i<=70;i++){
                db mid = (l+r)/2;
                if(check(mid))
                    r=mid;
                else
                    l=mid;
            }
            ans = min(ans,l);
        }
        printf("%.11f\n",ans);
    }
}
posted @ 2019-10-31 18:53  MXang  阅读(310)  评论(0编辑  收藏  举报