CSES - Restaurant Customers
Author: Michael Cao
In this problem, we're given intervals with distinct start and end points, and we want to find the maximum number of intervals overlapping some point.
Solution
Let's transform the problem by assigning start points the value and end points the value . Then, if we put the start and end points into an array and sort them, the problem becomes: "given an array of values, find the maximum sum at some prefix in the array." This can be done by looping over the array and storing a running sum.
Warning!
Make sure to not store the values in an array
, as the maximum value can be up to , and instead use something like a vector
.
Code
1#include <bits/stdc++.h>2using namespace std;34using ll = long long;56using vi = vector<int>;7#define pb push_back8#define rsz resize9#define all(x) begin(x), end(x)10#define sz(x) (int)(x).size()
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!