QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#354783#5104. Guardians of the GalleryJerryTclWA 7ms4108kbC++206.3kb2024-03-16 00:39:212024-03-16 00:39:23

Judging History

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

  • [2024-03-16 00:39:23]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:4108kb
  • [2024-03-16 00:39:21]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
template<typename T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using MaxHeap = priority_queue<T, vector<T>, less<T>>;
constexpr double eps = 1e-9;
int dSgn(double x) { return x < -eps ? -1 : x > eps ? 1 : 0; }
int dCmp(double x, double y) { return dSgn(x - y);  }
template<int CloseR = 1, int CloseL = 1> int dIn(double x, double l, double r)
    { return (CloseL ? l - eps : l + eps) < x && x < (CloseR ? r + eps : r - eps); }
typedef struct Pnt { double x, y; } Vec;
using cPnt = const Pnt; using cVec = const Vec;
inline Vec operator + (cVec a, cVec b) { return {a.x + b.x, a.y + b.y}; }
inline Vec operator - (cVec a, cVec b) { return {a.x - b.x, a.y - b.y}; }
inline Vec operator * (cVec a, const double b) { return {a.x * b, a.y * b}; }
inline Vec operator / (cVec a, const double b) { return {a.x / b, a.y / b}; }
inline int operator == (cPnt a, cPnt b) { return !dCmp(a.x, b.x) && !dCmp(a.y, b.y); }
inline int operator != (cPnt a, cPnt b) { return dCmp(a.x, b.x) || dCmp(a.y, b.y); }
inline double Dot(cVec a, cVec b) { return a.x * b.x + a.y * b.y; }
inline double Cro(cVec a, cVec b) { return a.x * b.y - a.y * b.x; }
inline double Len2(cVec a) { return Dot(a, a); }
inline double Len(cVec a) { return sqrt(Len2(a)); }
inline istream &operator >> (istream &in, Pnt &p) { return in >> p.x >> p.y; }
inline ostream &operator << (ostream &ou, Pnt &p) { return ou << p.x << " " << p.y; }
template<int Strict = 0> inline int In(cPnt p, cPnt a, cPnt b) {
    if(Strict && (p == a || p == b)) return 0;
    cVec PA = a - p, PB = b - p;
    return !dSgn(Cro(PA, PB)) && dSgn(Dot(PA, PB)) <= 0;
}
struct Polygon : vector<Pnt> {
    Polygon(int n) : vector<Pnt>(n) {}
    int nxtp(int x) const { return x + 1 == (int)size() ? 0 : x + 1; }
    void getXs(vector<Pnt> &ret, cPnt u, cPnt v) const {
        for(int i = 0; i < (int)size(); ++i) {
            cPnt p = (*this)[i], q = (*this)[nxtp(i)];
            cVec UV = v - u, PU = u - p, PV = v - p, PQ = q - p;
            if(!dSgn(Cro(UV, PQ))) continue;
            const double area0 = Cro(PU, PQ), area1 = Cro(PV, PQ);
            cPnt C = u + UV * (area0 / (area0 - area1));
            if(C != p && C != q && In(C, p, q)) ret.push_back(C);
        }
    }
    int containLine(cPnt u, cPnt v) const {
        static vector<Pnt> buf; buf.clear(); getXs(buf, u, v);
        for(cPnt p : buf) if(In(p, u, v)) return 0;
        double fl = 0, fr = 1; const double len = Len(v - u);
        const auto maintain = [&](cPnt p, cPnt q) {
            const auto &calc = [&](cPnt p) { return Len(p - u) / len; };
            double x = calc(p), y = calc(q); if(x > y) ::swap(x, y);
            if(dCmp(x, fl) != 1) fl = max(fl, y); else fr = min(fr, x);
        };
        for(int i = 0; i < (int)size(); ++i) {
            cPnt p = (*this)[i], z = (*this)[nxtp(i)];
            if(In<1>(p, u, v) || !In<1>(z, u, v)) continue;
            for(int j = nxtp(i);; j = nxtp(j)) {
                cPnt q = (*this)[nxtp(j)]; if(In<1>(q, u, v)) continue;
                const int x = dSgn(Cro(p - u, v - u));
                const int y = dSgn(Cro(q - u, v - u)); if(x == -y) return !x;
                maintain(x ? z : p, y ? (*this)[j] : q); break;
            }
        }
        const double f = (fl + fr) * .5;
        cPnt m = u * (1 - f) + v * f; int result = 0;
        for(int i = 0; i < (int)size(); ++i) {
            cPnt p = (*this)[i], q = (*this)[nxtp(i)];
            if(In(m, p, q)) return 1;
            if(p.y < q.y && dIn<0>(m.y, p.y, q.y))
                result += dSgn(Cro(q - p, m - p)) == 1;
            if(p.y > q.y && dIn<0>(m.y, q.y, p.y))
                result += dSgn(Cro(p - q, m - q)) == 1;
        }
        return result & 1;
    }
};
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n; cin >> n; Polygon pg(n); Pnt goal;
    for(int i = 0; i < n; ++i) cin >> pg[i];
    vector<pair<Pnt, double>> keys(n + 1);
    cin >> keys[0].fi >> goal, keys[0].se = 1e18;
    for(int i = 1; i <= n; ++i) keys[i] = {pg[i - 1], 1e18};
    const auto &check = [&](cPnt x) {
        int l = 0, r = 0;
        for(int i = 0; i < (int)pg.size(); ++i) {
            cPnt p = pg[i], q = pg[pg.nxtp(i)];
            if(In(p, x, goal) && x != p) {
                const int w = dSgn(Cro(x - goal, q - goal));
                if(w) { w == 1 ? ++l : ++r; if(l && r) return 0; }
            }
            if(In(q, x, goal) && x != q) {
                const int w = dSgn(Cro(x - goal, p - goal));
                if(w) { w == 1 ? ++l : ++r; if(l && r) return 0; }
            }
        }
        return 1;
    };
    const auto &checkpnt = [&](cPnt p) {
        return pg.containLine(p, goal) && check(p); };
    for(int i = 0; i <= n; ++i) if(checkpnt(keys[i].fi)) {
        static vector<Pnt> buf; keys[i].se = 0;
        buf.clear(), pg.getXs(buf, keys[i].fi, goal);
        pair<double, int> mn = {1e18, -1};
        for(int j = 0; j < (int)buf.size(); ++j) {
            double l = buf[j].x, r = goal.x; if(l > r) swap(l, r);
            if(dIn(keys[i].fi.x, l, r)) mn = \
                min(mn, {Len2(keys[i].fi - buf[j]), j});
        }
        if(mn.se != -1 && check(buf[mn.se])) keys.emplace_back(buf[mn.se], 0);
    }
    const int z = (int)keys.size();
    for(int i = 0; i < z; ++i) if(keys[i].se == 0) {
        cPnt P = keys[i].fi; cVec OP = P - goal;
        const double len = Len(OP), len2 = Len2(OP);
        for(int j = 0; j < z; ++j) {
            cPnt Q = keys[j].fi; cVec OQ = Q - goal;
            const double fr = Dot(OP, OQ) / len2;
            if(!dIn(fr, 0, 1)) continue;
            if(pg.containLine(Q, goal + OP * fr))
                keys[j].se = min(keys[j].se, abs(Cro(OP, OQ)) / len);
        }
    }
    MinHeap<pair<double, int>> Q;
    for(int i = 0; i < z; ++i) Q.emplace(keys[i].se, i);
    while(!Q.empty() && Q.top().se) {
        const auto [d, u] = Q.top(); Q.pop();
        if(keys[u].se != d) continue;
        for(int v = 0; v < z; ++v) {
            const double len = Len(keys[u].fi - keys[v].fi);
            if(d + len < keys[v].se && pg.containLine( \
                keys[u].fi, keys[v].fi)) Q.emplace(keys[v].se = d + len, v);
        }
    }
    printf("%.13lf", keys[0].se);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

15
13 7
20 20
39 20
49 7
73 13
100 5
117 38
98 20
80 20
66 40
68 20
51 20
41 39
22 48
2 39
10 20
104 20

output:

29.0000000000000

result:

ok found '29.0000000', expected '29.0000000', error '0.0000000'

Test #2:

score: 0
Accepted
time: 1ms
memory: 4108kb

input:

16
39 2
48 22
39 41
20 51
20 68
40 66
20 80
20 98
38 117
5 100
13 73
7 49
19 39
20 23
20 20
7 13
20 10
20 104

output:

13.0000000000000

result:

ok found '13.0000000', expected '13.0000000', error '0.0000000'

Test #3:

score: 0
Accepted
time: 1ms
memory: 4024kb

input:

16
13 33
20 60
23 66
39 97
49 105
73 166
100 205
117 272
98 216
80 180
66 172
68 156
51 122
41 121
22 92
2 44
10 40
104 228

output:

140.8722825824867

result:

ok found '140.8722826', expected '140.8722826', error '0.0000000'

Test #4:

score: 0
Accepted
time: 1ms
memory: 4108kb

input:

16
64 17
50 28
67 23
65 18
77 4
88 20
78 10
70 29
61 28
47 32
54 17
43 13
35 20
41 30
27 20
42 6
81 12
33 23

output:

64.2045377025227

result:

ok found '64.2045377', expected '64.2045377', error '0.0000000'

Test #5:

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

input:

16
64 17
50 28
67 23
65 18
77 4
88 20
78 10
70 29
61 28
47 32
54 17
43 13
35 20
41 30
27 20
42 6
33 23
81 12

output:

72.2834980411767

result:

ok found '72.2834980', expected '72.2834980', error '0.0000000'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3968kb

input:

7
76 8
389 215
691 19
407 331
489 397
300 403
363 334
126 60
393 370

output:

6.6579177565106

result:

ok found '6.6579178', expected '6.6579178', error '0.0000000'

Test #7:

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

input:

3
0 1000
1000 0
1000 1000
567 578
589 601

output:

0.0000000000000

result:

ok found '0.0000000', expected '0.0000000', error '-0.0000000'

Test #8:

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

input:

3
0 1000
0 0
1000 0
366 366
367 366

output:

0.0000000000000

result:

ok found '0.0000000', expected '0.0000000', error '-0.0000000'

Test #9:

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

input:

5
50 93
278 178
199 300
596 362
208 519
421 388
142 153

output:

175.1697593917353

result:

ok found '175.1697594', expected '175.1697594', error '0.0000000'

Test #10:

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

input:

7
50 93
278 178
199 300
401 312
483 162
596 362
208 519
488 252
142 153

output:

289.6821398768899

result:

ok found '289.6821399', expected '289.6821399', error '0.0000000'

Test #11:

score: 0
Accepted
time: 1ms
memory: 3992kb

input:

8
10 10
40 25
20 25
20 35
12 23
30 23
10 20
5 40
15 15
19 26

output:

25.0000000000000

result:

ok found '25.0000000', expected '25.0000000', error '0.0000000'

Test #12:

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

input:

9
5 1000
6 3
5 999
0 1000
0 0
500 2
500 0
1000 0
1000 1000
1 4
993 1

output:

5.1010479069859

result:

ok found '5.1010479', expected '5.1010479', error '0.0000000'

Test #13:

score: 0
Accepted
time: 6ms
memory: 3836kb

input:

100
695 43
538 87
463 208
597 329
750 306
812 481
960 555
912 344
983 450
987 573
994 852
941 985
801 855
792 800
849 806
792 696
924 701
939 672
710 546
722 668
723 807
715 767
624 524
634 554
547 503
357 352
627 458
651 495
937 558
932 545
864 509
753 489
509 397
341 335
300 495
199 528
380 688
48...

output:

1695.1865730236420

result:

ok found '1695.1865730', expected '1695.1865730', error '0.0000000'

Test #14:

score: 0
Accepted
time: 1ms
memory: 3824kb

input:

20
840 854
839 45
996 905
959 938
852 938
730 423
425 493
136 481
213 778
527 740
691 941
22 830
83 313
462 155
636 21
462 321
360 324
238 422
402 492
806 406
952 822
410 838

output:

1424.3842014548488

result:

ok found '1424.3842015', expected '1424.3842015', error '0.0000000'

Test #15:

score: 0
Accepted
time: 4ms
memory: 4036kb

input:

74
89 395
52 622
124 832
115 698
95 598
199 491
190 356
191 398
132 315
94 371
34 221
91 0
153 139
220 465
260 283
312 30
409 15
338 50
343 52
437 69
359 89
332 213
377 505
375 396
405 199
657 90
658 50
360 50
618 23
642 7
824 191
688 417
795 227
709 286
662 321
646 175
485 210
381 357
420 329
441 3...

output:

2036.7557098766374

result:

ok found '2036.7557099', expected '2036.7557099', error '0.0000000'

Test #16:

score: 0
Accepted
time: 4ms
memory: 3780kb

input:

100
380 626
511 639
548 551
651 476
706 462
636 604
652 617
776 577
794 566
821 433
765 410
778 276
735 345
700 329
448 550
283 482
537 332
706 213
741 204
833 152
657 182
626 173
568 225
602 213
673 203
537 286
459 317
609 261
493 344
334 430
468 338
331 400
350 326
512 197
553 155
424 120
446 179
...

output:

307.8507108572825

result:

ok found '307.8507109', expected '307.8507109', error '0.0000000'

Test #17:

score: 0
Accepted
time: 7ms
memory: 3944kb

input:

100
425 641
614 667
719 714
598 761
548 727
505 713
415 832
505 856
724 762
764 767
803 755
773 727
826 633
832 509
842 570
829 456
742 430
706 513
604 527
942 208
912 569
959 330
975 605
977 878
882 609
844 694
869 789
930 896
992 894
763 937
699 930
701 854
732 810
709 820
657 881
507 896
342 805
...

output:

1941.5687357268885

result:

ok found '1941.5687357', expected '1941.5687357', error '0.0000000'

Test #18:

score: 0
Accepted
time: 7ms
memory: 3980kb

input:

100
845 528
842 889
837 997
809 663
786 746
793 554
782 470
769 798
709 992
520 993
95 983
191 897
250 666
136 715
139 745
32 979
32 918
5 916
0 740
31 283
10 238
36 177
102 740
141 635
145 353
132 435
106 607
130 383
41 66
139 12
403 11
330 45
225 48
153 216
251 342
233 374
289 424
266 99
334 62
34...

output:

1863.5717402634057

result:

ok found '1863.5717403', expected '1863.5717403', error '0.0000000'

Test #19:

score: 0
Accepted
time: 1ms
memory: 3932kb

input:

4
0 0
1000 0
1000 1000
0 1000
4 939
27 58

output:

0.0000000000000

result:

ok found '0.0000000', expected '0.0000000', error '-0.0000000'

Test #20:

score: 0
Accepted
time: 6ms
memory: 3952kb

input:

94
5 5
995 5
995 995
5 995
990 990
5 990
970 970
5 970
950 950
5 950
930 930
5 930
910 910
5 910
890 890
5 890
870 870
5 870
850 850
5 850
830 830
5 830
810 810
5 810
790 790
5 790
770 770
5 770
750 750
5 750
730 730
5 730
710 710
5 710
690 690
5 690
670 670
5 670
650 650
5 650
630 630
5 630
610 610...

output:

620.2910607126303

result:

ok found '620.2910607', expected '620.2910607', error '0.0000000'

Test #21:

score: 0
Accepted
time: 1ms
memory: 3824kb

input:

8
0 0
20 0
20 30
60 30
60 0
80 0
80 50
0 50
70 30
70 10

output:

0.0000000000000

result:

ok found '0.0000000', expected '0.0000000', error '-0.0000000'

Test #22:

score: -100
Wrong Answer
time: 1ms
memory: 4000kb

input:

5
2 0
10 0
0 10
0 3
5 3
1 8
5 2

output:

6.4031242374328

result:

wrong answer 1st numbers differ - expected: '5.0000000', found: '6.4031242', error = '0.2806248'