백준 24894번: 내적 [C++]

플래티넘 I 플래티넘 I

문제

내적

풀이

내적의 정의 $x_ix_j + y_iy_j$에서 식을 $y_i$로 묶으면

\[x_ix_j + y_iy_j = y_i\left(x_j\frac{x_i}{y_i} + y_j\right)\]

변수가 $x_i/y_i$이고 기울기가 $x_j$, y 절편이 $y_j$인 직선으로 생각할 수 있습니다. 따라서 $x_j$를 기준으로 오름차순 정렬한 뒤 벡터마다 내적을 구해 최댓값을 출력하면 됩니다.

시간 복잡도는 $\mathcal{O}(N\log N)$입니다.

코드

코드에서는 유리수 구조체를 이용해 계산했지만 start를 long double 등으로 관리하고 값이 아니라 직선을 반환하는 식으로 작성해도 됩니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define ASSERT(x) assert(x)
#else
#define ASSERT(X) ((void)(0))
#endif

using ll = long long;
using i128 = __int128;

i128 gcd(i128 a, i128 b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

struct Fraction {
    i128 numer, denom;

    Fraction(ll _numer, ll _denom): numer(_numer), denom(_denom) {
        if (denom < 0) {
            numer = -numer;
            denom = -denom;
        }

        reduce();
    }

    void reduce() {
        bool isNegative = numer < 0;

        if (isNegative)
            numer = -numer;

        i128 g = gcd(numer, denom);
        numer /= g;
        denom /= g;

        if (isNegative)
            numer = -numer;
    }

    bool operator<(const Fraction& f) const {
        return (numer * f.denom) < (denom * f.numer);
    }
    bool operator<=(const Fraction& f) const {
        return (numer * f.denom) <= (denom * f.numer);
    }

    Fraction& operator*=(const ll& x) {
        numer *= x;
        reduce();
        return *this;
    }
    Fraction operator*(const ll& x) const {
        Fraction ret(*this);
        ret *= x;
        return ret;
    }

    Fraction& operator+=(const ll& x) {
        numer += denom * x;
        reduce();
        return *this;
    }
    Fraction operator+(const ll& x) const {
        Fraction ret(*this);
        ret += x;
        return ret;
    }
};

struct Line {
    ll slope, yinter;
    Fraction start;

    Line(ll _slope, ll _yinter):
    slope(_slope),
    yinter(_yinter),
    start(LLONG_MIN, 1) {}

    Fraction getInter(const Line& l) {
        return Fraction(yinter - l.yinter, l.slope - slope);
    }
};

struct LineContainer {
    vector<Line> con;

    void emplace(ll slope, ll yinter) {
        Line l(slope, yinter);

        while (not con.empty()) {
            Line back = con.back();
            ASSERT(l.slope >= back.slope);

            if (l.slope == back.slope) {
                if (l.yinter > back.yinter) {
                    con.pop_back();
                    continue;
                }
                return;
            }

            l.start = l.getInter(back);
            if (l.start <= back.start)
                con.pop_back();
            else
                break;
        }

        con.push_back(l);
    }

    Fraction query(Fraction x) {
        ASSERT(not con.empty());

        Line l = *(upper_bound(
            con.begin(),
            con.end(),
            x,
            [](const Fraction& f, const Line& l) {
                return f < l.start;
            }
        ) - 1);

        return x * l.slope + l.yinter;
    }
};

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

    int N;
    cin >> N;

    vector<pair<ll, ll>> vectors(N);
    for (auto& [x, y] : vectors)
        cin >> x >> y;
    sort(vectors.begin(), vectors.end());

    LineContainer con;
    con.emplace(vectors[0].first, vectors[0].second);
    ll answer = 0;
    for (int i = 1; i < N; i++) {
        auto [x, y] = vectors[i];

        Fraction q = con.query(Fraction(x, y));
        q *= y;
        ASSERT(q.denom == 1);
        answer = max(answer, (ll)q.numer);
        con.emplace(x, y);
    }

    cout << answer;
    return 0;
}

백준 24894번: 내적 [C++]