QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#345538#7734. Intersection over UnionSortingWA 2ms3848kbC++204.8kb2024-03-07 05:43:132024-03-07 05:43:14

Judging History

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

  • [2024-03-07 05:43:14]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3848kb
  • [2024-03-07 05:43:13]
  • 提交

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;
}

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

void polygonCut(const vector<P>& poly, P s, P e, vector<P> &res) {
    res.clear();
	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);
	}
}

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

ld calc(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";

    vector<P> swp;
    for(int i = 0; i < 4; ++i){
        // cout << i << " i" << endl;
        int prv = (i + 4 - 1) % 4;
        polygonCut(our, v[i], v[prv], swp);
        swap(swp, our);
        // 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 + area);
}

ld min_x, max_x, min_y, max_y;

ld search_y(ld x){
    ld r = (sqrt((ld)5) - 1) / 2;
    ld a = 0, b = (max_y - min_y) / 2;
    ld x1 = b - r*(b-a), x2 = a + r*(b-a);
	ld f1 = calc(x, x1), f2 = calc(x, x2);
    while((b - a) > EPS){
        if(f1 > f2){
        	b = x2; x2 = x1; f2 = f1;
			x1 = b - r*(b-a); f1 = calc(x, x1);
		} else {
			a = x1; x1 = x2; f1 = f2;
			x2 = a + r*(b-a); f2 = calc(x, x2);
		}
    }
    return calc(x, a);
}

ld search_x(){
    ld r = (sqrt((ld)5) - 1) / 2;
    ld a = 0, b = (max_x - min_x) / 2;
    ld x1 = b - r*(b-a), x2 = a + r*(b-a);
	ld f1 = search_y(x1), f2 = search_y(x2);
    while((b - a) > EPS){
        if(f1 > f2){
        	b = x2; x2 = x1; f2 = f1;
			x1 = b - r*(b-a); f1 = search_y(x1);
		} else {
			a = x1; x1 = x2; f1 = f2;
			x2 = a + r*(b-a); f2 = search_y(x2);
		}
    }
    return search_y(a);
}

void solve(){
    min_x = 2e9, max_x = -2e9;
    min_y = 2e9, max_y = -2e9;
    for(int i = 0; i < 4; ++i){
        int x, y;
        cin >> x >> y;
        p[i] = P{(ld)x, (ld)y};

        min_x = min(min_x, (ld)x);
        max_x = max(max_x, (ld)x);

        min_y = min(min_y, (ld)y);
        max_y = max(max_y, (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: 2ms
memory: 3848kb

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.707106781187
0.999999998968
0.623843224831

result:

wrong answer 2nd numbers differ - expected: '1.0000000', found: '1.0000000', error = '0.0000000'