This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 7;
struct Fraction{
ll p, q;
Fraction(ll _p = 0, ll _q = 1){
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) const {
return (p == oth.p && q == oth.q);
}
bool operator < (const Fraction &oth) const {
return ((long double) p * oth.q < (long double) oth.p * q);
}
bool operator <= (const Fraction &oth) const {
return (oth == (*this) || (*this) < oth);
}
};
struct Point{
int x, y;
Point(int _x = 0, int _y = 0){
x = _x;
y = _y;
}
Point operator - (const Point &oth) const {
return Point(x - oth.x, y - oth.y);
}
ll operator * (const Point &oth) const {
return (ll) x * oth.y - (ll) y * oth.x;
}
};
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];
int findOrientation(const Point &A, const Point &B, const Point &C){
ll x = (B - A) * (C - B);
if (x < 0)
return -1;
else if (x == 0)
return 0;
else
return 1;
}
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(n / 2 - 1);
vector<Point> cvh;
for(int i = 1; i <= n; i++){
while (cvh.size() > 1 && findOrientation(cvh[cvh.size() - 2], cvh[cvh.size() - 1], p[i]) > -1)
cvh.pop_back();
if (i % 2 && 3 <= i && i <= n - 2)
segments[i / 2 - 1].first = findXCoor(Line(p[i], cvh.back()), d);
cvh.push_back(p[i]);
}
for(int i = n; i >= 1; i--){
while (cvh.size() > 1 && findOrientation(cvh[cvh.size() - 2], cvh[cvh.size() - 1], p[i]) < 1)
cvh.pop_back();
if (i % 2 && 3 <= i && i <= n - 2)
segments[i / 2 - 1].second = findXCoor(Line(p[i], cvh.back()), d);
cvh.push_back(p[i]);
}
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |