답안 #386587

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
386587 2021-04-07T03:02:35 Z timmyfeng 함박 스테이크 (JOI20_hamburg) C++17
100 / 100
888 ms 14616 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 200000, X = 1000000000;

vector<array<int, 2>> skewers;
array<int, 4> rects[N];
bool tested[N];
int n;

array<int, 4> bounds() {
    int x1 = X, x2 = 1, y1 = X, y2 = 1;
    for (int i = 0; i < n; ++i) {
        if (!tested[i]) {
            auto [x3, x4, y3, y4] = rects[i];
            x1 = min(x1, x4);
            x2 = max(x2, x3);
            y1 = min(y1, y4);
            y2 = max(y2, y3);
        }
    }
    return {x1, x2, y1, y2};
}

void print() {
    for (auto [x, y] : skewers) {
        cout << x << " " << y << "\n";
    }
    exit(0);
}

void solve(int k) {
    if (k == 0) {
        if (count(tested, tested + n, true) == n) {
            print();
        }
        return;
    }

    auto [x1, x2, y1, y2] = bounds();

    for (auto x : {x1, x2}) {
        for (auto y : {y1, y2}) {
            skewers.push_back({x, y});
            vector<int> undo;
            for (int i = 0; i < n; ++i) {
                if (!tested[i]) {
                    auto [x3, x4, y3, y4] = rects[i];
                    if (x3 <= x && x <= x4 && y3 <= y && y <= y4) {
                        tested[i] = true;
                        undo.push_back(i);
                    }
                }
            }

            solve(k - 1);

            for (auto i : undo) {
                tested[i] = false;
            }
            skewers.pop_back();
        }
    }
}

void build(vector<array<int, 2>> &points) {
    sort(rbegin(points), rend(points));
    vector<array<int, 2>> hull;
    for (auto [x, y] : points) {
        if (hull.empty() || hull.back()[1] > y) {
            hull.push_back({x, y});
        }
    }
    reverse(begin(hull), end(hull));
    points = hull;
}

int query(vector<array<int, 2>> &hull, int x) {
    auto it = upper_bound(begin(hull), end(hull), array<int, 2>{x, X});
    return it == end(hull) ? X : it->at(1);
}

void solve_4() {
    auto [x1, x2, y1, y2] = bounds();

    int l1 = y1, r1 = y1, l2 = y2, r2 = y2, d1 = x1, u1 = x1, d2 = x2, u2 = x2;
    vector<array<int, 2>> ld, lu, dr, ur, du, lrud, lruu, lrdd, lrdu;

    for (int i = 0; i < n; ++i) {
        auto [x3, x4, y3, y4] = rects[i];
        bool l = x3 <= x1, r = x4 >= x2, d = y3 <= y1, u = y4 >= y2;
        if (l + r + d + u <= 2) {
            if (l && r) {
                lrud.push_back({y3, -y3});
                lruu.push_back({y3, y4});
                lrdd.push_back({-y4, -y3});
                lrdu.push_back({-y4, y4});
            } else if (d && u) {
                du.push_back({x3, x4});
                u1 = max(u1, x3);
                d2 = min(d2, x4);
            } else if (l && d) {
                ld.push_back({-y4, x4});
            } else if (l && u) {
                lu.push_back({y3, x4});
            } else if (d && r) {
                dr.push_back({x3, y4});
            } else if (u && r) {
                ur.push_back({x3, -y3});
            } else if (l) {
                l1 = max(l1, y3);
                l2 = min(l2, y4);
            } else if (r) {
                r1 = max(r1, y3);
                r2 = min(r2, y4);
            } else if (d) {
                d1 = max(d1, x3);
                d2 = min(d2, x4);
            } else if (u) {
                u1 = max(u1, x3);
                u2 = min(u2, x4);
            }
        }
    }

    build(ld), build(lu), build(dr), build(ur), build(du);
    build(lrud), build(lruu), build(lrdd), build(lrdu);

    for (int i = 0; i < n; ++i) {
        for (auto l : rects[i]) {
            if (l1 <= l && l <= l2) {
                int d = min({d2, query(ld, -l)});
                if (d1 <= d) {
                    int u = min({u2, query(lu, l), query(du, d)});
                    if (u1 <= u) {
                        int r3 = max({r1, -query(ur, u), -query(lrud, l), -query(lrdd, -l)});
                        int r4 = min({r2, query(dr, d), query(lruu, l), query(lrdu, -l)});
                        if (r3 <= r4) {
                            skewers = {{x1, l}, {x2, r3}, {d, y1}, {u, y2}};
                            return;
                        }
                    }
                }
            }
        }
    }
}

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

    int k;
    cin >> n >> k;

    for (int i = 0; i < n; ++i) {
        cin >> rects[i][0] >> rects[i][2] >> rects[i][1] >> rects[i][3];
    }

    solve(k);

    solve_4();

    if (!skewers.empty()) {
        print();
    }

    for (int i = 0; i < n; ++i) {
        auto &[x1, x2, y1, y2] = rects[i];
        y1 = X + 1 - y1;
        y2 = X + 1 - y2;
        swap(y1, y2);
    }

    solve_4();

    for (auto &[x, y] : skewers) {
        y = X + 1 - y;
    }

    print();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 3 ms 364 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 3 ms 364 KB Output is correct
10 Correct 3 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 3 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 3 ms 364 KB Output is correct
10 Correct 3 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 2 ms 364 KB Output is correct
13 Correct 8 ms 364 KB Output is correct
14 Correct 9 ms 364 KB Output is correct
15 Correct 7 ms 364 KB Output is correct
16 Correct 5 ms 364 KB Output is correct
17 Correct 9 ms 364 KB Output is correct
18 Correct 5 ms 492 KB Output is correct
19 Correct 8 ms 492 KB Output is correct
20 Correct 9 ms 492 KB Output is correct
21 Correct 8 ms 492 KB Output is correct
22 Correct 7 ms 492 KB Output is correct
23 Correct 9 ms 492 KB Output is correct
24 Correct 6 ms 492 KB Output is correct
25 Correct 4 ms 492 KB Output is correct
26 Correct 4 ms 492 KB Output is correct
27 Correct 5 ms 492 KB Output is correct
28 Correct 4 ms 492 KB Output is correct
29 Correct 4 ms 492 KB Output is correct
30 Correct 4 ms 492 KB Output is correct
31 Correct 8 ms 492 KB Output is correct
32 Correct 7 ms 492 KB Output is correct
33 Correct 9 ms 492 KB Output is correct
34 Correct 8 ms 492 KB Output is correct
35 Correct 9 ms 640 KB Output is correct
36 Correct 9 ms 620 KB Output is correct
37 Correct 10 ms 620 KB Output is correct
38 Correct 7 ms 492 KB Output is correct
39 Correct 9 ms 492 KB Output is correct
40 Correct 8 ms 492 KB Output is correct
41 Correct 9 ms 492 KB Output is correct
42 Correct 9 ms 492 KB Output is correct
43 Correct 9 ms 492 KB Output is correct
44 Correct 8 ms 512 KB Output is correct
45 Correct 5 ms 492 KB Output is correct
46 Correct 9 ms 492 KB Output is correct
47 Correct 9 ms 492 KB Output is correct
48 Correct 8 ms 492 KB Output is correct
49 Correct 9 ms 492 KB Output is correct
50 Correct 8 ms 492 KB Output is correct
51 Correct 9 ms 492 KB Output is correct
52 Correct 8 ms 492 KB Output is correct
53 Correct 9 ms 492 KB Output is correct
54 Correct 8 ms 492 KB Output is correct
55 Correct 9 ms 492 KB Output is correct
56 Correct 9 ms 492 KB Output is correct
57 Correct 9 ms 492 KB Output is correct
58 Correct 9 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 3 ms 364 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 97 ms 4716 KB Output is correct
6 Correct 94 ms 4716 KB Output is correct
7 Correct 95 ms 4844 KB Output is correct
8 Correct 94 ms 4716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 102 ms 4924 KB Output is correct
6 Correct 105 ms 5196 KB Output is correct
7 Correct 99 ms 4972 KB Output is correct
8 Correct 109 ms 4932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 3 ms 364 KB Output is correct
10 Correct 3 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 3 ms 364 KB Output is correct
13 Correct 101 ms 4844 KB Output is correct
14 Correct 99 ms 4716 KB Output is correct
15 Correct 99 ms 4844 KB Output is correct
16 Correct 99 ms 4844 KB Output is correct
17 Correct 100 ms 4844 KB Output is correct
18 Correct 97 ms 4844 KB Output is correct
19 Correct 101 ms 4844 KB Output is correct
20 Correct 115 ms 4816 KB Output is correct
21 Correct 241 ms 5012 KB Output is correct
22 Correct 188 ms 4892 KB Output is correct
23 Correct 147 ms 4872 KB Output is correct
24 Correct 155 ms 4944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 3 ms 364 KB Output is correct
10 Correct 3 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 2 ms 364 KB Output is correct
13 Correct 8 ms 364 KB Output is correct
14 Correct 9 ms 364 KB Output is correct
15 Correct 7 ms 364 KB Output is correct
16 Correct 5 ms 364 KB Output is correct
17 Correct 9 ms 364 KB Output is correct
18 Correct 5 ms 492 KB Output is correct
19 Correct 8 ms 492 KB Output is correct
20 Correct 9 ms 492 KB Output is correct
21 Correct 8 ms 492 KB Output is correct
22 Correct 7 ms 492 KB Output is correct
23 Correct 9 ms 492 KB Output is correct
24 Correct 6 ms 492 KB Output is correct
25 Correct 4 ms 492 KB Output is correct
26 Correct 4 ms 492 KB Output is correct
27 Correct 5 ms 492 KB Output is correct
28 Correct 4 ms 492 KB Output is correct
29 Correct 4 ms 492 KB Output is correct
30 Correct 4 ms 492 KB Output is correct
31 Correct 8 ms 492 KB Output is correct
32 Correct 7 ms 492 KB Output is correct
33 Correct 9 ms 492 KB Output is correct
34 Correct 8 ms 492 KB Output is correct
35 Correct 9 ms 640 KB Output is correct
36 Correct 9 ms 620 KB Output is correct
37 Correct 10 ms 620 KB Output is correct
38 Correct 7 ms 492 KB Output is correct
39 Correct 9 ms 492 KB Output is correct
40 Correct 8 ms 492 KB Output is correct
41 Correct 9 ms 492 KB Output is correct
42 Correct 9 ms 492 KB Output is correct
43 Correct 9 ms 492 KB Output is correct
44 Correct 8 ms 512 KB Output is correct
45 Correct 5 ms 492 KB Output is correct
46 Correct 9 ms 492 KB Output is correct
47 Correct 9 ms 492 KB Output is correct
48 Correct 8 ms 492 KB Output is correct
49 Correct 9 ms 492 KB Output is correct
50 Correct 8 ms 492 KB Output is correct
51 Correct 9 ms 492 KB Output is correct
52 Correct 8 ms 492 KB Output is correct
53 Correct 9 ms 492 KB Output is correct
54 Correct 8 ms 492 KB Output is correct
55 Correct 9 ms 492 KB Output is correct
56 Correct 9 ms 492 KB Output is correct
57 Correct 9 ms 492 KB Output is correct
58 Correct 9 ms 492 KB Output is correct
59 Correct 108 ms 12544 KB Output is correct
60 Correct 104 ms 12524 KB Output is correct
61 Correct 105 ms 12652 KB Output is correct
62 Correct 103 ms 12524 KB Output is correct
63 Correct 106 ms 12528 KB Output is correct
64 Correct 103 ms 12524 KB Output is correct
65 Correct 105 ms 12524 KB Output is correct
66 Correct 109 ms 12524 KB Output is correct
67 Correct 259 ms 12492 KB Output is correct
68 Correct 193 ms 12568 KB Output is correct
69 Correct 141 ms 12620 KB Output is correct
70 Correct 162 ms 12444 KB Output is correct
71 Correct 787 ms 12844 KB Output is correct
72 Correct 853 ms 13284 KB Output is correct
73 Correct 719 ms 12840 KB Output is correct
74 Correct 475 ms 12732 KB Output is correct
75 Correct 755 ms 12652 KB Output is correct
76 Correct 489 ms 12744 KB Output is correct
77 Correct 666 ms 13116 KB Output is correct
78 Correct 787 ms 12872 KB Output is correct
79 Correct 717 ms 12860 KB Output is correct
80 Correct 594 ms 13032 KB Output is correct
81 Correct 850 ms 12940 KB Output is correct
82 Correct 543 ms 13088 KB Output is correct
83 Correct 290 ms 12708 KB Output is correct
84 Correct 285 ms 12548 KB Output is correct
85 Correct 402 ms 12968 KB Output is correct
86 Correct 362 ms 12788 KB Output is correct
87 Correct 316 ms 12456 KB Output is correct
88 Correct 354 ms 12560 KB Output is correct
89 Correct 816 ms 13236 KB Output is correct
90 Correct 801 ms 12648 KB Output is correct
91 Correct 800 ms 13268 KB Output is correct
92 Correct 737 ms 12968 KB Output is correct
93 Correct 839 ms 13836 KB Output is correct
94 Correct 849 ms 13656 KB Output is correct
95 Correct 790 ms 13040 KB Output is correct
96 Correct 784 ms 13420 KB Output is correct
97 Correct 859 ms 13756 KB Output is correct
98 Correct 861 ms 12876 KB Output is correct
99 Correct 765 ms 13116 KB Output is correct
100 Correct 814 ms 12928 KB Output is correct
101 Correct 801 ms 12856 KB Output is correct
102 Correct 708 ms 12560 KB Output is correct
103 Correct 810 ms 12828 KB Output is correct
104 Correct 757 ms 12856 KB Output is correct
105 Correct 799 ms 12956 KB Output is correct
106 Correct 813 ms 13020 KB Output is correct
107 Correct 845 ms 13060 KB Output is correct
108 Correct 804 ms 13128 KB Output is correct
109 Correct 804 ms 12776 KB Output is correct
110 Correct 823 ms 13040 KB Output is correct
111 Correct 888 ms 13228 KB Output is correct
112 Correct 859 ms 13204 KB Output is correct
113 Correct 781 ms 13040 KB Output is correct
114 Correct 782 ms 12888 KB Output is correct
115 Correct 794 ms 12912 KB Output is correct
116 Correct 781 ms 13016 KB Output is correct
117 Correct 873 ms 14404 KB Output is correct
118 Correct 863 ms 14420 KB Output is correct
119 Correct 878 ms 14552 KB Output is correct
120 Correct 864 ms 14456 KB Output is correct
121 Correct 862 ms 14412 KB Output is correct
122 Correct 871 ms 14432 KB Output is correct
123 Correct 868 ms 14220 KB Output is correct
124 Correct 868 ms 14280 KB Output is correct
125 Correct 876 ms 14616 KB Output is correct
126 Correct 861 ms 14272 KB Output is correct