x = input().split(" ") n, m, s = int(x[0]), int(x[1]), int(x[2]) tabs = [] for _ in range(m): xx = input().split(" ") tabs.append([int(x) for x in xx]) tabs.append([n+1,n+1]) tabs.append([0,0]) sorted_tabs = sorted(tabs, key=lambda x: x[0]) curr_tab = -1 is_taken = False min_ = 10**12+1 res = 0 for i in range(m+1): if sorted_tabs[i][1]+1 < sorted_tabs[i+1][0]: if abs(sorted_tabs[i][1]+1-s)< min_ and sorted_tabs[i][1]+1!=s: res = sorted_tabs[i][1]+1 min_ = abs(sorted_tabs[i][1]+1-s) if abs(sorted_tabs[i+1][0]-1-s)< min_ and sorted_tabs[i+1][0]-1!=s: res = sorted_tabs[i+1][0]-1 min_ = abs(sorted_tabs[i+1][0]-1-s) if s >= sorted_tabs[i][1] + 1 and s <= sorted_tabs[i+1][0]-1: if s-1 > sorted_tabs[i][1]: res = s-1 min_ = 1 elif s+1 < sorted_tabs[i+1][0]: res = s+1 min_ = 1 print(res)
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 | x = input().split(" ") n, m, s = int(x[0]), int(x[1]), int(x[2]) tabs = [] for _ in range(m): xx = input().split(" ") tabs.append([int(x) for x in xx]) tabs.append([n+1,n+1]) tabs.append([0,0]) sorted_tabs = sorted(tabs, key=lambda x: x[0]) curr_tab = -1 is_taken = False min_ = 10**12+1 res = 0 for i in range(m+1): if sorted_tabs[i][1]+1 < sorted_tabs[i+1][0]: if abs(sorted_tabs[i][1]+1-s)< min_ and sorted_tabs[i][1]+1!=s: res = sorted_tabs[i][1]+1 min_ = abs(sorted_tabs[i][1]+1-s) if abs(sorted_tabs[i+1][0]-1-s)< min_ and sorted_tabs[i+1][0]-1!=s: res = sorted_tabs[i+1][0]-1 min_ = abs(sorted_tabs[i+1][0]-1-s) if s >= sorted_tabs[i][1] + 1 and s <= sorted_tabs[i+1][0]-1: if s-1 > sorted_tabs[i][1]: res = s-1 min_ = 1 elif s+1 < sorted_tabs[i+1][0]: res = s+1 min_ = 1 print(res) |