0% found this document useful (0 votes)
6 views2 pages

Sesti

The document contains a C++ program that processes a set of pairs of long integers, sorting them and calculating the minimum difference between certain elements based on a given condition. It utilizes a set to efficiently find the closest values while iterating through the sorted pairs. The result is printed at the end of the program, which is structured to handle multiple test cases, although only one is executed in this instance.

Uploaded by

ilina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Sesti

The document contains a C++ program that processes a set of pairs of long integers, sorting them and calculating the minimum difference between certain elements based on a given condition. It utilizes a set to efficiently find the closest values while iterating through the sorted pairs. The result is printed at the end of the program, which is structured to handle multiple test cases, although only one is executed in this instance.

Uploaded by

ilina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <bits/stdc++.

h>
#define endl '\n'
#define pb push_back
#define ll long long
#define fi first
#define se second
#define pll pair<ll,ll>

using namespace std;

void tests()
{
ll n,d,ptr,res=1e9,i;
// ll *pok1,*pok2;
cin >> n >> d;
vector<pll>x(n);
for(auto &x:x)
{
cin >> x.fi >> x.se;
}
set<ll>st;
sort(x.begin(),x.end(),greater<pll>());
ptr=0;
for(i=0;i<n;i++)
{
while(x[ptr].se-x[i].se>=d)
{
st.insert(x[ptr].fi);
ptr++;
}
if(st.empty())
{
continue;
}
auto pok1=st.lower_bound(x[i].fi);
auto pok2=st.upper_bound(x[i].fi);
if(pok1!=st.begin())
{
pok1--;
res=min(res,x[i].fi-(*pok1));
}
if(pok2!=st.end())
{
res=min(res,*pok2-x[i].fi);
}
}
if(res==1e9)
{
res-=1e9+1;
}
cout << res << endl;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt=1;
// cin >> tt;
while(tt--)
{
tests();
}
}

You might also like