Approach
Sorting and greedy selection
For ABC275 C — Counting Squares, 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
- 60 lines of Python from the credited upstream file abc275_c.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.
12 3 4def main():5 from itertools import combinations6 import sys7 8 input = sys.stdin.readline9 10 s = [input().rstrip() for _ in range(9)]11 xy = list()12 count = 013 14 for x in range(9):15 for y in range(9):16 if s[y][x] == "#":17 xy.append((x, y))18 count += 119 20 ans = 021 22 23 def is_square(points):24 25 26 27 dist = list() 28 29 for i in range(4):30 x1, y1 = points[i]31 32 for j in range(i + 1, 4):33 x2, y2 = points[j]34 35 dx = (x2 - x1) ** 236 dy = (y2 - y1) ** 237 38 dist.append(dx + dy)39 40 dist = sorted(dist)41 base = dist[0]42 43 if base == 0:44 return False 45 46 if dist.count(base) == 4 and dist.count(base * 2) == 2:47 return True48 else:49 return False50 51 for a, b, c, d in combinations(range(count), 4):52 if is_square((xy[a], xy[b], xy[c], xy[d])):53 ans += 154 55 print(ans)56 57 58if __name__ == "__main__":59 main()60