Skip to content

Commit e02ca4f

Browse files
committed
chore: add daily leetcode post Counting Stars-Inter-Uni Programming Contest
1 parent 90968ba commit e02ca4f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: Counting Stars-Inter-Uni Programming Contest.md
3+
date: 22/9/2024
4+
tags:
5+
- Contest
6+
- Python
7+
- Binary Search
8+
abbrlink: a29b0a05
9+
---
10+
11+
# Description:
12+
13+
https://interunia.unswcpmsoc.com/task/Counting%20Stars/
14+
15+
# Thinking:
16+
17+
- Given a set of star positions, we need to calculate the minimum number of stars required to explain these positions.
18+
- 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.
19+
- Each meteor may appear in multiple positions (because it moves), and the final cumulative image will show all the positions it has passed through.
20+
- The positions of fixed stars remain unchanged.
21+
22+
Therefore, we need to maintain a list of the **last y-coordinate of the current chain**.
23+
1. **Sort the points**: Sort them by increasing x coordinates.
24+
2. **Initialization**: Create an empty list `last_y` to store the last y-coordinate of each chain.
25+
3. **Traverse the set of points**:
26+
- For each point (x, y):
27+
- Use `bisect_right` to find the first position in `last_y` that is greater than the current y.
28+
- 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.
29+
- 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 to `last_y`.
30+
31+
# Code:
32+
33+
```python
34+
import bisect
35+
36+
n = int(input())
37+
stars = []
38+
39+
for _ in range(n):
40+
x, y = map(int, input().split())
41+
stars.append((x, y))
42+
43+
# 按 x 坐标递增排序
44+
stars.sort(key=lambda x: (x[0],))
45+
46+
last_y = []
47+
48+
for x, y in stars:
49+
idx = bisect.bisect_right(last_y, y)
50+
if idx < len(last_y):
51+
last_y[idx] = y # 更新链的最后一个 y 坐标
52+
else:
53+
last_y.append(y) # 创建新的链
54+
55+
print(len(last_y))
56+
```

0 commit comments

Comments
 (0)