Approach
Sorting and greedy selection
For ARC161 B — Exactly Three Bits, 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 arc161_b.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 3from bisect import bisect_right4from typing import List5 6 7def bisect_le(sorted_array: List[int], value: int):8 """Find the largest element <= x and its index, or None if it doesn't exist."""9 10 if sorted_array[0] <= value:11 index: int = bisect_right(sorted_array, value) - 112 13 return index, sorted_array[index]14 15 return None, None16 17 18def solve(candidates):19 n = int(input())20 21 _, value = bisect_le(candidates, n)22 23 if value is None:24 print(-1)25 else:26 print(value)27 28 29def main():30 import sys31 32 input = sys.stdin.readline33 34 t = int(input())35 36 from itertools import combinations37 38 candidates = []39 40 m = 6041 42 for i, j, k in combinations(range(m), 3):43 if i == j or j == k or i == k:44 continue45 46 candidate = 2**i + 2**j + 2**k47 candidates.append(candidate)48 49 candidates.sort()50 51 for _ in range(t):52 solve(candidates)53 54 55if __name__ == "__main__":56 main()57