QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#570060#8082. Minimum Euclidean DistancelongyinWA 280ms3988kbC++202.6kb2024-09-17 13:37:382024-09-17 13:37:39

Judging History

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

  • [2024-09-17 13:37:39]
  • 评测
  • 测评结果:WA
  • 用时:280ms
  • 内存:3988kb
  • [2024-09-17 13:37:38]
  • 提交

answer

#include <bits/stdc++.h>
//#define int long long
#define endl "\n"
using namespace std;

using ll = long long;
const int INF = 1e9;
const int N = 5e3 + 5;
const double eps = 1e-9;

struct Point {
    double x, y;
    using Vec = Point;

    Point operator + (const Point& p) {
        return {x + p.x, y + p.y};
    }

    Point operator - (const Point& p) {
        return {x - p.x, y - p.y};
    }

    Point operator * (const double k) {
        return {x * k, y * k};
    }

    Point operator / (const int k) {
        return {x / k, y / k};
    }

    double dot(const Point& p) {
        return x * p.x + y * p.y;
    }

    double cross(const Vec& p) {
        return x * p.y - y * p.x;
    }

    double dis(const Point& p) {
        return sqrt(dis2(p));
    }

    double dis2(const Point& p) {
        return pow(x - p.x, 2) + pow(y - p.y, 2);
    }

    double len() {
        return sqrt(len2());
    }

    double len2() {
        return pow(x, 2) + pow(y, 2);
    }

    void show() {
        cout << x << " " << y << endl;
    }
} points[N];
using Vec = Point;

double pToSegDis(Point& p, Point& a, Point& b) {
    Point s;
    if (a.x == b.x) {
        s = {a.x - p.x, 0};
    }
    else if (a.y == b.y) {
        s = {0, a.y - p.y};
    }
    else {
        double k = -1 / ((a.y - b.y) / (a.x - b.x));
        double d = abs((b - a).cross(p - a)) / a.dis(b);
        double m = sqrt(1 + k * k);
        double n = d / m;   
        s = Point({1, k}) * n;
    }
    Point h = p + s;
    double res = (b - a).dot(h - a) / a.dis2(b);

    if (res <= -eps)
        return p.dis2(a);
    else if (res >= 1 + eps)
        return p.dis2(b);
    else 
        return p.dis2(h);
}

int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
        cin >> points[i].x >> points[i].y;
    }
    points[n + 1] = points[1];

    for (int i = 1; i <= q; i++) {
        Point p, q;
        cin >> p.x >> p.y >> q.x >> q.y;
        Point o = (p + q) / 2;
        double r = p.dis(q) / 2;
        double res = INF;
        bool flag = false;
        for (int j = 1; j <= n; j++) {
            if ((points[j + 1] - points[j]).cross(o - points[j]) <= eps) {
                flag = true;
            }
            double d = pToSegDis(o, points[j], points[j + 1]);
            res = min(res, d);
        }
        if (!flag)
            res = 0;
        res += r * r / 2;
        cout << fixed << setprecision(10) << res << endl;
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3676kb

input:

4 3
0 0
1 0
1 1
0 1
0 0 1 1
1 1 2 2
1 1 2 3

output:

0.2500000000
0.7500000000
1.8750000000

result:

ok Your answer is acceptable!^ ^

Test #2:

score: 0
Accepted
time: 0ms
memory: 3852kb

input:

48 10
-30 0
-29 -4
-28 -7
-27 -9
-25 -12
-22 -16
-21 -17
-17 -20
-14 -22
-12 -23
-9 -24
-5 -25
-4 -25
0 -24
3 -23
5 -22
8 -20
12 -17
13 -16
16 -12
18 -9
19 -7
20 -4
21 0
21 1
20 5
19 8
18 10
16 13
13 17
12 18
8 21
5 23
3 24
0 25
-4 26
-5 26
-9 25
-12 24
-14 23
-17 21
-21 18
-22 17
-25 13
-27 10
-28 ...

output:

589.5000000000
51.4705882353
1051.2500000000
66.6250000000
174.1250000000
562.6750000000
272.3942307692
287.3850000000
689.6250000000
436.2500000000

result:

ok Your answer is acceptable!^ ^

Test #3:

score: -100
Wrong Answer
time: 280ms
memory: 3988kb

input:

5000 5000
-50000000 0
-49999885 -49450
-49999770 -85675
-49999604 -122394
-49999391 -157604
-49999130 -192731
-49998803 -229143
-49998399 -267196
-49997956 -303872
-49997469 -339362
-49996891 -377221
-49996257 -414903
-49995577 -451819
-49994843 -488600
-49994059 -524941
-49993173 -563137
-49992252 ...

output:

1000001134.6250000000
391704061645588.1250000000
3372744384592591.0000000000
1761094292653339.0000000000
797203335904044.7500000000
407842688547780.0000000000
3194363161448624.0000000000
1619744446324385.0000000000
363457485421825.1875000000
1771731086931932.7500000000
180657347288378.6562500000
767...

result:

wrong answer Except 2214785369560633.000000000000, but found 1000001134.625000000000!QAQ