Convex Hull Trick
Author: Andi Qu
Prerequisites
A way to find the maximum or minimum value of several convex functions at given points.
Convex Hull Trick (CHT) problems are usually of the following form:
Given several convex functions such that any two functions intersect at most once, answer queries of the form "what is the maximum/minimum for some given ?".
This section is not complete.
Ben - be clearer about what conditions need to be satisfied
Examples of such functions include:
- The straight line ()
- The square-root function ()
There are several data structures one can use to solve these problems efficiently.
In this module, we'll focus on the special case of CHT where "slopes" of functions are monotonic. This specific case is solvable in using a std::deque
in C++. For the more general CHT, see the LineContainer module.
Focus Problem – read through this problem before continuing!
Tutorial
Resources | |||
---|---|---|---|
CF | solves problem above | ||
GCP | |||
Jeffrey Xiao |
Solution - The Fair Nut and Rectangles
I won't analyse this problem in great detail since the Codeforces blog in the resources already does so, but essentially, we sort the rectangles by -coordinate and get the following DP recurrence:
Notice how the part of the recurrence describes a straight line .
Since we sorted the rectangles and no two rectangles are nested, the slopes of the lines we insert are strictly increasing. The query positions are also strictly increasing.
This means we can solve this problem using CHT in time! Here is my implementation:
#include <bits/stdc++.h>typedef long long ll;using namespace std;struct Rect {ll x, y, a;bool operator<(Rect B) { return x < B.x; }};Rect a[1000001];
Problems
Status | Source | Problem Name | Difficulty | Tags | Solution |
---|---|---|---|---|---|
APIO | Easy | Show TagsDP, convex | External Sol | ||
CEOI | Easy | Show TagsDP, convex | View Solution | ||
IOI | Easy | Show TagsDP, convex | External Sol | ||
POI | Normal | Show TagsDP, convex | View Solution | ||
APIO | Normal | Show TagsDP, convex | |||
POI | Hard | Show TagsDP, convex | View Solution | ||
Plat | Hard | Show TagsDP, convex | External Sol | ||
Plat | Hard | Show Tagsconvex | External Sol | ||
JOI | Hard | Show TagsDP, convex | View Solution |