Skip to content

Commit 695ab47

Browse files
authored
Merge pull request #420 from aricthecoder/day5sol2
Day 5 Solved room allocation problem
2 parents bece94b + 7733218 commit 695ab47

1 file changed

Lines changed: 153 additions & 0 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
3+
Problem : Room allocation CSES
4+
5+
Approach : sort guests according to their arrival time
6+
now we need to allocate room to a guest as soon as one become vacant (done by min heap)
7+
8+
Time Complexity : O(nlogn)
9+
Space Complexity : O(n);
10+
11+
Submission Link : https://cses.fi/problemset/result/15777656/
12+
13+
*/
14+
15+
#include <bits/stdc++.h>
16+
#include <bits/stdc++.h>
17+
#include <ext/pb_ds/assoc_container.hpp>
18+
#include <ext/pb_ds/tree_policy.hpp>
19+
using namespace std;
20+
using namespace __gnu_pbds;
21+
22+
#define fast_io ios::sync_with_stdio(false); cin.tie(nullptr);
23+
#define int long long
24+
#define ull unsigned long long
25+
#define ld long double
26+
#define vi vector<int>
27+
#define vb vector<bool>
28+
#define vs vector<string>
29+
#define pii pair<int,int>
30+
#define pll pair<long long,long long>
31+
#define vpi vector<pii>
32+
#define vpl vector<pll>
33+
#define vvi vector<vi>
34+
#define mii map<int,int>
35+
#define si set<int>
36+
#define osi ordered_set<int>
37+
#define loop(i,n) for (int i = 0; i < n; i++)
38+
#define rloop(i,n) for (int i = n - 1; i >= 0; i--)
39+
#define loop1(i,a,b) for (int i = a; i <= b; i++)
40+
#define rloop1(i,a,b) for (int i = a; i >= b; i--)
41+
#define pb push_back
42+
#define ppb pop_back
43+
#define mp make_pair
44+
#define F first
45+
#define S second
46+
#define lb lower_bound
47+
#define ub upper_bound
48+
#define all(x) (x).begin(), (x).end()
49+
#define rall(x) (x).rbegin(), (x).rend()
50+
#define sz(x) ((int)(x).size())
51+
#define findbo(x) find_by_order(x) // returns iterator to the k-th largest element (0-based)
52+
#define orderbk(x) order_of_key(x) // returns number of items strictly smaller than x
53+
#define endl '\n'
54+
#define printy cout<<"YES"<<endl
55+
#define printn cout<<"NO"<<endl
56+
#define vout(a) for (int i = 0; i < a.size(); i++) cout << a[i] << ' '; cout << endl;
57+
#define vpout(a) loop(i,sz(a)) cout <<a[i].F<<' '<< a[i].S<< endl; cout << endl;
58+
#define rt(x){ cout<<x<<endl; return; }
59+
template<typename T>
60+
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // less_equal<T> for multiset, greater<T> for decreasing order
61+
62+
const int MOD = 1e9 + 7;
63+
const int INF = 1e9;
64+
65+
#ifndef ONLINE_JUDGE
66+
#define debug(x) cerr<<#x<<" = "; _print(x); cerr<<endl;
67+
template<typename T> void _print(const T& x){ cerr<<x; }
68+
template<typename T,typename U> void _print(const pair<T,U>& p){ cerr<<"("; _print(p.first); cerr<<", "; _print(p.second); cerr<<")"; }
69+
template<typename T> void _print(const vector<T>& v){ cerr<<"["; for(size_t i=0;i<v.size();++i){ _print(v[i]); if(i+1!=v.size()) cerr<<", "; } cerr<<"]"; }
70+
template<typename T> void _print(const set<T>& s){ cerr<<"{"; for(auto it=s.begin(); it!=s.end(); ++it){ _print(*it); if(next(it)!=s.end()) cerr<<", "; } cerr<<"}"; }
71+
template<typename T,typename U> void _print(const map<T,U>& m){ cerr<<"{"; for(auto it=m.begin(); it!=m.end(); ++it){ _print(it->first); cerr<<": "; _print(it->second); if(next(it)!=m.end()) cerr<<", "; } cerr<<"}"; }
72+
#else
73+
#define debug(x)
74+
#endif
75+
76+
#define sumV(a) accumulate(all(a), 0LL)
77+
#define minV(a) min_element(all(a))
78+
#define maxV(a) max_element(all(a))
79+
80+
int log2f(long long n) {
81+
if (n==0) return -1;
82+
int res = 0;
83+
while (n > 1){
84+
n >>= 1;
85+
res++;
86+
}
87+
return res;
88+
}
89+
90+
int binpow(int a, int n) {
91+
int result = 1;
92+
a %= MOD;
93+
while (n > 0) {
94+
if (n & 1)
95+
result = (result * a) % MOD;
96+
a = (a * a) % MOD;
97+
n >>= 1;
98+
}
99+
return result;
100+
}
101+
102+
void solve(){
103+
int n;
104+
cin>>n;
105+
vpi v;
106+
loop(i,n){
107+
int a,b;
108+
cin>>a>>b;
109+
v.pb({a,b});
110+
}
111+
112+
vi ans(n) ,idx(n);
113+
iota(all(idx),0);
114+
115+
sort(all(idx),[&](int i,int j){
116+
return v[i]<v[j];
117+
});
118+
119+
sort(all(v));
120+
priority_queue<pii,vpi,greater<>> pq;
121+
122+
int maxroom = 0;
123+
loop(i,n){
124+
int arr = v[i].F, dep = v[i].S, room;
125+
if(pq.empty()){
126+
pq.push({dep,1});
127+
room = 1;
128+
}else{
129+
if(pq.top().F>=arr){
130+
room = sz(pq)+1;
131+
pq.push({dep,room});
132+
}else{
133+
room = pq.top().S;
134+
pq.pop();
135+
pq.push({dep,room});
136+
}
137+
}
138+
maxroom = max(maxroom,sz(pq));
139+
ans[idx[i]]=room;
140+
}
141+
cout<<maxroom<<endl;
142+
vout(ans);
143+
}
144+
145+
int32_t main(){
146+
fast_io;
147+
int t=1;
148+
// cin>>t;
149+
while(t--){
150+
solve();
151+
}
152+
return 0;
153+
}

0 commit comments

Comments
 (0)