CSES - Room Allocation

Author: Shreyas Thumathy

In this problem, we're asked the minimum number of rooms needed to accommodate nn customers, which arrive and leave on set days.

Main Idea

  • First Step: Sort by arrival time (we cannot have a customer arriving at say, time 3, occupying a room before a customer arriving at time 2).
  • Second Step: Maintain a minimum heap which stores the time a hotel room will be unoccupied.
    • Every time a new customer arrvies, we check to see if the minimum element in the heap is less than the arrival time of the new customer.
    • If this is true, we can just switch the times.
    • If not, then we need to add another room to the heap.
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int N;
int ans[200005];
vector<pair<pair<int, int>, int> > v(200005);

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!