GastonFontenla

Room Allocation

Jun 14th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define evento pair<pair<int, int>, int>
  6. #define tiempo first.first
  7. #define tipoEvento first.second
  8. #define id second
  9.  
  10. int main()
  11. {
  12.     int n, llegada, salida;
  13.     cin >> n;
  14.     vector <evento> v;
  15.     v.reserve(n*2);
  16.     for(int i=0; i<n; i++)
  17.     {
  18.         cin >> llegada >> salida;
  19.         v.push_back({{llegada, 0}, i});
  20.         v.push_back({{salida, 1}, i});
  21.     }
  22.  
  23.     sort(v.begin(), v.end());
  24.  
  25.     stack <int> habitaciones;
  26.  
  27.     for(int i=n; i>=1; i--)
  28.         habitaciones.push(i);
  29.  
  30.     vector <int> asignado(n, -10);
  31.     int maxHab = 0;
  32.  
  33.     for(int i=0; i<v.size(); i++)
  34.     {
  35.         if(v[i].tipoEvento == 0)
  36.         {
  37.             asignado[v[i].id] = habitaciones.top();
  38.             maxHab = max(maxHab, habitaciones.top());
  39.             habitaciones.pop();
  40.         }
  41.         else
  42.         {
  43.             habitaciones.push(asignado[v[i].id]);
  44.         }
  45.     }
  46.  
  47.     cout << maxHab << endl;
  48.     for(int i=0; i<n; i++)
  49.         cout << asignado[i] << " ";
  50.     cout << endl;
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment