import sys def main(): lines = sys.stdin.read().splitlines() line1 = lines[0].split() n = int(line1[0]) m = int(line1[1]) s = int(line1[2]) def czy_w_przedziale(x, zwroc_start): for _ in range(1,m+1): l = lines[_].split() start = int(l[0]) end = int(l[1]) if start <= x <= end: return start - 1 if zwroc_start else end + 1 return 0 i = s odDolu = 0 while i > 0: wynik = czy_w_przedziale(i, True) if wynik == 0: odDolu = i break else: i = wynik j = s odGory = n while j < n: wynik = czy_w_przedziale(j, False) if wynik == 0: odGory = j break else: j = wynik if abs(s - odDolu) == abs(s - odGory): print(odDolu) elif abs(s - odDolu) < abs(s - odGory): print(odDolu) else: print(odGory) if __name__ == "__main__": main()
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 | import sys def main(): lines = sys.stdin.read().splitlines() line1 = lines[0].split() n = int(line1[0]) m = int(line1[1]) s = int(line1[2]) def czy_w_przedziale(x, zwroc_start): for _ in range(1,m+1): l = lines[_].split() start = int(l[0]) end = int(l[1]) if start <= x <= end: return start - 1 if zwroc_start else end + 1 return 0 i = s odDolu = 0 while i > 0: wynik = czy_w_przedziale(i, True) if wynik == 0: odDolu = i break else: i = wynik j = s odGory = n while j < n: wynik = czy_w_przedziale(j, False) if wynik == 0: odGory = j break else: j = wynik if abs(s - odDolu) == abs(s - odGory): print(odDolu) elif abs(s - odDolu) < abs(s - odGory): print(odDolu) else: print(odGory) if __name__ == "__main__": main() |