Approach
Sorting and greedy selection
For CCC 2014 S5 - Lazy Fox, 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
- 126 lines of Python from the credited upstream file ccc14s5.py.
- The implementation visibly relies on sequence storage.
- 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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 85 86file = open ("s5.15.in", "r")87N = int(file.readline())88 89pts = [[0, 0]]90for i in range(N):91 pt = file.readline().split()92 pts.append([int(pt[0]), int(pt[1])])93 94pairs = []95for a in range(N+1):96 for b in range(a+1, N+1):97 dx = pts[a][0] - pts[b][0]98 dy = pts[a][1] - pts[b][1]99 pairs.append([dx * dx + dy * dy, a, b])100 101pairs.sort()102 103best = [0] * (N+1)104pbest = [0] * (N+1)105pdist = [0] * (N+1)106 107for pair in pairs:108 d = pair[0]109 a = pair[1]110 b = pair[2]111 112 if d != pdist[a]:113 pdist[a] = d114 pbest[a] = best[a]115 if d != pdist[b]:116 pdist[b] = d117 pbest[b] = best[b]118 119 if a == 0: 120 best[a] = max(best[a], pbest[b])121 else:122 best[a] = max(best[a], pbest[b] + 1)123 best[b] = max(best[b], pbest[a] + 1) 124 125print best[0] + 1126