Description:
https://interunia.unswcpmsoc.com/task/Counting%20Stars/
Thinking:
- Given a set of star positions, we need to calculate the minimum number of stars required to explain these positions.
- Meteors (i.e., moving stars) move from left to right, and from high to low (x coordinates increase, y coordinates decrease), without moving horizontally or vertically.
- Each meteor may appear in multiple positions (because it moves), and the final cumulative image will show all the positions it has passed through.
- The positions of fixed stars remain unchanged.
Therefore, we need to maintain a list of the last y-coordinate of the current chain.
- Sort the points: Sort them by increasing x coordinates.
- Initialization: Create an empty list
last_y
to store the last y-coordinate of each chain. - Traverse the set of points:
- For each point (x, y):
- Use
bisect_right
to find the first position inlast_y
that is greater than the current y. - If the index is less than the length of
last_y
, it means there is an existing chain that can accommodate the current point, so we update the last y-coordinate of that chain to the current y. - If the index is equal to the length of
last_y
, it means no suitable chain is found, so we need to create a new chain and add the current y tolast_y
.
- Use
- For each point (x, y):
Code:
1 | import bisect |