Submission #386505

#TimeUsernameProblemLanguageResultExecution timeMemory
386505phathnvPlanine (COCI21_planine)C++11
20 / 110
348 ms24848 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e6 + 7; struct Fraction{ ll p, q; Fraction(ll _p, ll _q){ p = _p; q = _q; assert(q != 0); if (q < 0){ p = -p; q = -q; } ll g = abs(__gcd(p, q)); p /= g; q /= g; } bool operator == (const Fraction &oth){ return (p == oth.p && q == oth.q); } bool operator < (const Fraction &oth) const { return ((long double) p * oth.q < (long double) oth.p * q); } }; struct Point{ int x, y; Point(int _x = 0, int _y = 0){ x = _x; y = _y; } }; struct Line{ ll a, b, c; Line(ll _a, ll _b, ll _c){ a = _a; b = _b; c = _c; } Line(const Point &A, const Point &B){ a = A.y - B.y; b = B.x - A.x; c = (ll) A.x * B.y - (ll) A.y * B.x; } }; int n, h; Point p[N]; Fraction findXCoor(const Line &d1, const Line &d2){ ll D = d1.a * d2.b - d2.a * d1.b; ll Dx = -d1.c * d2.b + d2.c * d1.b; assert(D != 0); return Fraction(Dx, D); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> h; for(int i = 1; i <= n; i++) cin >> p[i].x >> p[i].y; Line d(0, 1, -h); vector<pair<Fraction, Fraction>> segments; for(int i = 3; i <= n - 2; i += 2) segments.push_back({findXCoor(Line(p[i - 1], p[i]), d), findXCoor(Line(p[i], p[i + 1]), d)}); sort(segments.begin(), segments.end(), [](const pair<Fraction, Fraction> &a, const pair<Fraction, Fraction> &b){ return a.second < b.second; }); int answer = 0; Fraction lastX = Fraction(-1e18, 1); for(auto p : segments) if (lastX < p.first){ answer++; lastX = p.second; } cout << answer; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...