Approach
Sorting and greedy selection
For ARC121 A — 2nd Greatest Distance, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 75 lines of Python from the credited upstream file arc121_a.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4def main():5 import sys6 7 input = sys.stdin.readline8 9 n = int(input())10 xy = list()11 12 for i in range(n):13 xi, yi = map(int, input().split())14 xy.append((xi, yi, i))15 16 sorted_by_x = sorted(xy, key=lambda x: x[0])17 sorted_by_y = sorted(xy, key=lambda x: x[1])18 19 ans = list()20 21 ans.append(22 (23 abs(sorted_by_x[0][0] - sorted_by_x[-2][0]),24 sorted_by_x[0][2],25 sorted_by_x[-2][2],26 )27 )28 ans.append(29 (30 abs(sorted_by_x[1][0] - sorted_by_x[-1][0]),31 sorted_by_x[1][2],32 sorted_by_x[-1][2],33 )34 )35 ans.append(36 (37 abs(sorted_by_y[0][1] - sorted_by_y[-2][1]),38 sorted_by_y[0][2],39 sorted_by_y[-2][2],40 )41 )42 ans.append(43 (44 abs(sorted_by_y[1][1] - sorted_by_y[-1][1]),45 sorted_by_y[1][2],46 sorted_by_y[-1][2],47 )48 )49 ans.append(50 (51 abs(sorted_by_x[0][0] - sorted_by_x[-1][0]),52 sorted_by_x[0][2],53 sorted_by_x[-1][2],54 )55 )56 ans.append(57 (58 abs(sorted_by_y[0][1] - sorted_by_y[-1][1]),59 sorted_by_y[0][2],60 sorted_by_y[-1][2],61 )62 )63 64 ans = sorted(ans, reverse=True)65 _, i1, j1 = ans[0]66 67 for value, i2, j2 in ans[1:]:68 if i1 != i2 or j1 != j2:69 print(value)70 exit()71 72 73if __name__ == "__main__":74 main()75