#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
template<class T> void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T> void chkmx(T &x, T y) { if (y > x) x = y; }
using namespace std;
const int maxn = 1010;
ll n, s, ans = 1e18;
pll a[maxn], b[maxn];
int m, tot;
void check(ll x) {
if (llabs(x - s) < llabs(ans - s)) ans = x;
else if (llabs(x - s) == llabs(ans - s) && x < ans) ans = x;
}
int main() {
cin.tie(nullptr) -> ios::sync_with_stdio(false);
cin >> n >> m >> s;
rep (i, 1, m) cin >> a[i].fi >> a[i].se;
sort(a + 1, a + m + 1);
if (a[1].fi > 1) b[++tot] = pll(1, a[1].fi - 1);
rep (i, 2, m) if (a[i].fi > a[i - 1].se + 1) b[++tot] = pll(a[i - 1].se + 1, a[i].fi - 1);
if (a[m].se < n) b[++tot] = pll(a[m].se + 1, n);
rep (i, 1, tot) check(b[i].fi), check(b[i].se);
cout << ans << '\n';
}
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 | #include <bits/stdc++.h> #define rep(i, j, k) for (int i = (j); i <= (k); ++i) #define per(i, j, k) for (int i = (j); i >= (k); --i) #define SZ(v) int((v).size()) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second using ll = long long; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; template<class T> void chkmn(T &x, T y) { if (y < x) x = y; } template<class T> void chkmx(T &x, T y) { if (y > x) x = y; } using namespace std; const int maxn = 1010; ll n, s, ans = 1e18; pll a[maxn], b[maxn]; int m, tot; void check(ll x) { if (llabs(x - s) < llabs(ans - s)) ans = x; else if (llabs(x - s) == llabs(ans - s) && x < ans) ans = x; } int main() { cin.tie(nullptr) -> ios::sync_with_stdio(false); cin >> n >> m >> s; rep (i, 1, m) cin >> a[i].fi >> a[i].se; sort(a + 1, a + m + 1); if (a[1].fi > 1) b[++tot] = pll(1, a[1].fi - 1); rep (i, 2, m) if (a[i].fi > a[i - 1].se + 1) b[++tot] = pll(a[i - 1].se + 1, a[i].fi - 1); if (a[m].se < n) b[++tot] = pll(a[m].se + 1, n); rep (i, 1, tot) check(b[i].fi), check(b[i].se); cout << ans << '\n'; } |
English