-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(Searching and Sorting) Room Allocation
46 lines (42 loc) · 1.12 KB
/
(Searching and Sorting) Room Allocation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
#define int long long
const long long mod =1e9+7;
using namespace std;
int32_t main(){
int n;
cin>>n;
vector<pair<pair<int, int>,int> > vec(n);
for(int i=0 ;i<n; i++){
cin>>vec[i].first.first>>vec[i].first.second;
vec[i].second=i;
}
sort(vec.begin(), vec.end());
vector<int> ans(n);
int curr=0, total=0;
priority_queue<pair<int, int>> pq;
for(int i=0; i<n; i++){
if(pq.empty()){
curr++;
pq.push({-1*vec[i].first.second, curr});
ans[vec[i].second]=curr;
}else{
pair<int, int> v = pq.top();
if(-1*v.first<vec[i].first.first){
pq.pop();
pq.push({-1*vec[i].first.second, v.second});
ans[vec[i].second]=v.second;
}else{
curr++;
pq.push({-1*vec[i].first.second, curr});
ans[vec[i].second]=curr;
}
}
total=max(curr, total);
}
cout<<total<<endl;
for(int i=0 ;i<n; i++){
cout<<ans[i]<<" ";
}
return 0;
}