给定由分别由斜率(m)和截距(c)组成的二维数组 arr表示的一组线,以及Q查询,使得每个查询都包含一个值x 。任务是从所有给定的一组行中为每个x值找到 y 的最大值。
The given lines are represented by the equation y = m*x + c.
例子:
Input: arr[][2] ={ {1, 1}, {0, 0}, {-3, 3} }, Q = {-2, 2, 1}
Output: 9, 3, 2
For query x = -2, y values from the equations are -1, 0, 9. So the maximum value is 9
Similarly, for x = 2, y values are 3, 0, -3. So the maximum value is 3
And for x = 1, values of y = 2, 0, 0. So the maximum value is 2.
Input: arr[][] ={ {5, 6}, {3, 2}, {7, 3} }, Q = { 1, 2, 30 }
Output: 10, 17, 213
朴素的方法:朴素的方法是在每一行中替换 x的值并计算所有行中的最大值。对于每个查询,将花费O(N)时间,因此解决方案的复杂性变为O(Q * N) ,其中N是行数, Q是查询数。
有效的方法:想法是使用凸包技巧:
- 从给定的一组行中,可以找到并删除没有意义的行(对于 x 的任何值,它们永远不会给出最大值y ),从而减少该组。
- 现在,如果可以找到每行给出最大值的范围 (l, r) ,则可以使用二分搜索来回答每个查询。
- 因此,创建了一个按斜率递减顺序排列的直线向量,并按斜率递减顺序插入这些直线。
下面是上述方法的实现:
// C++ implementation of
// the above approach
#include
using namespace std;
struct Line {
int m, c;
public:
// Sort the line in decreasing
// order of their slopes
bool operator<(Line l)
{
// If slopes aren't equal
if (m != l.m)
return m > l.m;
// If the slopes are equal
else
return c > l.c;
}
// Checks if line L3 or L1 is better than L2
// Intersection of Line 1 and
// Line 2 has x-coordinate (b1-b2)/(m2-m1)
// Similarly for Line 1 and
// Line 3 has x-coordinate (b1-b3)/(m3-m1)
// Cross multiplication will
// give the below result
bool check(Line L1, Line L2, Line L3)
{
return (L3.c - L1.c) * (L1.m - L2.m)
< (L2.c - L1.c) * (L1.m - L3.m);
}
};
struct Convex_HULL_Trick {
// To store the lines
vector l;
// Add the line to the set of lines
void add(Line newLine)
{
int n = l.size();
// To check if after adding the new line
// whether old lines are
// losing significance or not
while (n >= 2
&& newLine.check(l[n - 2],
l[n - 1],
newLine)) {
n--;
}
l.resize(n);
// Add the present line
l.push_back(newLine);
}
// Function to return the y coordinate
// of the specified line
// for the given coordinate
int value(int in, int x)
{
return l[in].m * x + l[in].c;
}
// Function to Return the maximum value
// of y for the given x coordinate
int maxQuery(int x)
{
// if there is no lines
if (l.empty())
return INT_MAX;
int low = 0,
high = (int)l.size() - 2;
// Binary search
while (low <= high) {
int mid = (low + high) / 2;
if (value(mid, x)
< value(mid + 1, x))
low = mid + 1;
else
high = mid - 1;
}
return value(low, x);
}
};
// Driver code
int main()
{
Line lines[] = { { 1, 1 },
{ 0, 0 },
{ -3, 3 } };
int Q[] = { -2, 2, 1 };
int n = 3, q = 3;
Convex_HULL_Trick cht;
// Sort the lines
sort(lines, lines + n);
// Add the lines
for (int i = 0; i < n; i++)
cht.add(lines[i]);
// For each query in Q
for (int i = 0; i < q; i++) {
int x = Q[i];
cout << cht.maxQuery(x) << endl;
}
return 0;
}
9
3
2
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。