Submission #386586

# Submission time Handle Problem Language Result Execution time Memory
386586 2021-04-07T02:59:44 Z timmyfeng Hamburg Steak (JOI20_hamburg) C++17
15 / 100
238 ms 5196 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;
    }

    solve_4();

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

    print();
    */
}
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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 512 KB Output is correct
8 Correct 3 ms 364 KB Output is correct
9 Correct 4 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
# Verdict Execution time Memory Grader output
1 Correct 3 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 2 ms 364 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 364 KB Output is correct
13 Correct 8 ms 364 KB Output is correct
14 Correct 8 ms 364 KB Output is correct
15 Correct 7 ms 364 KB Output is correct
16 Correct 6 ms 364 KB Output is correct
17 Incorrect 9 ms 364 KB Unexpected end of file - int32 expected
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 92 ms 4716 KB Output is correct
6 Correct 97 ms 4716 KB Output is correct
7 Correct 96 ms 4844 KB Output is correct
8 Correct 92 ms 4716 KB Output is correct
# Verdict Execution time Memory 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 4844 KB Output is correct
6 Correct 113 ms 5196 KB Output is correct
7 Correct 97 ms 4972 KB Output is correct
8 Correct 106 ms 4940 KB Output is correct
# Verdict Execution time Memory 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 512 KB Output is correct
8 Correct 3 ms 364 KB Output is correct
9 Correct 4 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 99 ms 4844 KB Output is correct
14 Correct 97 ms 4716 KB Output is correct
15 Correct 101 ms 4848 KB Output is correct
16 Correct 97 ms 4844 KB Output is correct
17 Correct 97 ms 4844 KB Output is correct
18 Correct 97 ms 4844 KB Output is correct
19 Correct 99 ms 4844 KB Output is correct
20 Correct 108 ms 4844 KB Output is correct
21 Correct 238 ms 5012 KB Output is correct
22 Correct 203 ms 4764 KB Output is correct
23 Correct 145 ms 4844 KB Output is correct
24 Correct 170 ms 4900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 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 2 ms 364 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 364 KB Output is correct
13 Correct 8 ms 364 KB Output is correct
14 Correct 8 ms 364 KB Output is correct
15 Correct 7 ms 364 KB Output is correct
16 Correct 6 ms 364 KB Output is correct
17 Incorrect 9 ms 364 KB Unexpected end of file - int32 expected
18 Halted 0 ms 0 KB -