QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#497374#7734. Intersection over UnionlonelywolfTL 1ms3964kbC++237.7kb2024-07-29 03:24:432024-07-29 03:24:44

Judging History

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

  • [2024-07-29 03:24:44]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3964kb
  • [2024-07-29 03:24:43]
  • 提交

answer

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

mt19937 eng(time(0));

const db eps=1e-6;
const db pi=acos(-1);

int sgn(db k) {
    if (k > eps) return 1; 
    else if (k < -eps) return -1; 
    return 0;
}
// -1: < | 0: == | 1: >
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 * (const db &k1) const { return (point){x*k1,y*k1}; }
    point operator / (const 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 弧度 
    void operator -= (const point &k1) { x-=k1.x,y-=k1.y; }

    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
    void unit() {db w=abs(); 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);
}

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 
    
};
// 直线与直线交点
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));}
db area(auto &A) {
    db ans = 0;
    for (int i = 0; i < A.size(); i++) {
        ans += cross(A[i], A[(i + 1) % A.size()]);
    }
    return ans / 2;
}
db 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]);
    vector<point> a;
    for (int i = 0; i < q.size(); i++) {
        a.push_back(getLL(q[i], q[(i + 1) % q.size()]));
    }
    return area(a);
}


db rp = (sqrt(5) - 1) / 2;

vector<point> P(4);
vector<line> L(8);

db s;
db calc2 (db l, db d) {
    L[1] = {point{0, d}, point{-1, d}};
    L[3] = {point{0, -d}, point{1, -d}};
    L[5] = {point{l, 0}, point{l, 1}};
    L[7] = {point{-l, 0}, point{-l, -1}};

    db s1 = getHL(L);
    db s2 = l * d * 4;

    return s1 / (s + s2 - s1);
}

db calc (db d) {
    db lo = 0, hi = 1, lv = -1, hv = -1;
    for (int t = 1; t <= 20; t++) {
        db b = (hi - lo) * rp;
        db rm = lo + b;
        db lm = hi - b;
        
        if (lv == -1) {
            lv = calc2(lm, d);
        } 
        if (hv == -1) {
            hv = calc2(rm, d);
        }

        if (hv < lv) {
            hv = lv;
            lv = -1;
            hi = rm;
        } else {
            lv = hv;
            hv = -1;
            lo = lm;
        }
    } 
    return lv == -1 ? hv : lv; 
}


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

    cout << fixed << setprecision(10);

    int T = 10000;
    cin >> T;
    while (T--) {
        for (int i = 0; i < 4; i++) {
            cin >> P[i].x >> P[i].y;
            // P[i].x = eng() % 100;
            // P[i].y = eng() % 100;
        }

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

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

        point o = (P[0] + P[2]) * 0.5;
        for (int i = 0; i < 4; i++) {
            P[i] -= o;
            P[i].unit();
        }
        s = area(P);

        for (int i = 0; i < 4; i++) {
            L[i * 2] = {P[i], P[(i + 1) % 4]};
        }

        db lo = 0, hi = 1, lv = -1, hv = -1;
        for (int t = 1; t <= 20; t++) {
            db b = (hi - lo) * rp;
            db rm = lo + b;
            db lm = hi - b;
            
            if (lv == -1) {
                lv = calc(lm);
            } 
            if (hv == -1) {
                hv = calc(rm);
            }

            if (hv < lv) {
                hv = lv;
                lv = -1;
                hi = rm;
            } else {
                lv = hv;
                hv = -1;
                lo = lm;
            }
        } 
        db ans = lv == -1 ? hv : lv; 
        cout << ans << "\n";
    }

    return 0;
}  

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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.7071067804
1
0.6238432248

result:

ok 3 numbers

Test #2:

score: -100
Time Limit Exceeded

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.9929653227
0.3647450844
0.5908698150
0.6217704848
0.5788323830
1
0.4999999923
0.6852345794
0.4559617193
0.5002873938
0.9802958261
0.3260689162
0.5440373886
0.3804801071
0.9231183304
0.8925586891
0.6176803195
0.9456769594
0.4998688915
0.9999200947
0.6474214762
0.7778122045
0.4999999999
0.9560555462...

result: