#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
struct pr
{
long long l, p;
};
bool compare(const pr& a, const pr& b) {
return a.l < b.l;
}
pr tab[1000];
pr tab_m[1000];
int main()
{
long long n, s;
int m;
scanf("%lld %d %lld", &n, &m, &s);
for (int i = 0; i < m; i++)
{
scanf("%lld %lld", &tab[i].l, &tab[i].p);
}
std::sort(tab, tab + m, compare);
int j = 0;
tab_m[j] = tab[0];
for (int i = 1; i < m; i++)
{
if (tab[i].l == tab[i - 1].p + 1)
{
tab_m[j].p = tab[i].p;
}
else
{
j++;
tab_m[j] = tab[i];
}
}
int si;
for (si = 0; si < j; si++)
{
if (tab_m[si].p >= s && tab_m[si].l <= s)
{
break;
}
}
long long res;
if (tab_m[si].p == n)
{
res = tab_m[si].l - 1;
}
else
if (tab_m[si].l == 1)
{
res = tab_m[si].p + 1;
}
else
{
res = tab_m[si].p - s < s - tab_m[si].l ? tab_m[si].p + 1 : tab_m[si].l - 1;
}
printf("%lld", res);
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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; struct pr { long long l, p; }; bool compare(const pr& a, const pr& b) { return a.l < b.l; } pr tab[1000]; pr tab_m[1000]; int main() { long long n, s; int m; scanf("%lld %d %lld", &n, &m, &s); for (int i = 0; i < m; i++) { scanf("%lld %lld", &tab[i].l, &tab[i].p); } std::sort(tab, tab + m, compare); int j = 0; tab_m[j] = tab[0]; for (int i = 1; i < m; i++) { if (tab[i].l == tab[i - 1].p + 1) { tab_m[j].p = tab[i].p; } else { j++; tab_m[j] = tab[i]; } } int si; for (si = 0; si < j; si++) { if (tab_m[si].p >= s && tab_m[si].l <= s) { break; } } long long res; if (tab_m[si].p == n) { res = tab_m[si].l - 1; } else if (tab_m[si].l == 1) { res = tab_m[si].p + 1; } else { res = tab_m[si].p - s < s - tab_m[si].l ? tab_m[si].p + 1 : tab_m[si].l - 1; } printf("%lld", res); return 0; } |
English