Skip to content

Commit 50fde74

Browse files
authored
Create day005sol02.cpp
1 parent 0627fc3 commit 50fde74

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Submission Link: https://cses.fi/problemset/result/15799460/
2+
3+
/*
4+
-> I made a vector which stores pair(pair(arrival, departue), person no.).
5+
-> I sorted so that person coming first gets room first.
6+
-> I made a priority queue min-heap(element with lower value have higher priority) and stored element as
7+
pair(departure, room no.)
8+
-> Now I compared each incoming element with the top one only as it has been emptied first. If a[i].first.first then remove
9+
first element and pushed the incoming element and in ans vector put the room else I would add no. of room and push the element
10+
in pq and in ans vector put the room. Now I printed rm(no. of room) and vector ans(in which room each person stays).
11+
*/
12+
13+
14+
#include <bits/stdc++.h>
15+
using namespace std;
16+
17+
int main() {
18+
int n;
19+
cin>>n;
20+
vector<pair<pair<int,int>,int>>a(n);
21+
for(int i=0;i<n;i++) {
22+
cin>>a[i].first.first;
23+
cin>>a[i].first.second;
24+
a[i].second=i;
25+
}
26+
sort(a.begin(),a.end());
27+
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
28+
int rm=1;
29+
pq.push({a[0].first.second,rm});
30+
vector <int> ans(n);
31+
ans[a[0].second]=1;
32+
for(int i=1;i<n;i++) {
33+
int d=pq.top().first;
34+
int r=pq.top().second;
35+
if(a[i].first.first>d) {
36+
pq.pop();
37+
pq.push({a[i].first.second,r});
38+
ans[a[i].second]=r;
39+
}
40+
else {
41+
rm++;
42+
pq.push({a[i].first.second,rm});
43+
ans[a[i].second]=rm;
44+
}
45+
}
46+
cout<<rm<<endl;
47+
for(int i=0;i<n;i++) cout<<ans[i]<<" ";
48+
cout<<endl;
49+
}

0 commit comments

Comments
 (0)