답안 #516107

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
516107 2022-01-20T11:38:32 Z KoD Planine (COCI21_planine) C++17
110 / 110
243 ms 70176 KB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::pair;
using std::tuple;

using i64 = std::int64_t;

struct Point {
    int x, y;
    Point(int x = 0, int y = 0) : x(x), y(y) {}
    friend Point operator-(const Point& p, const Point& q) {
        return Point(p.x - q.x, p.y - q.y);
    } 
    friend i64 cross(const Point& p, const Point& q) {
        return (i64)p.x * q.y - (i64)p.y * q.x;
    }
};

struct Frac {
    i64 x, y;
    Frac(i64 x = 0, i64 y = 1) : x(x), y(y) {}
    friend Frac operator+(const Frac& a, const Frac& b) {
        return Frac(a.x * b.y + b.x * a.y, a.y * b.y);
    }
    friend Frac operator-(const Frac& a, const Frac& b) {
        return Frac(a.x * b.y - b.x * a.y, a.y * b.y);    
    }
    friend Frac operator*(const Frac& a, const Frac& b) {
        return Frac(a.x * b.x, a.y * b.y);
    }
    friend bool operator<(const Frac& a, const Frac& b) {
        return a.x * b.y < b.x * a.y;
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N, H;
    std::cin >> N >> H;
    vector<Point> P(N);
    for (auto& [x, y] : P) {
        std::cin >> x >> y;
    }
    vector<Point> pts;
    vector<Frac> left(N), right(N);
    for (int i = 1; i < N - 1; ++i) {
        if (i % 2 == 1) {
            int n = pts.size();
            while (n >= 2) {
                if (cross(P[i] - pts[n - 1], pts[n - 2] - pts[n - 1]) >= 0) {
                    pts.pop_back();
                    n -= 1;
                } else {
                    break;
                }
            }
            pts.push_back(P[i]);
        } else {
            int ok = 0, ng = pts.size();
            while (ng - ok > 1) {
                const int md = (ok + ng) / 2;
                (cross(P[i] - pts[md], pts[md - 1] - pts[md]) < 0 ? ok : ng) = md;
            }
            left[i] = Frac(P[i].x) + Frac(pts[ok].x - P[i].x) * Frac(H - P[i].y, pts[ok].y - P[i].y);
        }
    }
    pts.clear();
    for (int i = N - 2; i >= 1; --i) {
        if (i % 2 == 1) {
            int n = pts.size();
            while (n >= 2) {
                if (cross(P[i] - pts[n - 1], pts[n - 2] - pts[n - 1]) <= 0) {
                    pts.pop_back();
                    n -= 1;
                } else {
                    break;
                }
            }
            pts.push_back(P[i]);
        } else {
            int ok = 0, ng = pts.size();
            while (ng - ok > 1) {
                const int md = (ok + ng) / 2;
                (cross(P[i] - pts[md], pts[md - 1] - pts[md]) > 0 ? ok : ng) = md;
            }
            right[i] = Frac(P[i].x) + Frac(pts[ok].x - P[i].x) * Frac(H - P[i].y, pts[ok].y - P[i].y);
        }
    }
    vector<pair<Frac, Frac>> interval;
    for (int i = 2; i < N - 2; i += 2) {
        interval.emplace_back(right[i], left[i]);
    }
    std::sort(interval.begin(), interval.end());
    Frac last(-2000000000000);
    int ans = 0;
    for (const auto& [r, l] : interval) {
        if (last < l) {
            ans += 1;
            last = r;
        }
    }
    std::cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1100 KB Output is correct
2 Correct 2 ms 1228 KB Output is correct
3 Correct 3 ms 1140 KB Output is correct
4 Correct 19 ms 7412 KB Output is correct
5 Correct 22 ms 7612 KB Output is correct
6 Correct 23 ms 7712 KB Output is correct
7 Correct 188 ms 65956 KB Output is correct
8 Correct 217 ms 68004 KB Output is correct
9 Correct 220 ms 69804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 1 ms 452 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 436 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 0 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1100 KB Output is correct
2 Correct 2 ms 1228 KB Output is correct
3 Correct 3 ms 1140 KB Output is correct
4 Correct 19 ms 7412 KB Output is correct
5 Correct 22 ms 7612 KB Output is correct
6 Correct 23 ms 7712 KB Output is correct
7 Correct 188 ms 65956 KB Output is correct
8 Correct 217 ms 68004 KB Output is correct
9 Correct 220 ms 69804 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 1 ms 452 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 436 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 0 ms 316 KB Output is correct
17 Correct 206 ms 66980 KB Output is correct
18 Correct 231 ms 66856 KB Output is correct
19 Correct 27 ms 7468 KB Output is correct
20 Correct 218 ms 66088 KB Output is correct
21 Correct 24 ms 7348 KB Output is correct
22 Correct 235 ms 69796 KB Output is correct
23 Correct 1 ms 312 KB Output is correct
24 Correct 232 ms 69028 KB Output is correct
25 Correct 21 ms 7480 KB Output is correct
26 Correct 243 ms 70176 KB Output is correct
27 Correct 10 ms 3904 KB Output is correct