QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#345528#7734. Intersection over UnionSortingWA 0ms3812kbC++207.7kb2024-03-07 05:16:102024-03-07 05:16:10

Judging History

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

  • [2024-03-07 05:16:10]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3812kb
  • [2024-03-07 05:16:10]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cassert>
#include <bitset>
#include <stack>
#include <utility>
#include <iomanip>

using namespace std;
typedef double ld;

template <class T> int sgn(T x) { return (x > 0) - (x < 0); }
template<class T>
struct Point {
	typedef Point P;
	T x, y;
	explicit Point(T x=0, T y=0) : x(x), y(y) {}
	bool operator<(P p) const { return tie(x,y) < tie(p.x,p.y); }
	bool operator==(P p) const { return tie(x,y)==tie(p.x,p.y); }
	P operator+(P p) const { return P(x+p.x, y+p.y); }
	P operator-(P p) const { return P(x-p.x, y-p.y); }
	P operator*(T d) const { return P(x*d, y*d); }
	P operator/(T d) const { return P(x/d, y/d); }
	T dot(P p) const { return x*p.x + y*p.y; }
	T cross(P p) const { return x*p.y - y*p.x; }
	T cross(P a, P b) const { return (a-*this).cross(b-*this); }
	T dist2() const { return x*x + y*y; }
	double dist() const { return sqrt((double)dist2()); }
	// angle to x-axis in interval [-pi, pi]
	double angle() const { return atan2(y, x); }
	P unit() const { return *this/dist(); } // makes dist()=1
	P perp() const { return P(-y, x); } // rotates +90 degrees
	P normal() const { return perp().unit(); }
	// returns point rotated 'a' radians ccw around the origin
	P rotate(double a) const {
		return P(x*cos(a)-y*sin(a),x*sin(a)+y*cos(a)); }
	friend ostream& operator<<(ostream& os, P p) {
		return os << "(" << p.x << "," << p.y << ")"; }
};
typedef Point<ld> P;
template<class T>
T polygonArea2(const vector<Point<T>> &v) {
	T a = v.back().cross(v[0]);
	for(int i = 0; i < (int)v.size() - 1; ++i) a += v[i].cross(v[i+1]);
	return a;
}

pair<int, P> lineInter(P s1, P e1, P s2, P e2) {
	auto d = (e1 - s1).cross(e2 - s2);
	if (d == 0) // if parallel
		return {-(s1.cross(e1, s2) == 0), P(0, 0)};
	auto p = s2.cross(e1, e2), q = s2.cross(e2, s1);
	return {1, (s1 * p + e1 * q) / d};
}

vector<P> polygonCut(const vector<P>& poly, P s, P e) {
	vector<P> res;
	for(int i = 0; i < poly.size(); ++i){
		P cur = poly[i], prev = i ? poly[i-1] : poly.back();
		bool side = s.cross(e, cur) < 0;
		if (side != (s.cross(e, prev) < 0))
			res.push_back(lineInter(s, e, cur, prev).second);
		if (side)
			res.push_back(cur);
	}
	return res;
}

const ld EPS = 1e-9;
P p[4];
P center;
ld area;
vector<P> v;

template<class P>
bool inPolygon(vector<P> &p, P a, bool strict = true) {
	int cnt = 0, n = (int)p.size();
    for(int i = 0; i < n; ++i){
		P q = p[(i + 1) % n];
		// if (onSegment(p[i], q, a)) return !strict;
		//or: if (segDist(p[i], q, a) <= eps) return !strict;
		cnt ^= ((a.y<p[i].y) - (a.y<q.y)) * a.cross(p[i], q) > 0;
	}
	return cnt;
}

pair<ld, ld> interm(ld x, ld y){
    // cout << "calc " << x << " " << y << endl;
    vector<P> our;
    our.push_back(P{center.x - x, center.y - y});
    our.push_back(P{center.x + x, center.y - y});
    our.push_back(P{center.x + x, center.y + y});
    our.push_back(P{center.x - x, center.y + y});

    // cout << center.x << " " << center.y << "\n";

    for(int i = 0; i < 4; ++i){
        // cout << i << " i" << endl;
        int prv = (i + 4 - 1) % 4;
        our = polygonCut(our, v[i], v[prv]);
        // cout << our.size() << " size here" << endl;
    }
    // cout << our.size() << " size" << endl;

    ld inters_area = abs(polygonArea2(our)) / 2;
    ld added_area = 4 * x * y - inters_area;
    
    return {inters_area, added_area};
}

ld calc(ld x, ld y){
    auto [inters_area, added_area] = interm(x, y);
    return inters_area / (added_area + area);   
}

ld get_best(ld x, vector<P> pts){
    sort(pts.begin(), pts.end(), [&](auto l, auto r){
        if(l.y != r.y) return l.y < r.y;
        return l.x < r.x;
    });

    ld ans = 0;
    for(int i = 0; i < pts.size(); ++i){
        if(pts[i].y > center.y + EPS){
            ans = max(ans, calc(x, pts[i].y));
        }
    }

    ld prev_len = 2 * x;
    ld prev_y = center.y;
    for(int i = 0; i < pts.size(); ++i){
        if(abs(pts[i].y - center.y) < EPS) continue;
        if(i != (int)pts.size() - 1 && abs(pts[i].y - pts[i + 1].y) < EPS) continue;
        assert(i >= 2);
        auto [inters, add] = interm(x, pts[i - 2].y - center.y);
        ans = max(ans, inters / (add + area));

        if(abs(pts[i - 1].y - pts[i].y) > EPS){
            // cout << i << " i " << pts.size()  << endl;
            continue;
        }

        vector<P> bounds{pts[i - 1], pts[i]};
        ld len = abs(pts[i].x - pts[i - 1].x);
        ld total = 2 * x;
        

        if(len / total > inters / (area + add)){
            prev_len = len;
            prev_y = pts[i].y;
            continue;
        }

        ld u = inters;
        ld d = area + add;
        ld A = (len - prev_len) / 2;
        ld B = 2 * x - prev_len;

        ld t = (u * B - prev_len * d) / (A * (d + u));

        ld perfect_y = prev_y * ((ld)1 - t) + pts[i].y * t;
        return max(ans, calc(x, perfect_y));
    }
    return ans;
}

pair<P, P> points_on_line(ld x, ld y){
    pair<P, P> bounds;
    bounds.first = P{center.x - x, y};
    bounds.second = P{center.x + x, y};
    for(int i = 0; i < 4; ++i){
        int nxt = (i + 1) % 4;
        auto [yes, pt] = lineInter(P{center.x, y}, P{center.x + 1, y}, v[i], v[nxt]);
        if(yes){
            if(pt.x < center.x)
            bounds.first = max(bounds.first, pt);
            else
            bounds.second = min(bounds.second, pt);
        }
    }
    return bounds;
}

ld search_y(ld x){
    vector<P> pts;
    for(int i = 0; i < 4; ++i){
        int nxt = (i + 1) % 4;
        auto [yes, pt] = lineInter(center - P{(ld)1, (ld)0}, center + P{(ld)1, (ld)0}, v[i], v[nxt]);
        if(yes){
            pts.push_back(pt);
        }
    }
    sort(pts.begin(), pts.end());
    if(pts.empty()) return (ld)0;

    P lft = center - P{x, (ld)0};
    P rght = center + P{x, (ld)0};

    pts[0] = max(pts[0], lft);
    pts[1] = min(pts[1], rght);

    for(int i = 0; i < 4; ++i){
        int nxt = (i + 1) % 4;
        auto [yes, pt] = lineInter(lft, lft + P{0, 1}, v[i], v[nxt]);
        if(yes && pt.y > center.y){
            auto [p1, p2] = points_on_line(x, pt.y);
            pts.push_back(p1);
            pts.push_back(p2);
        }
    }

    for(int i = 0; i < 4; ++i){
        int nxt = (i + 1) % 4;
        auto [yes, pt] = lineInter(rght, rght + P{0, 1}, v[i], v[nxt]);
        if(yes && pt.y > center.y){
            auto [p1, p2] = points_on_line(x, pt.y);
            pts.push_back(p1);
            pts.push_back(p2);
        }
    }

    for(int i = 0; i < 4; ++i){
        if(center.x - x < v[i].x && v[i].x < center.x + x && v[i].y > center.y){
            pts.push_back(v[i]);
        }
    }

    return get_best(x, pts);
}

ld search_x(){
    ld l = 0, r = 2e9;
    while((r - l) > EPS){
        ld mid1 = (1.1 * l + r) / 2.1;
        ld mid2 = (l + 1.1 * r) / 2.1;

        if(search_y(mid1) > search_y(mid2)){
            r = mid2;
        }
        else{
            l = mid1;
        }
    }
    return search_y(l);
}

void solve(){
    for(int i = 0; i < 4; ++i){
        int x, y;
        cin >> x >> y;
        p[i] = P{(ld)x, (ld)y};
    }

    v = vector<P>(p, p + 4);

    area = polygonArea2(v);
    if(area < 0){
        reverse(p, p + 4);
        reverse(v.begin(), v.end());
        area = -area;
    }
    area /= 2;

    center = (p[0] + p[1] + p[2] + p[3]) / 4;
    cout << fixed << setprecision(12) <<  search_x() << "\n";
}

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

    int t;
    cin >> t;

    while(t--){
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3812kb

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.624960540538
0.999999999671
0.609094889795

result:

wrong answer 1st numbers differ - expected: '0.7071068', found: '0.6249605', error = '0.0821462'