import sys
input = sys.stdin.readline
n, m, s = map(int, input().split())
tab = []
for _ in range(m):
tab.append(tuple(map(int, input().split())))
tab.sort()
new_tab = [tab[0]]
for a, b in tab[1:]:
if a == new_tab[-1][1] + 1:
new_tab[-1] = (new_tab[-1][0], b)
else:
new_tab.append((a, b))
# print(tab)
# print(new_tab)
best, where = 10**15, -1
for a, b in new_tab:
if a > 1:
if abs(s - (a - 1)) < best:
best = abs(s - (a - 1))
where = a - 1
if b < n:
if abs(s - (b + 1)) < best:
best = abs(s - (b + 1))
where = b + 1
print(where)
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 | import sys input = sys.stdin.readline n, m, s = map(int, input().split()) tab = [] for _ in range(m): tab.append(tuple(map(int, input().split()))) tab.sort() new_tab = [tab[0]] for a, b in tab[1:]: if a == new_tab[-1][1] + 1: new_tab[-1] = (new_tab[-1][0], b) else: new_tab.append((a, b)) # print(tab) # print(new_tab) best, where = 10**15, -1 for a, b in new_tab: if a > 1: if abs(s - (a - 1)) < best: best = abs(s - (a - 1)) where = a - 1 if b < n: if abs(s - (b + 1)) < best: best = abs(s - (b + 1)) where = b + 1 print(where) |
English