#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef long double ld;
typedef unordered_map<int, int> umii;
typedef vector<pair<ll,ll>> vpll;
typedef tuple<int,int,int> tp;
const ll MOD = 1e9+696969;
void solve()
{
ll n, s, m;
cin >> n >> m >> s;
vpll part(m);
for(int i=0; i<m; ++i)
cin >> part[i].first >> part[i].second;
sort(part.begin(), part.end());
vpll free;
if(part[0].first>1)
free.push_back({1,part[0].first-1});
for(int i=0; i<m-1; ++i)
{
ll l=part[i].second +1;
ll r= part[i+1].first -1;
if(l<=r)
free.push_back({l,r});
}
if(part[m-1].second<n)
free.push_back({part[m-1].second+1,n});
ll best = LLONG_MAX;
ll bestbuild = -1;
for(auto x:free)
{
ll l=x.first;
ll r=x.second;
ll cand;
if(s<l)
cand =l;
else if(s>r)
cand = r;
else
cand =s;
ll dist=abs(s-cand);
if(dist<best or (dist==best and cand < bestbuild))
{
best=dist;
bestbuild=cand;
}
}
cout << bestbuild;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<pii> vpii; typedef vector<string> vs; typedef vector<char> vc; typedef vector<bool> vb; typedef long double ld; typedef unordered_map<int, int> umii; typedef vector<pair<ll,ll>> vpll; typedef tuple<int,int,int> tp; const ll MOD = 1e9+696969; void solve() { ll n, s, m; cin >> n >> m >> s; vpll part(m); for(int i=0; i<m; ++i) cin >> part[i].first >> part[i].second; sort(part.begin(), part.end()); vpll free; if(part[0].first>1) free.push_back({1,part[0].first-1}); for(int i=0; i<m-1; ++i) { ll l=part[i].second +1; ll r= part[i+1].first -1; if(l<=r) free.push_back({l,r}); } if(part[m-1].second<n) free.push_back({part[m-1].second+1,n}); ll best = LLONG_MAX; ll bestbuild = -1; for(auto x:free) { ll l=x.first; ll r=x.second; ll cand; if(s<l) cand =l; else if(s>r) cand = r; else cand =s; ll dist=abs(s-cand); if(dist<best or (dist==best and cand < bestbuild)) { best=dist; bestbuild=cand; } } cout << bestbuild; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; } |
English