Skip to content

Instantly share code, notes, and snippets.

@lakshith-403
Created October 23, 2021 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lakshith-403/d65ff995a992e7148328a50a14c31eac to your computer and use it in GitHub Desktop.
Save lakshith-403/d65ff995a992e7148328a50a14c31eac to your computer and use it in GitHub Desktop.
/**
* @author Lakshith Nishshanke
* @brief
* @version 0.1
* @date 2021-10-23
*
* @copyright Copyright (c) 2021
*
* Used a greedy approach. Sorted time frames first by their starting time,second by their endind time.
* And then greedily checked if it is possible to reserve dates in the sorted order.
* I don't have a mathematical proof but considering constraints solution should be approx. O(n log n). Too much for DP.
* So should be a greedy. Hope this works
*/
#include <bits/stdc++.h>
using namespace std;
inline void io(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void solve(){
int n;
cin >> n;
vector<pair<int,int>> vec;
for(int i=0;i<n;i++){
int a,b;
cin >> a >> b;
vec.push_back({a,b});
}
sort(vec.begin(),vec.end());
vector<int> ans;
int day = vec[0].first;
int pat = 0;
for(int i=0;i<n;i++){
if(vec[pat].second<day||vec[pat].first>day){
cout << "IMPOSSIBLE\n";
return;
}
ans.push_back(pat++);
day++;
}
for(int x:ans)cout << x+1 << " ";
cout << "\n";
}
signed main(){
//comment before submision
freopen("in", "r", stdin);
freopen("out", "w", stdout);
io();
int n;
cin >> n;
while(n--)
solve();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment