Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- struct Department{
- ll a, b;
- bool operator<(const Department& v) const{
- return a<v.a;
- }
- };
- int t;
- int main(){
- //freopen("buyinggifts.in", "r", stdin);
- cin >> t;
- while(t--){
- int n;
- cin >> n;
- vector<Department> dep(n);
- vector<ll> mx(n, -1);
- for(int i=0; i<n; i++){
- cin >> dep[i].a >> dep[i].b;
- }
- sort(dep.begin(), dep.end());
- ll mxv = -1;
- //!!!Need to use base values as -1 to check with right values at line 41 because there are some
- //values that are possibly equal to 0;
- for(int i=n-1; i>=0; i--){
- mx[i] = mxv;
- mxv = max(mxv, dep[i].b);
- //Get suffix to compare later on in calculation
- }
- set<ll> works;
- ll ans = LLONG_MAX;
- for(int i=0; i<n; i++){
- if(i<n-1){
- ans = min(ans, abs(mx[i]-dep[i].a));
- //check with max values because max values are always valid
- }
- if(dep[i].a>mx[i] && !works.empty()){
- auto it = works.upper_bound(dep[i].a);
- //get upper_bound and check out of bounds to see which values work under top condition
- //on line 44
- if(it!=works.end()){
- ans = min(ans, abs(*it-dep[i].a));
- }
- //it--;
- if(it!=works.begin()){
- it--;
- ans = min(ans, abs(*it-dep[i].a));
- }
- }
- works.insert(dep[i].b);
- //Keep values that can be valid to be used
- }
- cout << ans << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement