Approach
Sorting and greedy selection
For ABC360 D — Ghost Ants, 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
- 58 lines of Python from the credited upstream file abc360_d.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 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 main():19 import sys20 21 input = sys.stdin.readline22 23 n, t = map(int, input().split())24 s = input().rstrip()25 x = list(map(int, input().split()))26 27 28 29 y = list()30 31 for xi, si in zip(x, s):32 if si == "1":33 continue34 35 y.append(xi)36 37 inf = 10**1838 y = [-inf] + sorted(y) + [inf]39 ans = 040 41 for xi, si in zip(x, s):42 if si == "0":43 continue44 45 j, _ = bisect_le(y, xi + 2 * t)46 i, _ = bisect_le(y, xi)47 48 if i is None or j is None:49 continue50 51 ans += max(j - i, 0)52 53 print(ans)54 55 56if __name__ == "__main__":57 main()58