Approach
Sorting and greedy selection
For ABC273 D — LRUD Instructions, 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
- 57 lines of Python from the credited upstream file abc273_d.py.
- The implementation visibly relies on hash lookup, 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 from collections import defaultdict6 from bisect import bisect_left7 import sys8 9 input = sys.stdin.readline10 11 h, w, y, x = map(int, input().split())12 n = int(input())13 14 rows = defaultdict(lambda: [0, w + 1])15 cols = defaultdict(lambda: [0, h + 1])16 17 18 for i in range(n):19 ri, ci = map(int, input().split())20 rows[ri].append(ci)21 cols[ci].append(ri)22 23 for key, value in rows.items():24 rows[key] = sorted(value)25 26 for key, value in cols.items():27 cols[key] = sorted(value)28 29 q = int(input())30 ans = [(0, 0)] * q31 32 for i in range(q):33 di, li = input().rstrip().split()34 li = int(li)35 36 if di == "L":37 index = bisect_left(rows[y], x)38 x = max(x - li, rows[y][index - 1] + 1)39 elif di == "R":40 index = bisect_left(rows[y], x)41 x = min(x + li, rows[y][index] - 1)42 elif di == "U":43 index = bisect_left(cols[x], y)44 y = max(y - li, cols[x][index - 1] + 1)45 elif di == "D":46 index = bisect_left(cols[x], y)47 y = min(y + li, cols[x][index] - 1)48 49 ans[i] = (y, x)50 51 for ri, ci in ans:52 print(ri, ci)53 54 55if __name__ == "__main__":56 main()57