#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, m, s;
cin >> n >> m >> s;
vector<pair<long long, long long>> w(m);
for (int i = 0; i < m; i++)
{
cin >> w[i].first >> w[i].second;
}
sort(w.begin(), w.end());
long long r = -1;
long long maks = INT_MAX;
for (int i = 0; i < m; i++)
{
long long l;
if(i==0)
{
l = 1;
}
else
{
l = w[i-1].second+1;
}
long long p = w[i].first - 1;
if (p >= l)
{
long long c;
if(s<l)
{
c = l;
}
else
{
c = p;
}
long long d = abs(s - c);
if (d < maks || (d == maks && c < r))
{
r = c;
maks = d;
}
}
}
if (w.back().second < n)
{
long long c = w.back().second + 1;
long long d = abs(s - c);
if (d < maks || (d == maks && c < r))
{
r = c;
maks = d;
}
}
cout << r;
}
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 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, m, s; cin >> n >> m >> s; vector<pair<long long, long long>> w(m); for (int i = 0; i < m; i++) { cin >> w[i].first >> w[i].second; } sort(w.begin(), w.end()); long long r = -1; long long maks = INT_MAX; for (int i = 0; i < m; i++) { long long l; if(i==0) { l = 1; } else { l = w[i-1].second+1; } long long p = w[i].first - 1; if (p >= l) { long long c; if(s<l) { c = l; } else { c = p; } long long d = abs(s - c); if (d < maks || (d == maks && c < r)) { r = c; maks = d; } } } if (w.back().second < n) { long long c = w.back().second + 1; long long d = abs(s - c); if (d < maks || (d == maks && c < r)) { r = c; maks = d; } } cout << r; } |
English