import sys
def read_int_list():
return list(map(int, [x for x in sys.stdin.readline().strip().split(' ') if x != '']))
if __name__=='__main__':
n, m, s = read_int_list()
Z = []
for i in range(m):
d, g = read_int_list()
Z.append((d, g))
Z.sort()
T = []
T.append(Z[0])
for (d, g) in Z[1:]:
if d == T[-1][1] + 1:
T[-1] = (T[-1][0], g)
else:
T.append((d, g))
p = None
for (d, g) in T:
if d > 1 and (p is None or abs(p-s) > abs(d-1-s)):
p = d-1
if g < n and (p is None or abs(p-s) > abs(g+1-s)):
p = g+1
print(p)
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 | import sys def read_int_list(): return list(map(int, [x for x in sys.stdin.readline().strip().split(' ') if x != ''])) if __name__=='__main__': n, m, s = read_int_list() Z = [] for i in range(m): d, g = read_int_list() Z.append((d, g)) Z.sort() T = [] T.append(Z[0]) for (d, g) in Z[1:]: if d == T[-1][1] + 1: T[-1] = (T[-1][0], g) else: T.append((d, g)) p = None for (d, g) in T: if d > 1 and (p is None or abs(p-s) > abs(d-1-s)): p = d-1 if g < n and (p is None or abs(p-s) > abs(g+1-s)): p = g+1 print(p) |
English