QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#497312#7734. Intersection over UnionlonelywolfWA 920ms3924kbC++238.1kb2024-07-28 22:36:422024-07-28 22:36:42

Judging History

你现在查看的是最新测评结果

  • [2024-07-28 22:36:42]
  • 评测
  • 测评结果:WA
  • 用时:920ms
  • 内存:3924kb
  • [2024-07-28 22:36:42]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using db = double;

const db eps=1e-8;

int sgn(db k) {
    if (k > eps) return 1; 
    else if (k < -eps) return -1; 
    return 0;
}
int cmp(db k1, db k2) { return sgn(k1 - k2); }
// k3 in [k1, k2]
int inmid(db k1, db k2, db k3) { return sgn(k1-k3) * sgn(k2-k3) <= 0; }
// 点 (x, y)
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; }
    // 逆时针旋转 k1 弧度 
    point rotate(db k1) {return (point){x*cos(k1)-y*sin(k1),x*sin(k1)+y*cos(k1)};}
    // 逆时针旋转 90°
    point rotleft() { return (point){-y,x}; }
    // 优先比较 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; }
    // 与点 k1 的距离
    db dis(point k1) {return ((*this)-k1).abs();}
    // 化为单位向量, require: abs() > 0
    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);}
    // 方向角 atan2(y, x)
    db getw() {return atan2(y,x);} 
    // 将向量对称到 (-pi, pi] 半平面中
    point getdel() {if (sgn(x)==-1||(sgn(x)==0&&sgn(y)==-1)) return (*this)*(-1); else return (*this);}
    // (-pi, 0] -> 0, (0, pi] -> 1
    int getP() const {return sgn(y)==1||(sgn(y)==0&&sgn(x)==-1);}
};

/* 点与线段的位置关系及交点 */

// k3 在 矩形 [k1, k2] 中
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; }
// 从 k1 转到 k2 的方向角
db rad(point k1,point k2) {return atan2(cross(k1,k2),dot(k1,k2)); }
// k1 k2 k3 逆时针 1 顺时针 -1 否则 0  
int clockwise(point k1,point k2,point k3){
    return sgn(cross(k2-k1,k3-k1));
}
// 按 (-pi, pi] 顺序进行极角排序
int cmpangle (point k1,point k2){
    return k1.getP()< k2.getP()||(k1.getP()==k2.getP()&&sgn(cross(k1,k2))>0);
}
// 点 q 在线段 k1, k2 上
int onS(point k1,point k2,point q){return inmid(k1,k2,q)&&sgn(cross(k1-q,k2-k1))==0;}
// q 到直线 k1,k2 的投影 
point proj(point k1,point k2,point q) {
    point k=k2-k1; return k1+k*(dot(q-k1,k)/k.abs2());
}
// q 关于直线 k1,k2 的镜像 
point reflect(point k1,point k2,point q) {return proj(k1,k2,q)*2-q;}
// 判断 直线 (k1, k2) 和 直线 (k3, k4) 是否相交
int checkLL(point k1,point k2,point k3,point k4){
    return cmp(cross(k3-k1,k4-k1),cross(k3-k2,k4-k2))!=0;
}
// 求直线 (k1, k2) 和 直线 (k3, k4) 的交点 
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);
}


/* 直线与半平面交 */

// 直线 p[0] -> p[1]
struct line {
    point p[2];
    line() {}
    line(point k1, point k2) {p[0]=k1; p[1]=k2;}
    point& operator [] (int k) {return p[k];}
    // k 严格位于直线左侧 / 半平面 p[0] -> p[1]
    int include(point k){return sgn(cross(p[1]-p[0],k-p[0]))>0;}
    // 方向向量
    point dir() {return p[1]-p[0];}
    // 向左平移 d, 默认为 eps 
    line push(db d = eps) {
        point delta=(p[1]-p[0]).rotleft().unit()*d;
        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 sgn(cross(k1.dir(),k2.dir()))==0;}
// 平行且同向
int sameDir(line k1,line k2){return parallel(k1,k2)&&sgn(dot(k1.dir(),k2.dir()))==1;}
// 同向则左侧优先,否则按极角排序,用于半平面交
int operator < (line k1,line k2){
    if (sameDir(k1,k2)) return k2.include(k1[0]); 
    return cmpangle(k1.dir(),k2.dir());
}
// k3 (半平面) 包含 k1,k2 的交点, 用于半平面交
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;
}


db area(auto &A) {
    db ans = 0;
    for (int i = 0; i < A.size(); i++) {
        ans += cross(A[i], A[(i + 1) % A.size()]);
    }
    if (ans < 0) {
        ans = -ans;
    }
    return ans / 2;
}

void solve() {
    array<point, 4> P;
    for (auto &[x, y] : P) {
        cin >> x >> y;
    }

    if (P[0].x == P[1].x || P[0].y == P[1].y) {
        cout << 1 << "\n";
        return;
    }

    if (cross(P[1] - P[0], P[2] - P[0]) < 0) {
        reverse(P.begin(), P.end());
    }

    auto o = (P[0] + P[2]) / 2;
    for (auto &p : P) {
        p = p - o;
        p = p.unit();
    }
    db s = area(P);

    vector<line> L(8);
    for (int i = 0; i < 4; i++) {
        L[i] = {P[i], P[(i + 1) % 4]};
    }

    auto calc = [&] (db l, db d) {
        L[4] = line(point{0, d}, point{-1, d});
        L[5] = line(point{0, -d}, point{1, -d});
        L[6] = line(point{l, 0}, point{l, 1});
        L[7] = line(point{-l, 0}, point{-l, -1});

        auto v = getHL(L);

        vector<point> a(v.size());
        for (int i = 0; i < v.size(); i++) {
            a[i] = getLL(v[i], v[(i + 1) % v.size()]);
        }

        db s1 = area(a);
        db s2 = l * d * 4;
        return s1 / (s + s2 - s1);
    };

    db mx = 0, my = 0;
    for (auto &p : P) {
        mx = max(mx, p.x);
        my = max(my, p.y);
    }
    db lox = 0, hix = mx, loy = 0, hiy = my;
    while (hix - lox > eps && hiy - loy > eps) {
        db lx = (2 * lox + hix) / 3;
        db rx = (lox + 2 * hix) / 3;

        db ly = (2 * loy + hiy) / 3;
        db ry = (loy + 2 * hiy) / 3;

        vector<db> val {calc(lx, ly), calc(rx, ly), calc(lx, ry), calc(rx, ry)};
        int pos = -1;
        db ans = 1e18;
        for (int i = 0; i < 4; i++) {
            if (val[i] < ans) {
                ans = val[i];
                pos = i;
            }
        }
        if (pos == 0) {
            lox = lx;
            loy = ly;
        } 
        if (pos == 1) {
            hix = rx;
            loy = ly;
        }
        if (pos == 2) {
            lox = lx;
            hiy = ry;
        }
        if (pos == 3) {
            hix = rx;
            hiy = ry;
        }
    } 
    while (hix - lox > eps) {
        db lx = (2 * lox + hix) / 3;
        db rx = (lox + 2 * hix) / 3;

        if (calc(lx, loy) < calc(rx, loy)) {
            lox = lx;
        } else {
            hix = rx;
        }
    }
    while (hiy - loy > eps) {
        db ly = (2 * loy + hiy) / 3;
        db ry = (loy + 2 * hiy) / 3;

        if (calc(lox, ly) < calc(lox, ry)) {
            loy = ly;
        } else {
            hiy = ry;
        }
    }
    cout << calc((lox + hix) / 2, (loy + hiy) / 2) << "\n";
}

signed main () {  
    ios::sync_with_stdio(false);
    cin.tie(nullptr);  

    cout << fixed << setprecision(9);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}  

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3916kb

input:

3
0 2 2 0 0 -2 -2 0
7 -2 9 -2 9 2 7 2
7 13 11 10 5 2 1 5

output:

0.707106781
1
0.623843225

result:

ok 3 numbers

Test #2:

score: -100
Wrong Answer
time: 920ms
memory: 3924kb

input:

10000
-568767734 379152673 -565681345 -54946093 -131582579 -51859704 -134668968 382239062
-194884120 -559906233 -194884142 -158042604 -998611400 -158042648 -998611378 -559906277
459335966 -945199065 478030260 -934243779 450535683 -887326546 431841389 -898281832
-483567491 491964356 -523827401 408140...

output:

0.992965413
0.999999932
0.590869816
0.621770485
0.578832384
1
0.499999997
0.685234581
0.455961720
0.500287394
0.980296111
0.492043572
0.544037389
0.380475365
0.923118332
0.892558701
0.617680320
0.945676970
0.499874370
0.999954388
0.647421476
0.777812205
0.500000000
0.956055550
0.997373088
0.18079271...

result:

wrong answer 14th numbers differ - expected: '0.3804801', found: '0.3804754', error = '0.0000047'