目录

CF1388E-Uncle Bogdan and Projections

CF1388E-Uncle Bogdan and Projections

题目:

题目描述:

After returning to shore, uncle Bogdan usually visits the computer club “The Rock”, to solve tasks in a pleasant company. One day, uncle Bogdan met his good old friend who told him one unusual task…

There are $ n $ non-intersecting horizontal segments with ends in integers points on the plane with the standard cartesian coordinate system. All segments are strictly above the $ OX $ axis. You can choose an arbitrary vector ( $ a $ , $ b $ ), where $ b < 0 $ and coordinates are real numbers, and project all segments to $ OX $ axis along this vector. The projections shouldn’t intersect but may touch each other.

Find the minimum possible difference between $ x $ coordinate of the right end of the rightmost projection and $ x $ coordinate of the left end of the leftmost projection.

输入格式:

The first line contains the single integer $ n $ ( $ 1 \le n \le 2000 $ ) — the number of segments.

The $ i $ -th of the next $ n $ lines contains three integers $ xl_i $ , $ xr_i $ and $ y_i $ ( $ -10^6 \le xl_i < xr_i \le 10^6 $ ; $ 1 \le y_i \le 10^6 $ ) — coordinates of the corresponding segment.

It’s guaranteed that the segments don’t intersect or touch.

输出格式:

Print the minimum possible difference you can get.

Your answer will be considered correct if its absolute or relative error doesn’t exceed $ 10^{-6} $ .

Formally, if your answer is $ a $ and jury’s answer is $ b $ then your answer will be considered correct if $ \frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6} $ .

样例:

样例输入 1:

1
2
3
4
3
1 6 2
4 6 4
4 6 6

样例输出 1:

1
9.000000000

样例输入 2:

1
2
3
4
3
2 5 1
4 6 4
7 8 2

样例输出 2:

1
6.333333333

样例输入 3:

1
2
3
2
1 3 1
4 7 1

样例输出 3:

1
6.000000000

思路:

实现:

  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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "ybwhead/ios.h"

#define pb push_back
#define x first
#define y second

using namespace std;

const int N = 1e5 + 10;
const double eps = 1e-9;

double xl[N], xr[N], y[N], pi = acos(-1), mn_x, mx_x;
int ind_l, ind_r;

double point_pr(double x, double y, double ctg)
{
    return x - y * ctg;
}

int main()
{
    int n;
    vector<pair<double, int>> q;
    vector<pair<double, pair<int, int>>> events, prom_left, prom_right;
    yin >> n;
    pair<double, double> mx, mn;
    mx = {-1.0, -1.0};
    mn = {2e9, 2e9};
    mn_x = 2e9;
    mx_x = -2e9;
    for (int i = 0; i < n; ++i)
    {
        yin >> xl[i] >> xr[i] >> y[i];

        if (xl[i] < mn_x)
        {
            mn_x = xl[i];
        }
        if (xr[i] > mx_x)
        {
            mx_x = xr[i];
        }

        if (mx.y < y[i])
        {
            mx.y = y[i];
            mx.x = xl[i];
            ind_l = i;
        }
        else if (mx.y == y[i] && mx.x > xl[i])
        {
            mx.y = y[i];
            mx.x = xl[i];
            ind_l = i;
        }

        if (mn.y > y[i])
        {
            mn.y = y[i];
            mn.x = xl[i];
            ind_r = i;
        }
        else if (mn.y == y[i] && mn.x < xl[i])
        {
            mn.y = y[i];
            mn.x = xl[i];
            ind_r = i;
        }
    }
    double a1, a2;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            if (y[i] > y[j])
            {
                a1 = (xr[i] - xl[j]) / (y[i] - y[j]);
                a2 = (xl[i] - xr[j]) / (y[i] - y[j]);
                q.pb({a1, 1});
                q.pb({a2, 2});

                a1 = (xl[i] - xl[j]) / (y[i] - y[j]);
                a2 = (xr[i] - xr[j]) / (y[i] - y[j]);
                events.pb({a1, {i, j}});
                events.pb({a2, {-i - 1, j}});
            }
        }
    }

    if (q.empty())
    {
        yout << mx_x - mn_x << endl;
        return 0;
    }

    sort(q.rbegin(), q.rend());
    int cnt = 0;
    double last = 0;
    for (auto i : q)
    {
        if (i.y == 2)
        {
            --cnt;
            if (!cnt)
            {
                events.pb({i.x, {-1e9, -1e9}});
            }
        }
        else
        {
            if (!cnt)
            {
                events.pb({i.x, {-1e9, -1e9}});
            }
            ++cnt;
        }
    }
    sort(events.rbegin(), events.rend());
    double ans = 1e18, ang;
    last = -1e18;
    for (auto i : events)
    {
        if (i.y.x == i.y.y)
        {
            unordered_set<int> s;
            vector<int> to_check;
            for (auto j : prom_left)
            {
                s.insert(j.y.x);
                if (j.y.x == ind_l)
                {
                    to_check.pb(j.y.y);
                }
            }
            prom_left.clear();
            for (auto j : to_check)
            {
                if (!s.count(j))
                {
                    ind_l = j;
                    break;
                }
            }
            s.clear();
            to_check.clear();

            for (auto j : prom_right)
            {
                s.insert(j.y.y);
                if (j.y.y == ind_r)
                {
                    to_check.pb(-j.y.x - 1);
                }
            }
            prom_right.clear();
            for (auto j : to_check)
            {
                if (!s.count(j))
                {
                    ind_r = j;
                    break;
                }
            }
            s.clear();
            to_check.clear();

            double res = point_pr(xr[ind_r], y[ind_r], i.x) - point_pr(xl[ind_l], y[ind_l], i.x);
            if (ans > res)
            {
                ans = res;
                ang = i.x;
            }
        }
        else if (i.y.x < 0)
        {
            if (abs(i.x - last) > eps)
            {
                unordered_set<int> s;
                vector<int> to_check;
                for (auto j : prom_right)
                {
                    s.insert(j.y.y);
                    if (j.y.y == ind_r)
                    {
                        to_check.pb(-j.y.x - 1);
                    }
                }
                prom_right.clear();
                for (auto j : to_check)
                {
                    if (!s.count(j))
                    {
                        ind_r = j;
                        break;
                    }
                }
                s.clear();
                to_check.clear();
            }
            prom_right.pb(i);
        }
        else
        {
            if (abs(i.x - last) > eps)
            {
                unordered_set<int> s;
                vector<int> to_check;
                for (auto j : prom_left)
                {
                    s.insert(j.y.x);
                    if (j.y.x == ind_l)
                    {
                        to_check.pb(j.y.y);
                    }
                }
                prom_left.clear();
                for (auto j : to_check)
                {
                    if (!s.count(j))
                    {
                        ind_l = j;
                        break;
                    }
                }
                s.clear();
                to_check.clear();
            }
            prom_left.pb(i);
        }
        last = i.x;
    }
    yout << ans << endl;
    return 0;
}